Skip to content

Commit f6cf570

Browse files
authored
fix: Do minimum work when falling back to re-collecting an object to get its anchor
This change introduces a new attribute on collectors: `fallback_config`. It is used by the `Handlers.get_anchor` method when collecting an object to find its anchor. It allows to pass specific options to the collector to avoid collecting the object members or, for example, parsing Python docstrings, as it is not useful, since we only care about the anchor. Issue #329: #329 PR #330: #330
1 parent 1353e4d commit f6cf570

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ dependencies = [
2424
"mkdocs~=1.2",
2525
"mkdocs-autorefs>=0.1,<0.4",
2626
"pymdown-extensions>=6.3,<10.0",
27-
"pytkdocs>=0.2.0,<0.13.0",
27+
"pytkdocs~=0.14.0",
2828
]
2929

3030
[project.urls]

src/mkdocstrings/handlers/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,9 @@ def get_anchor(self, identifier: str) -> Optional[str]:
339339
A string - anchor without '#', or None if there isn't any identifier familiar with it.
340340
"""
341341
for handler in self._handlers.values():
342+
fallback_config = getattr(handler.collector, "fallback_config", {})
342343
try:
343-
anchor = handler.renderer.get_anchor(handler.collector.collect(identifier, {}))
344+
anchor = handler.renderer.get_anchor(handler.collector.collect(identifier, fallback_config))
344345
except CollectionError:
345346
continue
346347
if anchor is not None:

src/mkdocstrings/handlers/python.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ class PythonCollector(BaseCollector):
141141
Obviously one could use a single filter instead: `"!^_[^_]"`, which is the default.
142142
"""
143143

144+
fallback_config = {"docstring_style": "markdown", "filters": ["!.*"]}
145+
"""The configuration used when falling back to re-collecting an object to get its anchor.
146+
147+
This configuration is used in [`Handlers.get_anchor`][mkdocstrings.handlers.base.Handlers.get_anchor].
148+
149+
When trying to fix (optional) cross-references, the autorefs plugin will try to collect
150+
an object with every configured handler until one succeeds. It will then try to get
151+
an anchor for it. It's because objects can have multiple identifiers (aliases),
152+
for example their definition path and multiple import paths in Python.
153+
154+
When re-collecting the object, we have no use for its members, or for its docstring being parsed.
155+
This is why the fallback configuration filters every member out, and uses the Markdown style,
156+
which we know will not generate any warnings.
157+
"""
158+
144159
def __init__(self, setup_commands: Optional[List[str]] = None) -> None:
145160
"""Initialize the object.
146161

0 commit comments

Comments
 (0)