Skip to content

Commit a2bc632

Browse files
committed
feat: pre load external modules
Add option to preload external modules that are not part of the displayed documentation to allow resolving aliases to objects in these modules, and presenting their documentation in a module that imports them, and exports the imports with `__all__` attribute.
1 parent a6c55fb commit a2bc632

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

docs/schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949
"format": "path"
5050
}
5151
},
52+
"preload_external_modules": {
53+
"title": "Load external modules to resolve aliases.",
54+
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#global-only-options",
55+
"type": "array",
56+
"items": {
57+
"type":"string"
58+
}
59+
},
5260
"options": {
5361
"title": "Options for collecting and rendering objects.",
5462
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#globallocal-options",

docs/usage.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ Some options are **global only**, and go directly under the handler's name.
5252

5353
More details at [Finding modules](#finding-modules).
5454

55+
- `preload_external_modules`: this option is used to load external modules that are
56+
not specified directly, in order to resolve aliases. For example, if you define
57+
a class `C` in package `A` and import it in package `B`, you can explicitly export
58+
it by defining `__all__=['C']` in package B, and then the documentation for
59+
class `C` will appear under package `B`, even if `A` documentation is not part of
60+
the classes given to mkdocstrings to document. (i.e. even if `::: A` does not appear in
61+
the documentation). The modules should be listed as an array of strings, e.g. `['A', 'B']`.
62+
5563
## Global/local options
5664

5765
The other options can be used both globally *and* locally, under the `options` key.

src/mkdocstrings_handlers/python/handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class PythonHandler(BaseHandler):
9999
"members": None,
100100
"filters": ["!^_[^_]"],
101101
"annotations_path": "brief",
102+
"load_external_modules": False,
102103
}
103104
"""
104105
Attributes: Headings options:
@@ -235,11 +236,12 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
235236
modules_collection=self._modules_collection,
236237
lines_collection=self._lines_collection,
237238
)
238-
try:
239+
try: # noqa: WPS229 we expect one type of exception, and want to fail on the first one
240+
for pre_loaded_module in config.get("preload_external_modules", []):
241+
loader.load_module(pre_loaded_module)
239242
loader.load_module(module_name)
240243
except ImportError as error:
241244
raise CollectionError(str(error)) from error
242-
243245
unresolved, iterations = loader.resolve_aliases(implicit=False, external=False)
244246
if unresolved:
245247
logger.debug(f"{len(unresolved)} aliases were still unresolved after {iterations} iterations")

0 commit comments

Comments
 (0)