Skip to content

Commit 277bad0

Browse files
committed
Merge branch 'master' of github.com:mkdocstrings/python
2 parents f2eee9a + 02052e2 commit 277bad0

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

docs/schema.json

+14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
"format": "path"
5050
}
5151
},
52+
"load_external_modules": {
53+
"title": "Load external modules to resolve aliases.",
54+
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#global-only-options",
55+
"type": "boolean",
56+
"default": false
57+
},
5258
"options": {
5359
"title": "Options for collecting and rendering objects.",
5460
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#globallocal-options",
@@ -262,6 +268,14 @@
262268
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#globallocal-options",
263269
"enum": ["brief", "source"],
264270
"default": "brief"
271+
},
272+
"preload_modules": {
273+
"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).",
274+
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#globallocal-options",
275+
"type": "array",
276+
"items": {
277+
"type":"string"
278+
}
265279
}
266280
},
267281
"additionalProperties": false

docs/usage.md

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ Some options are **global only**, and go directly under the handler's name.
5252

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

55+
- `load_external_modules`:
56+
this option allows resolving aliases to any external module.
57+
Enabling this option will tell handler that when it encounters an import that is made public
58+
through the `__all__` variable, it will resolve it recursively to *any* module.
59+
**Use with caution:** this can load a *lot* of modules, slowing down your build
60+
or triggering errors that we do not yet handle.
61+
**We recommend using the `preload_modules` option instead**,
62+
which acts as an include-list rather than as include-all.
63+
5564
## Global/local options
5665

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

src/mkdocstrings_handlers/python/handler.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class PythonHandler(BaseHandler):
9999
"members": None,
100100
"filters": ["!^_[^_]"],
101101
"annotations_path": "brief",
102+
"preload_modules": None,
103+
"load_external_modules": False,
102104
}
103105
"""
104106
Attributes: Headings options:
@@ -150,6 +152,16 @@ class PythonHandler(BaseHandler):
150152
Attributes: Additional options:
151153
show_bases (bool): Show the base classes of a class. Default: `True`.
152154
show_source (bool): Show the source code of this object. Default: `True`.
155+
preload_modules (list[str] | None): Pre-load modules that are
156+
not specified directly in autodoc instructions (`::: identifier`).
157+
It is useful when you want to render documentation for a particular member of an object,
158+
and this member is imported from another package than its parent.
159+
160+
For an imported member to be rendered, you need to add it to the `__all__` attribute
161+
of the importing module.
162+
163+
The modules must be listed as an array of strings. Default: `None`.
164+
153165
""" # noqa: E501
154166

155167
def __init__(
@@ -235,12 +247,16 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
235247
modules_collection=self._modules_collection,
236248
lines_collection=self._lines_collection,
237249
)
238-
try:
250+
try: # noqa: WPS229 we expect one type of exception, and want to fail on the first one
251+
for pre_loaded_module in final_config.get("preload_modules") or []:
252+
if pre_loaded_module not in self._modules_collection:
253+
loader.load_module(pre_loaded_module)
239254
loader.load_module(module_name)
240255
except ImportError as error:
241256
raise CollectionError(str(error)) from error
242-
243-
unresolved, iterations = loader.resolve_aliases(implicit=False, external=False)
257+
unresolved, iterations = loader.resolve_aliases(
258+
implicit=False, external=final_config["load_external_modules"]
259+
)
244260
if unresolved:
245261
logger.debug(f"{len(unresolved)} aliases were still unresolved after {iterations} iterations")
246262
logger.debug(f"Unresolved aliases: {', '.join(sorted(unresolved))}")

0 commit comments

Comments
 (0)