Skip to content

Commit

Permalink
fix: Always resolve secondary URLs to closest (don't log warnings)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pawamoy committed Feb 11, 2025
1 parent dcc7868 commit 243ad35
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/mkdocs_autorefs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _get_item_url(
raise

if len(urls) > 1:
if self.config.resolve_closest and from_url is not None:
if (self.config.resolve_closest or qualifier == "secondary") and from_url is not None:
return self._get_closest_url(from_url, urls, qualifier)
log.warning(
"Multiple %s URLs found for '%s': %s. "
Expand Down
20 changes: 12 additions & 8 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,30 @@ def test_register_secondary_url() -> None:
assert plugin._secondary_url_map == {"foo": ["foo.html#foo"]}


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


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

0 comments on commit 243ad35

Please sign in to comment.