Skip to content

Commit 8c86bad

Browse files
authored
Remove env from autodoc interfaces (#14018)
1 parent 08c7ee3 commit 8c86bad

File tree

7 files changed

+69
-38
lines changed

7 files changed

+69
-38
lines changed

sphinx/ext/autodoc/_generate.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
from sphinx.util.typing import restify, stringify_annotation
1717

1818
if TYPE_CHECKING:
19-
from collections.abc import Iterator
19+
from collections.abc import Iterator, Mapping, MutableSet
2020
from typing import Literal
2121

2222
from sphinx.config import Config
23-
from sphinx.environment import BuildEnvironment, _CurrentDocument
23+
from sphinx.environment import _CurrentDocument
2424
from sphinx.events import EventManager
2525
from sphinx.ext.autodoc._directive_options import _AutoDocumenterOptions
2626
from sphinx.ext.autodoc._property_types import _ItemProperties
@@ -38,13 +38,14 @@ def _generate_directives(
3838
*,
3939
config: Config,
4040
current_document: _CurrentDocument,
41-
env: BuildEnvironment,
4241
events: EventManager,
4342
get_attr: _AttrGetter,
4443
indent: str,
4544
options: _AutoDocumenterOptions,
4645
props: _ItemProperties,
4746
record_dependencies: set[str],
47+
ref_context: Mapping[str, str | None],
48+
reread_always: MutableSet[str],
4849
result: StringList,
4950
) -> None:
5051
"""Generate reST for the object given by *props*, and possibly for its members.
@@ -136,14 +137,15 @@ def _generate_directives(
136137
attr_docs=analyzer.attr_docs if analyzer is not None else {},
137138
config=config,
138139
current_document=current_document,
139-
env=env,
140140
events=events,
141141
get_attr=get_attr,
142142
indent=indent,
143143
options=options,
144144
props=props,
145145
real_modname=real_modname,
146146
record_dependencies=record_dependencies,
147+
ref_context=ref_context,
148+
reread_always=reread_always,
147149
result=result,
148150
)
149151

@@ -204,14 +206,15 @@ def _document_members(
204206
attr_docs: dict[tuple[str, str], list[str]],
205207
config: Config,
206208
current_document: _CurrentDocument,
207-
env: BuildEnvironment,
208209
events: EventManager,
209210
get_attr: _AttrGetter,
210211
indent: str,
211212
options: _AutoDocumenterOptions,
212213
props: _ItemProperties,
213214
real_modname: str,
214215
record_dependencies: set[str],
216+
ref_context: Mapping[str, str | None],
217+
reread_always: MutableSet[str],
215218
result: StringList,
216219
) -> None:
217220
"""Generate reST for member documentation.
@@ -233,12 +236,13 @@ def _document_members(
233236
attr_docs=attr_docs,
234237
config=config,
235238
current_document=current_document,
236-
env=env,
237239
events=events,
238240
get_attr=get_attr,
239241
options=options,
240242
parent_modname=real_modname,
241243
props=props,
244+
ref_context=ref_context,
245+
reread_always=reread_always,
242246
)
243247

244248
# for implicit module members, check __module__ to avoid
@@ -259,13 +263,14 @@ def _document_members(
259263
all_members=True,
260264
config=config,
261265
current_document=current_document,
262-
env=env,
263266
events=events,
264267
get_attr=get_attr,
265268
indent=member_indent,
266269
options=options,
267270
props=member_props,
268271
record_dependencies=record_dependencies,
272+
ref_context=ref_context,
273+
reread_always=reread_always,
269274
result=result,
270275
)
271276

sphinx/ext/autodoc/_member_finder.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
from sphinx.util.typing import AnyTypeAliasType
3030

3131
if TYPE_CHECKING:
32-
from collections.abc import Iterable, Iterator, Mapping, Sequence, Set
32+
from collections.abc import Iterable, Iterator, Mapping, MutableSet, Sequence, Set
3333
from typing import Any, Literal
3434

3535
from sphinx.config import Config
36-
from sphinx.environment import BuildEnvironment, _CurrentDocument
36+
from sphinx.environment import _CurrentDocument
3737
from sphinx.events import EventManager
3838
from sphinx.ext.autodoc._directive_options import _AutoDocumenterOptions
3939
from sphinx.ext.autodoc._property_types import _AutodocObjType, _ItemProperties
@@ -96,12 +96,13 @@ def _gather_members(
9696
attr_docs: dict[tuple[str, str], list[str]],
9797
config: Config,
9898
current_document: _CurrentDocument,
99-
env: BuildEnvironment,
10099
events: EventManager,
101100
get_attr: _AttrGetter,
102101
options: _AutoDocumenterOptions,
103102
parent_modname: str,
104103
props: _ItemProperties,
104+
ref_context: Mapping[str, str | None],
105+
reread_always: MutableSet[str],
105106
) -> list[tuple[_ItemProperties, bool, str]]:
106107
"""Generate reST for member documentation.
107108
@@ -179,11 +180,12 @@ def _gather_members(
179180
type_aliases=config.autodoc_type_aliases,
180181
current_document=current_document,
181182
config=config,
182-
env=env,
183183
events=events,
184184
get_attr=get_attr,
185185
options=options,
186186
parent_modname=parent_modname,
187+
ref_context=ref_context,
188+
reread_always=reread_always,
187189
)
188190
if member_props is None:
189191
continue

sphinx/ext/autodoc/directive.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ def run(self) -> list[Node]:
140140
config = env.config
141141
current_document = env.current_document
142142
events = env.events
143+
ref_context = env.ref_context
144+
reread_always = env.reread_always
143145

144146
props = _load_object_by_name(
145147
name=name,
@@ -148,10 +150,11 @@ def run(self) -> list[Node]:
148150
type_aliases=config.autodoc_type_aliases,
149151
current_document=current_document,
150152
config=config,
151-
env=env,
152153
events=events,
153154
get_attr=get_attr,
154155
options=documenter_options,
156+
ref_context=ref_context,
157+
reread_always=reread_always,
155158
)
156159
if props is None:
157160
return []
@@ -162,13 +165,14 @@ def run(self) -> list[Node]:
162165
more_content=self.content,
163166
config=config,
164167
current_document=current_document,
165-
env=env,
166168
events=events,
167169
get_attr=get_attr,
168170
indent='',
169171
options=documenter_options,
170172
props=props,
171173
record_dependencies=record_dependencies,
174+
ref_context=ref_context,
175+
reread_always=reread_always,
172176
result=result,
173177
)
174178
if not result:

sphinx/ext/autodoc/importer.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
from sphinx.util.typing import get_type_hints, restify, stringify_annotation
5252

5353
if TYPE_CHECKING:
54-
from collections.abc import Callable, Mapping, Sequence
54+
from collections.abc import Callable, Mapping, MutableSet, Sequence
5555
from importlib.machinery import ModuleSpec
5656
from typing import Any, Protocol, TypeAlias
5757

5858
from sphinx.config import Config
59-
from sphinx.environment import BuildEnvironment, _CurrentDocument
59+
from sphinx.environment import _CurrentDocument
6060
from sphinx.events import EventManager
6161
from sphinx.ext.autodoc._directive_options import _AutoDocumenterOptions
6262
from sphinx.ext.autodoc._property_types import _AutodocFuncProperty, _AutodocObjType
@@ -493,18 +493,19 @@ def _load_object_by_name(
493493
type_aliases: dict[str, Any] | None,
494494
current_document: _CurrentDocument,
495495
config: Config,
496-
env: BuildEnvironment,
497496
events: EventManager,
498497
get_attr: _AttrGetter,
499498
options: _AutoDocumenterOptions,
500499
parent_modname: str | None = None,
500+
ref_context: Mapping[str, str | None],
501+
reread_always: MutableSet[str],
501502
) -> _ItemProperties | None:
502503
"""Import and load the object given by *name*."""
503504
parsed = _parse_name(
504505
name=name,
505506
objtype=objtype,
506507
current_document=current_document,
507-
env=env,
508+
ref_context=ref_context,
508509
)
509510
if parsed is None:
510511
return None
@@ -538,7 +539,8 @@ def _load_object_by_name(
538539
im_ = None
539540
if im_ is None:
540541
logger.warning(exc.args[0], type='autodoc', subtype='import_object')
541-
env.note_reread()
542+
# See BuildEnvironment.note_reread()
543+
reread_always.add(current_document.docname)
542544
return None
543545
else:
544546
im = im_
@@ -939,7 +941,7 @@ def _parse_name(
939941
name: str,
940942
objtype: _AutodocObjType,
941943
current_document: _CurrentDocument,
942-
env: BuildEnvironment,
944+
ref_context: Mapping[str, str | None],
943945
) -> tuple[str, tuple[str, ...], str | None, str | None] | None:
944946
"""Parse *name* into module name, path, arguments, and return annotation."""
945947
# Parse the definition in *name*.
@@ -984,8 +986,8 @@ def _parse_name(
984986
base=base,
985987
parents=parents,
986988
current_document=current_document,
987-
ref_context_py_module=env.ref_context.get('py:module'),
988-
ref_context_py_class=env.ref_context.get('py:class', ''),
989+
ref_context_py_module=ref_context.get('py:module'),
990+
ref_context_py_class=ref_context.get('py:class', ''), # type: ignore[arg-type]
989991
)
990992
if resolved is None:
991993
msg = 'must be implemented in subclasses'

sphinx/ext/autosummary/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
307307
events = env.events
308308
get_attr = _AutodocAttrGetter(env._registry.autodoc_attrgetters)
309309
opts = _AutoDocumenterOptions()
310+
ref_context = env.ref_context
311+
reread_always = env.reread_always
310312

311313
max_item_chars = 50
312314

@@ -346,10 +348,11 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
346348
type_aliases=config.autodoc_type_aliases,
347349
current_document=current_document,
348350
config=config,
349-
env=env,
350351
events=events,
351352
get_attr=get_attr,
352353
options=opts,
354+
ref_context=ref_context,
355+
reread_always=reread_always,
353356
)
354357
if props is None:
355358
logger.warning(

tests/test_ext_autodoc/autodoc_util.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,35 @@ def do_autodoc(
3535

3636
config = app.config
3737
current_document = app.env.current_document
38-
env = app.env
3938
events = app.events
39+
ref_context = app.env.ref_context
40+
reread_always: set[str] = set()
4041
props = _load_object_by_name(
4142
name=name,
4243
objtype=obj_type,
4344
mock_imports=config.autodoc_mock_imports,
4445
type_aliases=config.autodoc_type_aliases,
4546
current_document=current_document,
4647
config=config,
47-
env=env,
4848
events=events,
4949
get_attr=safe_getattr,
5050
options=doc_options,
51+
ref_context=ref_context,
52+
reread_always=reread_always,
5153
)
5254
result = StringList()
5355
if props is not None:
5456
_generate_directives(
5557
config=config,
5658
current_document=current_document,
57-
env=env,
5859
events=events,
5960
get_attr=safe_getattr,
6061
indent='',
6162
options=doc_options,
6263
props=props,
6364
record_dependencies=set(),
65+
ref_context=ref_context,
66+
reread_always=reread_always,
6467
result=result,
6568
)
6669
return result

0 commit comments

Comments
 (0)