Skip to content

Commit fe64e72

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 fe64e72

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

docs/schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@
262262
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#globallocal-options",
263263
"enum": ["brief", "source"],
264264
"default": "brief"
265+
},
266+
"preload_modules": {
267+
"title": "Pre-load modules. It permits to resolve aliases pointing to these modules (packages), and therefore render members of an object that are external to the given object (originating from another package).",
268+
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#globallocal-only-options",
269+
"type": "array",
270+
"items": {
271+
"type":"string"
272+
}
265273
}
266274
},
267275
"additionalProperties": false

src/mkdocstrings_handlers/python/handler.py

Lines changed: 15 additions & 1 deletion
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+
"preload_modules": None,
102103
}
103104
"""
104105
Attributes: Headings options:
@@ -150,6 +151,16 @@ class PythonHandler(BaseHandler):
150151
Attributes: Additional options:
151152
show_bases (bool): Show the base classes of a class. Default: `True`.
152153
show_source (bool): Show the source code of this object. Default: `True`.
154+
preload_modules (list[str] | None): Pre-load modules that are
155+
not specified directly in autodoc instructions (`::: identifier`).
156+
It is useful when you want to render documentation for a particular member of an object,
157+
and this member is imported from another package than its parent.
158+
159+
For an imported member to be rendered, you need to add it to the `__all__` attribute
160+
of the importing module.
161+
162+
The modules must be listed as an array of strings. Default: `None`.
163+
153164
""" # noqa: E501
154165

155166
def __init__(
@@ -235,7 +246,10 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
235246
modules_collection=self._modules_collection,
236247
lines_collection=self._lines_collection,
237248
)
238-
try:
249+
try: # noqa: WPS229 we expect one type of exception, and want to fail on the first one
250+
for pre_loaded_module in final_config.get("preload_modules") or []:
251+
if pre_loaded_module not in self._modules_collection:
252+
loader.load_module(pre_loaded_module)
239253
loader.load_module(module_name)
240254
except ImportError as error:
241255
raise CollectionError(str(error)) from error

0 commit comments

Comments
 (0)