Skip to content

Commit 243ad35

Browse files
committed
fix: Always resolve secondary URLs to closest (don't log warnings)
Downstream projects rendering aliases of objects imported from upstream ones will render these upstream objects' docstrings. These docstrings can contain cross-references to other upstream objects that are not rendered directly in downstream project's docs. If downstream project renders subclasses of upstream class, with inherited members, only primary URLs will be registered for the aliased/downstream identifiers, and only secondary URLs will be registered for the upstream identifiers. When trying to apply the cross-reference for the upstream docstring, autorefs will find only secondary URLs, and multiple ones. But the end user does not have control over this. It means we shouldn't log warnings when multiple secondary URLs are found, and always resolve to closest. Issue-52: #52
1 parent dcc7868 commit 243ad35

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/mkdocs_autorefs/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def _get_item_url(
223223
raise
224224

225225
if len(urls) > 1:
226-
if self.config.resolve_closest and from_url is not None:
226+
if (self.config.resolve_closest or qualifier == "secondary") and from_url is not None:
227227
return self._get_closest_url(from_url, urls, qualifier)
228228
log.warning(
229229
"Multiple %s URLs found for '%s': %s. "

tests/test_plugin.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,30 @@ def test_register_secondary_url() -> None:
9494
assert plugin._secondary_url_map == {"foo": ["foo.html#foo"]}
9595

9696

97-
def test_warn_multiple_urls(caplog: pytest.LogCaptureFixture) -> None:
97+
@pytest.mark.parametrize("primary", [True, False])
98+
def test_warn_multiple_urls(caplog: pytest.LogCaptureFixture, primary: bool) -> None:
9899
"""Warn when multiple URLs are found for the same identifier."""
99100
plugin = AutorefsPlugin()
100101
plugin.config = AutorefsConfig()
101-
plugin.register_anchor(identifier="foo", page="foo.html", primary=True)
102-
plugin.register_anchor(identifier="foo", page="bar.html", primary=True)
102+
plugin.register_anchor(identifier="foo", page="foo.html", primary=primary)
103+
plugin.register_anchor(identifier="foo", page="bar.html", primary=primary)
103104
url_mapper = functools.partial(plugin.get_item_url, from_url="/hello")
104105
# YORE: Bump 2: Replace `, _legacy_refs=False` with `` within line.
105106
fix_refs('<autoref identifier="foo">Foo</autoref>', url_mapper, _legacy_refs=False)
106-
assert "Multiple primary URLs found for 'foo': ['foo.html#foo', 'bar.html#foo']" in caplog.text
107+
qualifier = "primary" if primary else "secondary"
108+
assert (f"Multiple {qualifier} URLs found for 'foo': ['foo.html#foo', 'bar.html#foo']" in caplog.text) is primary
107109

108110

109-
def test_use_closest_url(caplog: pytest.LogCaptureFixture) -> None:
111+
@pytest.mark.parametrize("primary", [True, False])
112+
def test_use_closest_url(caplog: pytest.LogCaptureFixture, primary: bool) -> None:
110113
"""Use the closest URL when multiple URLs are found for the same identifier."""
111114
plugin = AutorefsPlugin()
112115
plugin.config = AutorefsConfig()
113116
plugin.config.resolve_closest = True
114-
plugin.register_anchor(identifier="foo", page="foo.html", primary=True)
115-
plugin.register_anchor(identifier="foo", page="bar.html", primary=True)
117+
plugin.register_anchor(identifier="foo", page="foo.html", primary=primary)
118+
plugin.register_anchor(identifier="foo", page="bar.html", primary=primary)
116119
url_mapper = functools.partial(plugin.get_item_url, from_url="/hello")
117120
# YORE: Bump 2: Replace `, _legacy_refs=False` with `` within line.
118121
fix_refs('<autoref identifier="foo">Foo</autoref>', url_mapper, _legacy_refs=False)
119-
assert "Multiple primary URLs found for 'foo': ['foo.html#foo', 'bar.html#foo']" not in caplog.text
122+
qualifier = "primary" if primary else "secondary"
123+
assert f"Multiple {qualifier} URLs found for 'foo': ['foo.html#foo', 'bar.html#foo']" not in caplog.text

0 commit comments

Comments
 (0)