1111import sys
1212import traceback
1313from 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
1616
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
2317from mkdocstrings_handlers .python .rendering import (
2418 do_brief_xref ,
2519 rebuild_category_lists ,
2822 sort_object ,
2923)
3024
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+
3133# TODO: add a deprecation warning once the new handler handles 95% of use-cases
3234
3335logger = get_logger (__name__ )
@@ -116,12 +118,12 @@ class PythonHandler(BaseHandler):
116118
117119 - `show_bases` (`bool`): Show the base classes of a class. Default: `True`.
118120 - `show_source` (`bool`): Show the source code of this object. Default: `True`.
119- """ # noqa: E501
121+ """
120122
121- def __init__ ( # noqa: WPS231
123+ def __init__ (
122124 self ,
123125 * args ,
124- setup_commands : Optional [ List [ str ]] = None ,
126+ setup_commands : list [ str ] | None = None ,
125127 config_file_path : str | None = None ,
126128 paths : list [str ] | None = None ,
127129 ** kwargs ,
@@ -150,17 +152,16 @@ def __init__( # noqa: WPS231
150152 paths .append (os .path .dirname (config_file_path ))
151153 search_paths = []
152154 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 ))
156157 if path not in search_paths :
157158 search_paths .append (path )
158159 self ._paths = search_paths
159160
160161 commands = []
161162
162163 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 )])
164165
165166 if setup_commands :
166167 # prevent the Python interpreter or the setup commands
@@ -172,7 +173,7 @@ def __init__( # noqa: WPS231
172173 * setup_commands ,
173174 "sys.stdout.flush()" ,
174175 "sys.stdout = sys.__stdout__" , # restore stdout
175- ]
176+ ],
176177 )
177178
178179 if commands :
@@ -186,7 +187,7 @@ def __init__( # noqa: WPS231
186187 else :
187188 cmd = [sys .executable , "-m" , "pytkdocs" , "--line-by-line" ]
188189
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 (
190191 cmd ,
191192 universal_newlines = True ,
192193 stdout = PIPE ,
@@ -198,8 +199,12 @@ def __init__( # noqa: WPS231
198199
199200 @classmethod
200201 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 ]]:
203208 """Yield items and their URLs from an inventory file streamed from `in_file`.
204209
205210 This implements mkdocstrings' `load_inventory` "protocol" (see plugin.py).
@@ -216,10 +221,10 @@ def load_inventory(
216221 if base_url is None :
217222 base_url = posixpath .dirname (url )
218223
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 ():
220225 yield item .name , posixpath .join (base_url , item .uri )
221226
222- def collect (self , identifier : str , config : dict ) -> CollectorItem : # noqa: WPS231
227+ def collect (self , identifier : str , config : dict ) -> CollectorItem :
223228 """Collect the documentation tree given an identifier and selection options.
224229
225230 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
338343
339344
340345def 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 ,
344349 config_file_path : str | None = None ,
345350 paths : list [str ] | None = None ,
346351 ** config : Any ,
0 commit comments