From 266f41f2033e034060001bc2bed376b4f3a8d7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Mon, 11 Sep 2023 18:56:13 +0200 Subject: [PATCH] fix: Make `load_external_modules` a global-only option Issue #87: https://github.com/mkdocstrings/python/issues/87 --- src/mkdocstrings_handlers/python/handler.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/mkdocstrings_handlers/python/handler.py b/src/mkdocstrings_handlers/python/handler.py index 20515fd..637636b 100644 --- a/src/mkdocstrings_handlers/python/handler.py +++ b/src/mkdocstrings_handlers/python/handler.py @@ -108,7 +108,6 @@ class PythonHandler(BaseHandler): "filters": ["!^_[^_]"], "annotations_path": "brief", "preload_modules": None, - "load_external_modules": False, "allow_inspection": True, } """ @@ -189,6 +188,7 @@ def __init__( config_file_path: str | None = None, paths: list[str] | None = None, locale: str = "en", + load_external_modules: bool = False, **kwargs: Any, ) -> None: """Initialize the handler. @@ -198,10 +198,12 @@ def __init__( config_file_path: The MkDocs configuration file path. paths: A list of paths to use as Griffe search paths. locale: The locale to use when rendering content. + load_external_modules: Load external modules when resolving aliases. **kwargs: Same thing, but with keyword arguments. """ super().__init__(*args, **kwargs) self._config_file_path = config_file_path + self._load_external_modules = load_external_modules paths = paths or [] glob_base_dir = os.path.dirname(os.path.abspath(config_file_path)) if config_file_path else "." with chdir(glob_base_dir): @@ -282,7 +284,7 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem: raise CollectionError(str(error)) from error unresolved, iterations = loader.resolve_aliases( implicit=False, - external=final_config["load_external_modules"], + external=self._load_external_modules, ) if unresolved: logger.debug(f"{len(unresolved)} aliases were still unresolved after {iterations} iterations") @@ -372,11 +374,13 @@ def get_anchors(self, data: CollectorItem) -> tuple[str, ...]: # noqa: D102 (ig def get_handler( + *, theme: str, custom_templates: str | None = None, config_file_path: str | None = None, paths: list[str] | None = None, locale: str = "en", + load_external_modules: bool = False, **config: Any, # noqa: ARG001 ) -> PythonHandler: """Simply return an instance of `PythonHandler`. @@ -387,6 +391,7 @@ def get_handler( config_file_path: The MkDocs configuration file path. paths: A list of paths to use as Griffe search paths. locale: The locale to use when rendering content. + load_external_modules: Load external modules when resolving aliases. **config: Configuration passed to the handler. Returns: @@ -399,4 +404,5 @@ def get_handler( config_file_path=config_file_path, paths=paths, locale=locale, + load_external_modules=load_external_modules, )