Skip to content

Commit 88ab0d9

Browse files
committed
feat: allow option to be configurable
1 parent 941d0e5 commit 88ab0d9

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/mkdocstrings_handlers/python/handler.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class PythonHandler(BaseHandler):
116116
"annotations_path": "brief",
117117
"preload_modules": None,
118118
"allow_inspection": True,
119+
"force_inspection": False,
119120
"summary": False,
120121
"show_labels": True,
121122
"unwrap_annotated": False,
@@ -127,6 +128,7 @@ class PythonHandler(BaseHandler):
127128
Attributes: General options:
128129
find_stubs_package (bool): Whether to load stubs package (package-stubs) when extracting docstrings. Default `False`.
129130
allow_inspection (bool): Whether to allow inspecting modules when visiting them is not possible. Default: `True`.
131+
force_inspection (bool): Whether to force using dynamic analysis when loading data. Default: `False`.
130132
show_bases (bool): Show the base classes of a class. Default: `True`.
131133
show_inheritance_diagram (bool): Show the inheritance diagram of a class using Mermaid. Default: `False`.
132134
show_source (bool): Show the source code of this object. Default: `True`.
@@ -242,7 +244,11 @@ def __init__(
242244
paths = paths or []
243245

244246
# Expand paths with glob patterns.
245-
glob_base_dir = os.path.dirname(os.path.abspath(config_file_path)) if config_file_path else "."
247+
glob_base_dir = (
248+
os.path.dirname(os.path.abspath(config_file_path))
249+
if config_file_path
250+
else "."
251+
)
246252
with chdir(glob_base_dir):
247253
resolved_globs = [glob.glob(path) for path in paths]
248254
paths = [path for glob_list in resolved_globs for path in glob_list]
@@ -257,7 +263,9 @@ def __init__(
257263
for path in reversed(paths):
258264
# If it's not absolute, make path relative to the config file path, then make it absolute.
259265
if not os.path.isabs(path) and config_file_path:
260-
path = os.path.abspath(os.path.join(os.path.dirname(config_file_path), path)) # noqa: PLW2901
266+
path = os.path.abspath(
267+
os.path.join(os.path.dirname(config_file_path), path),
268+
)
261269
# Don't add duplicates.
262270
if path not in search_paths:
263271
search_paths.insert(0, path)
@@ -297,7 +305,11 @@ def load_inventory(
297305
for item in Inventory.parse_sphinx(in_file, domain_filter=domains).values():
298306
yield item.name, posixpath.join(base_url, item.uri)
299307

300-
def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem: # noqa: D102
308+
def collect(
309+
self,
310+
identifier: str,
311+
config: Mapping[str, Any],
312+
) -> CollectorItem:
301313
module_name = identifier.split(".", 1)[0]
302314
unknown_module = module_name not in self._modules_collection
303315
if config.get("fallback", False) and unknown_module:
@@ -309,7 +321,9 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
309321
parser = parser_name and Parser(parser_name)
310322

311323
if unknown_module:
312-
extensions = self.normalize_extension_paths(final_config.get("extensions", []))
324+
extensions = self.normalize_extension_paths(
325+
final_config.get("extensions", []),
326+
)
313327
loader = GriffeLoader(
314328
extensions=load_extensions(*extensions),
315329
search_paths=self._paths,
@@ -318,6 +332,7 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
318332
modules_collection=self._modules_collection,
319333
lines_collection=self._lines_collection,
320334
allow_inspection=final_config["allow_inspection"],
335+
force_inspection=final_config["force_inspection"],
321336
)
322337
try:
323338
for pre_loaded_module in final_config.get("preload_modules") or []:
@@ -339,7 +354,9 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
339354
external=self._load_external_modules,
340355
)
341356
if unresolved:
342-
logger.debug(f"{len(unresolved)} aliases were still unresolved after {iterations} iterations")
357+
logger.debug(
358+
f"{len(unresolved)} aliases were still unresolved after {iterations} iterations",
359+
)
343360
logger.debug(f"Unresolved aliases: {', '.join(sorted(unresolved))}")
344361

345362
try:
@@ -357,7 +374,11 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
357374

358375
return doc_object
359376

360-
def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str: # noqa: D102 (ignore missing docstring)
377+
def render(
378+
self,
379+
data: CollectorItem,
380+
config: Mapping[str, Any],
381+
) -> str:
361382
final_config = ChainMap(config, self.default_config) # type: ignore[arg-type]
362383

363384
template_name = rendering.do_get_template(self.env, data)
@@ -368,7 +389,9 @@ def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str: # noqa
368389
# than as an item in a dictionary.
369390
heading_level = final_config["heading_level"]
370391
try:
371-
final_config["members_order"] = rendering.Order(final_config["members_order"])
392+
final_config["members_order"] = rendering.Order(
393+
final_config["members_order"],
394+
)
372395
except ValueError as error:
373396
choices = "', '".join(item.value for item in rendering.Order)
374397
raise PluginError(
@@ -377,7 +400,8 @@ def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str: # noqa
377400

378401
if final_config["filters"]:
379402
final_config["filters"] = [
380-
(re.compile(filtr.lstrip("!")), filtr.startswith("!")) for filtr in final_config["filters"]
403+
(re.compile(filtr.lstrip("!")), filtr.startswith("!"))
404+
for filtr in final_config["filters"]
381405
]
382406

383407
summary = final_config["summary"]
@@ -439,9 +463,14 @@ def update_env(self, md: Markdown, config: dict) -> None:
439463
self.env.filters["as_classes_section"] = rendering.do_as_classes_section
440464
self.env.filters["as_modules_section"] = rendering.do_as_modules_section
441465
self.env.globals["AutorefsHook"] = rendering.AutorefsHook
442-
self.env.tests["existing_template"] = lambda template_name: template_name in self.env.list_templates()
466+
self.env.tests["existing_template"] = (
467+
lambda template_name: template_name in self.env.list_templates()
468+
)
443469

444-
def get_anchors(self, data: CollectorItem) -> tuple[str, ...]: # noqa: D102 (ignore missing docstring)
470+
def get_anchors(
471+
self,
472+
data: CollectorItem,
473+
) -> tuple[str, ...]:
445474
anchors = [data.path]
446475
try:
447476
if data.canonical_path != data.path:

0 commit comments

Comments
 (0)