Skip to content

Commit

Permalink
feat: Allow resolving alias to external modules
Browse files Browse the repository at this point in the history
PR #61: #61
Follow-up of PR #60: #60
Co-authored-by: gilfree <gilfree@users.noreply.github.com>
  • Loading branch information
gilfree authored Mar 7, 2023
1 parent 36002cb commit 02052e2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
"format": "path"
}
},
"load_external_modules": {
"title": "Load external modules to resolve aliases.",
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#global-only-options",
"type": "boolean",
"default": false
},
"options": {
"title": "Options for collecting and rendering objects.",
"markdownDescription": "https://mkdocstrings.github.io/python/usage/#globallocal-options",
Expand Down
9 changes: 9 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ Some options are **global only**, and go directly under the handler's name.

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

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

## 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 @@ -100,6 +100,7 @@ class PythonHandler(BaseHandler):
"filters": ["!^_[^_]"],
"annotations_path": "brief",
"preload_modules": None,
"load_external_modules": False,
}
"""
Attributes: Headings options:
Expand Down Expand Up @@ -253,8 +254,9 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
loader.load_module(module_name)
except ImportError as error:
raise CollectionError(str(error)) from error

unresolved, iterations = loader.resolve_aliases(implicit=False, external=False)
unresolved, iterations = loader.resolve_aliases(
implicit=False, external=final_config["load_external_modules"]
)
if unresolved:
logger.debug(f"{len(unresolved)} aliases were still unresolved after {iterations} iterations")
logger.debug(f"Unresolved aliases: {', '.join(sorted(unresolved))}")
Expand Down

0 comments on commit 02052e2

Please sign in to comment.