8000 feat: Allow pre-loading modules · FasterSpeeding/python@36002cb · GitHub
[go: up one dir, main page]

Skip to content

Commit 36002cb

Browse files
authored
feat: Allow pre-loading modules
Add option to preload modules. Preloading modules allows to render members of objects that originate from other packages than their parent. Direct members of modules must be listed in `__all__`. This option typically allows to render aliases, by collecting their parent module (package) which allows to resolve these aliases. Issue mkdocstrings/mkdocstrings#503: mkdocstrings/mkdocstrings#503 PR mkdocstrings#60: mkdocstrings#60
1 parent a6c55fb commit 36002cb

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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-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)
0