diff --git a/CHANGELOG.md b/CHANGELOG.md index 260649c..819bb2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning][]. ### Fixed - header shadow now only extends downward (#70) +- the autosummary class template now correctly handles classes referenced through aliased modules (#71) ## [0.2.3] diff --git a/src/scverse_doc/autodoc/templates/autosummary/class.rst b/src/scverse_doc/autodoc/templates/autosummary/class.rst index bbd9fff..2dac963 100644 --- a/src/scverse_doc/autodoc/templates/autosummary/class.rst +++ b/src/scverse_doc/autodoc/templates/autosummary/class.rst @@ -7,6 +7,9 @@ .. autoclass:: {{ fullname }} {% set methods = methods | select("ne", "__init__") | list %} +{#- Inside autoclass, names resolve against the class’s real module, so prefix the class name: + `objname` can go through a module alias, and bare members break autosummary’s instance attribute fallback. #} +{% set clsname = fullname.split(".") | last %} {% block attributes %} {% for item in attributes %} @@ -16,7 +19,7 @@ .. autosummary:: {% endif %} - ~{{ [objname, item] | join(".") }} + ~{{ [clsname, item] | join(".") }} {%- endfor %} {% endblock %} @@ -28,7 +31,7 @@ .. autosummary:: {% endif %} - ~{{ [objname, item] | join(".") }} + ~{{ [clsname, item] | join(".") }} {% endfor %} {% endblock %} diff --git a/tests/roots/autodoc/apipkg/__init__.py b/tests/roots/autodoc/apipkg/__init__.py index c9c700b..7e2cfa0 100644 --- a/tests/roots/autodoc/apipkg/__init__.py +++ b/tests/roots/autodoc/apipkg/__init__.py @@ -1,2 +1,4 @@ # SPDX-License-Identifier: MPL-2.0 """A package to document recursively.""" + +from ._core import aliased # noqa: F401 diff --git a/tests/roots/autodoc/apipkg/_core/__init__.py b/tests/roots/autodoc/apipkg/_core/__init__.py new file mode 100644 index 0000000..4f80ae4 --- /dev/null +++ b/tests/roots/autodoc/apipkg/_core/__init__.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: MPL-2.0 +"""Private implementation, re-exported under public names.""" diff --git a/tests/roots/autodoc/apipkg/_core/aliased.py b/tests/roots/autodoc/apipkg/_core/aliased.py new file mode 100644 index 0000000..830abac --- /dev/null +++ b/tests/roots/autodoc/apipkg/_core/aliased.py @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: MPL-2.0 +"""A module only reachable through an alias, i.e. not importable as `apipkg.aliased`.""" + + +class Aliased: + """A class in an aliased module.""" + + attr: int = 1 + """An attribute.""" + + def meth(self) -> None: + """A method.""" diff --git a/tests/roots/autodoc/index.rst b/tests/roots/autodoc/index.rst index c3ae512..15df479 100644 --- a/tests/roots/autodoc/index.rst +++ b/tests/roots/autodoc/index.rst @@ -7,6 +7,14 @@ API apipkg +.. currentmodule:: apipkg + +.. autosummary:: + :toctree: generated + :recursive: + + aliased + .. currentmodule:: apimod .. autosummary:: diff --git a/tests/test_build.py b/tests/test_build.py index 2e9d792..0e3a8b7 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -173,6 +173,12 @@ def test_class_template_documents_members_by_kind(autodoc: Path) -> None: assert 'href="#apimod.Thing.ivar"' in html +def test_class_template_handles_aliased_modules(autodoc: Path) -> None: + html = (autodoc / "generated" / "apipkg.aliased.Aliased.html").read_text() + assert 'href="#apipkg.aliased.Aliased.attr"' in html + assert 'href="#apipkg.aliased.Aliased.meth"' in html + + def test_module_template_recurses(autodoc: Path) -> None: assert (autodoc / "generated" / "apipkg.sub.deep.html").is_file()