1919import traceback
2020from contextlib import contextmanager
2121from io import StringIO
22- from typing import Dict , List , Optional
22+ from typing import TYPE_CHECKING , Any , Iterator
2323
24+ from pytkdocs import debug
2425from pytkdocs .loader import Loader
25- from pytkdocs .objects import Object
2626from pytkdocs .serializer import serialize_object
2727
28-
29- from pytkdocs import debug
28+ if TYPE_CHECKING :
29+ from pytkdocs . objects import Object
3030
3131
3232class _DebugInfo (argparse .Action ):
@@ -39,16 +39,15 @@ def __call__(self, *args: Any, **kwargs: Any) -> None: # noqa: ARG002
3939
4040
4141def process_config (config : dict ) -> dict :
42- """
43- Process a loading configuration.
42+ """Process a loading configuration.
4443
4544 The `config` argument is a dictionary looking like this:
4645
4746 ```python
4847 {
4948 "objects": [
5049 {"path": "python.dotted.path.to.the.object1"},
51- {"path": "python.dotted.path.to.the.object2"}
50+ {"path": "python.dotted.path.to.the.object2"},
5251 ]
5352 }
5453 ```
@@ -69,7 +68,7 @@ def process_config(config: dict) -> dict:
6968 "path.to.object2": [
7069 "message1",
7170 "message2",
72- ]
71+ ],
7372 },
7473 "objects": [
7574 {
@@ -80,7 +79,7 @@ def process_config(config: dict) -> dict:
8079 "path": "path.to.object2",
8180 # other attributes, see the documentation for `pytkdocs.objects` or `pytkdocs.serializer`
8281 },
83- ]
82+ ],
8483 }
8584 ```
8685
@@ -114,8 +113,7 @@ def process_config(config: dict) -> dict:
114113
115114
116115def process_json (json_input : str ) -> dict :
117- """
118- Process JSON input.
116+ """Process JSON input.
119117
120118 Simply load the JSON as a Python dictionary, then pass it to [`process_config`][pytkdocs.cli.process_config].
121119
@@ -129,39 +127,36 @@ def process_json(json_input: str) -> dict:
129127
130128
131129def extract_docstring_parsing_errors (errors : dict , obj : Object ) -> None :
132- """
133- Recursion helper.
130+ """Recursion helper.
134131
135132 Update the `errors` dictionary by side-effect. Recurse on the object's children.
136133
137134 Arguments:
138135 errors: The dictionary to update.
139136 obj: The object.
140137 """
141- if hasattr (obj , "docstring_errors" ) and obj .docstring_errors : # noqa: WPS421 (hasattr)
138+ if hasattr (obj , "docstring_errors" ) and obj .docstring_errors :
142139 errors [obj .path ] = obj .docstring_errors
143140 for child in obj .children :
144141 extract_docstring_parsing_errors (errors , child )
145142
146143
147144def extract_errors (obj : Object ) -> dict :
148- """
149- Extract the docstring parsing errors of each object, recursively, into a flat dictionary.
145+ """Extract the docstring parsing errors of each object, recursively, into a flat dictionary.
150146
151147 Arguments:
152148 obj: An object from `pytkdocs.objects`.
153149
154150 Returns:
155151 A flat dictionary. Keys are the objects' names.
156152 """
157- parsing_errors : Dict [str , List [str ]] = {}
153+ parsing_errors : dict [str , list [str ]] = {}
158154 extract_docstring_parsing_errors (parsing_errors , obj )
159155 return parsing_errors
160156
161157
162158def get_parser () -> argparse .ArgumentParser :
163- """
164- Return the program argument parser.
159+ """Return the program argument parser.
165160
166161 Returns:
167162 The argument parser for the program.
@@ -180,9 +175,8 @@ def get_parser() -> argparse.ArgumentParser:
180175
181176
182177@contextmanager
183- def discarded_stdout ():
184- """
185- Discard standard output.
178+ def discarded_stdout () -> Iterator [None ]:
179+ """Discard standard output.
186180
187181 Yields:
188182 Nothing: We only yield to act as a context manager.
@@ -218,14 +212,14 @@ def main(args: list[str] | None = None) -> int:
218212 with discarded_stdout ():
219213 try :
220214 output = json .dumps (process_json (line ))
221- except Exception as error : # noqa: W0703 (we purposely catch everything)
215+ except Exception as error : # noqa: BLE001
222216 # Don't fail on error. We must handle the next inputs.
223217 # Instead, print error as JSON.
224218 output = json .dumps ({"error" : str (error ), "traceback" : traceback .format_exc ()})
225- print (output ) # noqa: WPS421 (we need to print at some point)
219+ print (output )
226220 else :
227221 with discarded_stdout ():
228222 output = json .dumps (process_json (sys .stdin .read ()))
229- print (output ) # noqa: WPS421 (we need to print at some point)
223+ print (output )
230224
231225 return 0
0 commit comments