11
11
import sys
12
12
import traceback
13
13
from collections import ChainMap
14
- from subprocess import PIPE , Popen # noqa: S404
15
- from typing import Any , BinaryIO , Iterator , List , Optional , Sequence , Tuple
14
+ from subprocess import PIPE , Popen
15
+ from typing import TYPE_CHECKING , Any , BinaryIO , Iterator
16
16
17
- from markdown import Markdown
18
-
19
- from mkdocstrings .extension import PluginError
20
- from mkdocstrings .handlers .base import BaseHandler , CollectionError , CollectorItem
21
- from mkdocstrings .inventory import Inventory
22
- from mkdocstrings .loggers import get_logger
23
17
from mkdocstrings_handlers .python .rendering import (
24
18
do_brief_xref ,
25
19
rebuild_category_lists ,
28
22
sort_object ,
29
23
)
30
24
25
+ from mkdocstrings .extension import PluginError
26
+ from mkdocstrings .handlers .base import BaseHandler , CollectionError , CollectorItem
27
+ from mkdocstrings .inventory import Inventory
28
+ from mkdocstrings .loggers import get_logger
29
+
30
+ if TYPE_CHECKING :
31
+ from markdown import Markdown
32
+
31
33
# TODO: add a deprecation warning once the new handler handles 95% of use-cases
32
34
33
35
logger = get_logger (__name__ )
@@ -116,12 +118,12 @@ class PythonHandler(BaseHandler):
116
118
117
119
- `show_bases` (`bool`): Show the base classes of a class. Default: `True`.
118
120
- `show_source` (`bool`): Show the source code of this object. Default: `True`.
119
- """ # noqa: E501
121
+ """
120
122
121
- def __init__ ( # noqa: WPS231
123
+ def __init__ (
122
124
self ,
123
125
* args ,
124
- setup_commands : Optional [ List [ str ]] = None ,
126
+ setup_commands : list [ str ] | None = None ,
125
127
config_file_path : str | None = None ,
126
128
paths : list [str ] | None = None ,
127
129
** kwargs ,
@@ -150,17 +152,16 @@ def __init__( # noqa: WPS231
150
152
paths .append (os .path .dirname (config_file_path ))
151
153
search_paths = []
152
154
for path in paths :
153
- if not os .path .isabs (path ):
154
- if config_file_path :
155
- path = os .path .abspath (os .path .join (os .path .dirname (config_file_path ), path ))
155
+ if not os .path .isabs (path ) and config_file_path :
156
+ path = os .path .abspath (os .path .join (os .path .dirname (config_file_path ), path ))
156
157
if path not in search_paths :
157
158
search_paths .append (path )
158
159
self ._paths = search_paths
159
160
160
161
commands = []
161
162
162
163
if search_paths :
163
- commands .extend ([f"sys.path.insert(0, { path !r} )" for path in reversed (search_paths )]) # noqa: WPS441
164
+ commands .extend ([f"sys.path.insert(0, { path !r} )" for path in reversed (search_paths )])
164
165
165
166
if setup_commands :
166
167
# prevent the Python interpreter or the setup commands
@@ -172,7 +173,7 @@ def __init__( # noqa: WPS231
172
173
* setup_commands ,
173
174
"sys.stdout.flush()" ,
174
175
"sys.stdout = sys.__stdout__" , # restore stdout
175
- ]
176
+ ],
176
177
)
177
178
178
179
if commands :
@@ -186,7 +187,7 @@ def __init__( # noqa: WPS231
186
187
else :
187
188
cmd = [sys .executable , "-m" , "pytkdocs" , "--line-by-line" ]
188
189
189
- self .process = Popen ( # noqa: S603,S607 (we trust the input, and we don't want to use the absolute path)
190
+ self .process = Popen (
190
191
cmd ,
191
192
universal_newlines = True ,
192
193
stdout = PIPE ,
@@ -198,8 +199,12 @@ def __init__( # noqa: WPS231
198
199
199
200
@classmethod
200
201
def load_inventory (
201
- cls , in_file : BinaryIO , url : str , base_url : Optional [str ] = None , ** kwargs
202
- ) -> Iterator [Tuple [str , str ]]:
202
+ cls ,
203
+ in_file : BinaryIO ,
204
+ url : str ,
205
+ base_url : str | None = None ,
206
+ ** kwargs ,
207
+ ) -> Iterator [tuple [str , str ]]:
203
208
"""Yield items and their URLs from an inventory file streamed from `in_file`.
204
209
205
210
This implements mkdocstrings' `load_inventory` "protocol" (see plugin.py).
@@ -216,10 +221,10 @@ def load_inventory(
216
221
if base_url is None :
217
222
base_url = posixpath .dirname (url )
218
223
219
- for item in Inventory .parse_sphinx (in_file , domain_filter = ("py" ,)).values (): # noqa: WPS526
224
+ for item in Inventory .parse_sphinx (in_file , domain_filter = ("py" ,)).values ():
220
225
yield item .name , posixpath .join (base_url , item .uri )
221
226
222
- def collect (self , identifier : str , config : dict ) -> CollectorItem : # noqa: WPS231
227
+ def collect (self , identifier : str , config : dict ) -> CollectorItem :
223
228
"""Collect the documentation tree given an identifier and selection options.
224
229
225
230
In this method, we feed one line of JSON to the standard input of the subprocess that was opened
@@ -338,9 +343,9 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore
338
343
339
344
340
345
def get_handler (
341
- theme : str , # noqa: W0613 (unused argument config)
342
- custom_templates : Optional [ str ] = None ,
343
- setup_commands : Optional [ List [ str ]] = None ,
346
+ theme : str ,
347
+ custom_templates : str | None = None ,
348
+ setup_commands : list [ str ] | None = None ,
344
349
config_file_path : str | None = None ,
345
350
paths : list [str ] | None = None ,
346
351
** config : Any ,
0 commit comments