Skip to content

Commit 8661219

Browse files
committed
👌 Move the resolution of __all__ into the renderer
1 parent b889607 commit 8661219

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

docs/apidocs/autodoc2/autodoc2.render.base.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Classes
2323
API
2424
~~~
2525

26-
.. py:class:: RendererBase(db: autodoc2.db.Database, config: autodoc2.config.Config, warn: typing.Callable[[str, autodoc2.utils.WarningSubtypes], None] | None = None, resolved_all: dict[str, autodoc2.utils.ResolvedDict] | None = None, standalone: bool = True)
26+
.. py:class:: RendererBase(db: autodoc2.db.Database, config: autodoc2.config.Config, warn: typing.Callable[[str, autodoc2.utils.WarningSubtypes], None] | None = None, standalone: bool = True)
2727
:canonical: autodoc2.render.base.RendererBase
2828

2929
Bases: :py:obj:`abc.ABC`

docs/apidocs/autodoc2/autodoc2.render.myst_.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ API
4040

4141
.. autodoc2-docstring:: autodoc2.render.myst_._RE_DELIMS
4242

43-
.. py:class:: MystRenderer(db: autodoc2.db.Database, config: autodoc2.config.Config, warn: typing.Callable[[str, autodoc2.utils.WarningSubtypes], None] | None = None, resolved_all: dict[str, autodoc2.utils.ResolvedDict] | None = None, standalone: bool = True)
43+
.. py:class:: MystRenderer(db: autodoc2.db.Database, config: autodoc2.config.Config, warn: typing.Callable[[str, autodoc2.utils.WarningSubtypes], None] | None = None, standalone: bool = True)
4444
:canonical: autodoc2.render.myst_.MystRenderer
4545

4646
Bases: :py:obj:`autodoc2.render.base.RendererBase`

docs/apidocs/autodoc2/autodoc2.render.rst_.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ API
4040

4141
.. autodoc2-docstring:: autodoc2.render.rst_._RE_DELIMS
4242

43-
.. py:class:: RstRenderer(db: autodoc2.db.Database, config: autodoc2.config.Config, warn: typing.Callable[[str, autodoc2.utils.WarningSubtypes], None] | None = None, resolved_all: dict[str, autodoc2.utils.ResolvedDict] | None = None, standalone: bool = True)
43+
.. py:class:: RstRenderer(db: autodoc2.db.Database, config: autodoc2.config.Config, warn: typing.Callable[[str, autodoc2.utils.WarningSubtypes], None] | None = None, standalone: bool = True)
4444
:canonical: autodoc2.render.rst_.RstRenderer
4545

4646
Bases: :py:obj:`autodoc2.render.base.RendererBase`

src/autodoc2/render/base.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from collections import OrderedDict
88
import typing as t
99

10-
from autodoc2.utils import WarningSubtypes
10+
from autodoc2.utils import WarningSubtypes, resolve_all
1111

1212
if t.TYPE_CHECKING:
1313
from autodoc2.config import Config
@@ -26,7 +26,6 @@ def __init__(
2626
db: Database,
2727
config: Config,
2828
warn: t.Callable[[str, WarningSubtypes], None] | None = None,
29-
resolved_all: dict[str, ResolvedDict] | None = None,
3029
standalone: bool = True,
3130
) -> None:
3231
"""Initialise the renderer.
@@ -41,7 +40,7 @@ def __init__(
4140
self._config = config
4241
self._standalone = standalone
4342
self._warn = warn or (lambda msg, type_: None)
44-
self._resolved_all = resolved_all
43+
self._resolved_all: dict[str, ResolvedDict] = {}
4544
self._resolve_all_warned: set[str] = set()
4645
"""The full_names of modules that have already been warned about, regarding __all__ resolution"""
4746
self._is_hidden_cache: OrderedDict[str, bool] = OrderedDict()
@@ -93,6 +92,18 @@ def get_children(
9392
f"__all__ missing or empty in {item['full_name']}",
9493
WarningSubtypes.ALL_MISSING,
9594
)
95+
96+
if item["full_name"] not in self._resolved_all:
97+
# TODO perhaps here we should find the "highest ancestor" available in the db and use that?
98+
self._resolved_all.update(
99+
{
100+
k: v
101+
for k, v in resolve_all(self._db, item["full_name"]).items()
102+
if any(
103+
pat.fullmatch(k) for pat in self.config.module_all_regexes
104+
)
105+
}
106+
)
96107
resolved_data = (
97108
self._resolved_all.get(item["full_name"])
98109
if self._resolved_all

src/autodoc2/sphinx/extension.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from autodoc2.db import InMemoryDb, UniqueError
1919
from autodoc2.sphinx.docstring import DocstringRenderer
2020
from autodoc2.sphinx.logging_ import LOGGER, warn_sphinx
21-
from autodoc2.utils import ResolvedDict, WarningSubtypes, resolve_all, yield_modules
21+
from autodoc2.utils import WarningSubtypes, yield_modules
2222

2323
try:
2424
from sphinx.util.display import status_iterator
@@ -177,7 +177,6 @@ def run_autodoc_package(app: Sphinx, config: Config, pkg_index: int) -> str | No
177177
autodoc2_cache[path.as_posix()] = {"hash": hash_str, "db": db}
178178

179179
# find all the package/module, so we know what files to write
180-
# To following __all__ strategy
181180
LOGGER.info("[Autodoc2] Determining files to write ...")
182181
to_write: t.List[str] = []
183182
stack = [root_module]
@@ -192,15 +191,6 @@ def run_autodoc_package(app: Sphinx, config: Config, pkg_index: int) -> str | No
192191
]
193192
)
194193

195-
resolved_all: None | t.Dict[str, ResolvedDict] = None
196-
if config.module_all_regexes:
197-
LOGGER.info("[Autodoc2] Resolving __all__ imports ...")
198-
resolved_all = {
199-
k: v
200-
for k, v in resolve_all(db, root_module).items()
201-
if any(pat.fullmatch(k) for pat in config.module_all_regexes)
202-
}
203-
204194
def _warn_render(msg: str, type_: WarningSubtypes) -> None:
205195
warn_sphinx(msg, type_)
206196

@@ -219,9 +209,7 @@ def _warn_render(msg: str, type_: WarningSubtypes) -> None:
219209
if pat.fullmatch(mod_name):
220210
render_cls = cls
221211
break
222-
content = "\n".join(
223-
render_cls(db, config, _warn_render, resolved_all).render_item(mod_name)
224-
)
212+
content = "\n".join(render_cls(db, config, _warn_render).render_item(mod_name))
225213
out_path = output / (mod_name + render_cls.EXTENSION)
226214
paths.append(out_path)
227215
if not (out_path.exists() and out_path.read_text("utf8") == content):

src/autodoc2/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ class ResolvedDict(t.TypedDict):
138138

139139

140140
def resolve_all(db: Database, package_name: str) -> dict[str, ResolvedDict]:
141-
"""Give a module name, yield all names that would be imported by star."""
141+
"""Given a module name, yield all names that would be imported by star."""
142142
# see: https://docs.python.org/3/reference/simple_stmts.html#import
143143

144144
# gather all the packages and modules in the database,
145-
# that are, or begin with package_name
145+
# that are/begin with package_name
146146
modules = {
147147
i["full_name"]: i
148148
for i in itertools.chain(db.get_by_type("module"), db.get_by_type("package"))

0 commit comments

Comments
 (0)