Skip to content

Commit

Permalink
feat: pre load external modules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gilfree committed Feb 15, 2023
1 parent a6c55fb commit a2bc632
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
"format": "path"
}
},
"preload_external_modules": {
"title": "Load external modules to resolve aliases.",
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#global-only-options",
"type": "array",
"items": {
"type":"string"
}
},
"options": {
"title": "Options for collecting and rendering objects.",
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#globallocal-options",
Expand Down
8 changes: 8 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ Some options are **global only**, and go directly under the handler's name.

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

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

## Global/local options

The other options can be used both globally *and* locally, under the `options` key.
Expand Down
6 changes: 4 additions & 2 deletions src/mkdocstrings_handlers/python/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class PythonHandler(BaseHandler):
"members": None,
"filters": ["!^_[^_]"],
"annotations_path": "brief",
"load_external_modules": False,
}
"""
Attributes: Headings options:
Expand Down Expand Up @@ -235,11 +236,12 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
modules_collection=self._modules_collection,
lines_collection=self._lines_collection,
)
try:
try: # noqa: WPS229 we expect one type of exception, and want to fail on the first one
for pre_loaded_module in config.get("preload_external_modules", []):
loader.load_module(pre_loaded_module)
loader.load_module(module_name)
except ImportError as error:
raise CollectionError(str(error)) from error

unresolved, iterations = loader.resolve_aliases(implicit=False, external=False)
if unresolved:
logger.debug(f"{len(unresolved)} aliases were still unresolved after {iterations} iterations")
Expand Down

0 comments on commit a2bc632

Please sign in to comment.