Skip to content

Commit 7f5a4f3

Browse files
committed
Create Documenter objects after importing objects
1 parent 3e9dc0f commit 7f5a4f3

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
lines changed

sphinx/ext/autodoc/_member_finder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,10 @@ def _gather_members(
233233
if not obj_type:
234234
# don't know how to document this member
235235
continue
236-
doccls = registry.documenters[obj_type]
237236
# give explicitly separated module name, so that members
238237
# of inner classes can be documented
239238
dotted_parts = '.'.join((*props.parts, member_name))
240239
full_name = f'{props.module_name}::{dotted_parts}'
241-
documenter = doccls(directive, full_name, indent)
242240

243241
# We now try to import all objects before ordering them. This is to
244242
# avoid possible circular imports if we were to import objects after
@@ -253,10 +251,13 @@ def _gather_members(
253251
env=env,
254252
events=events,
255253
get_attr=get_attr,
256-
options=documenter.options,
254+
options=directive.genopt,
257255
)
258256
if member_props is None:
259257
continue
258+
259+
doccls = registry.documenters[obj_type]
260+
documenter = doccls(directive, full_name, indent)
260261
documenter.props = member_props
261262

262263
member_documenters.append((documenter, is_attr))

sphinx/ext/autodoc/directive.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ def run(self) -> list[Node]:
142142

143143
# generate the output
144144
get_attr = _AutodocAttrGetter(self.env._registry.autodoc_attrgetters)
145-
params = DocumenterBridge(
146-
self.env, reporter, documenter_options, lineno, self.state, get_attr
147-
)
148-
documenter = doccls(params, self.arguments[0])
149145
props = _load_object_by_name(
150146
name=self.arguments[0],
151147
objtype=objtype, # type: ignore[arg-type]
@@ -156,11 +152,17 @@ def run(self) -> list[Node]:
156152
env=self.env,
157153
events=self.env.events,
158154
get_attr=get_attr,
159-
options=documenter.options,
155+
options=documenter_options,
156+
)
157+
if props is None:
158+
return []
159+
160+
params = DocumenterBridge(
161+
self.env, reporter, documenter_options, lineno, self.state, get_attr
160162
)
161-
if props is not None:
162-
documenter.props = props
163-
documenter._generate(more_content=self.content)
163+
documenter = doccls(params, self.arguments[0])
164+
documenter.props = props
165+
documenter._generate(more_content=self.content)
164166
if not params.result:
165167
return []
166168

sphinx/ext/autosummary/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
369369

370370
result = StringList() # initialize for each documenter
371371
obj_type = _get_documenter(obj, parent)
372-
doccls = env._registry.documenters[obj_type]
373372
if isinstance(obj, ModuleType):
374373
full_name = real_name
375374
else:
@@ -379,7 +378,6 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
379378
# NB. using full_name here is important, since Documenters
380379
# handle module prefixes slightly differently
381380
self.bridge.result = result
382-
documenter = doccls(self.bridge, full_name)
383381
props = _load_object_by_name(
384382
name=full_name,
385383
objtype=obj_type,
@@ -390,7 +388,7 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
390388
env=env,
391389
events=events,
392390
get_attr=get_attr,
393-
options=documenter.options,
391+
options=self.bridge.genopt,
394392
)
395393
if props is None:
396394
logger.warning(
@@ -400,7 +398,6 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
400398
)
401399
items.append((display_name, '', '', real_name))
402400
continue
403-
documenter.props = props
404401

405402
# try to also get a source code analyzer for attribute docs
406403
real_module = props._obj___module__ or props.module_name
@@ -413,7 +410,6 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
413410
logger.debug('[autodoc] module analyzer failed: %s', err)
414411
# no source file -- e.g. for builtin and C modules
415412
analyzer = None
416-
documenter.analyzer = analyzer
417413

418414
# -- Grab the signature
419415

@@ -429,6 +425,10 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
429425

430426
# -- Grab the summary
431427

428+
doccls = env._registry.documenters[obj_type]
429+
documenter = doccls(self.bridge, full_name)
430+
documenter.props = props
431+
documenter.analyzer = analyzer
432432
documenter.add_content(None, indent=documenter.indent)
433433
lines = result.data[:]
434434
if props.obj_type != 'module':

tests/test_ext_autodoc/autodoc_util.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import TYPE_CHECKING
44
from unittest.mock import Mock
55

6+
from docutils.statemachine import StringList
7+
68
from sphinx.ext.autodoc._directive_options import (
79
_AutoDocumenterOptions,
810
_process_documenter_options,
@@ -17,8 +19,6 @@
1719
if TYPE_CHECKING:
1820
from typing import Any
1921

20-
from docutils.statemachine import StringList
21-
2222
from sphinx.application import Sphinx
2323
from sphinx.ext.autodoc._property_types import _AutodocObjType
2424

@@ -38,12 +38,7 @@ def do_autodoc(
3838
default_options=app.config.autodoc_default_options,
3939
options=options,
4040
)
41-
docoptions = _AutoDocumenterOptions.from_directive_options(opts)
42-
state = Mock()
43-
bridge = DocumenterBridge(
44-
app.env, LoggingReporter(''), docoptions, 1, state, safe_getattr
45-
)
46-
documenter = doccls(bridge, name)
41+
doc_options = _AutoDocumenterOptions.from_directive_options(opts)
4742
props = _load_object_by_name(
4843
name=name,
4944
objtype=obj_type,
@@ -54,9 +49,16 @@ def do_autodoc(
5449
env=app.env,
5550
events=app.events,
5651
get_attr=safe_getattr,
57-
options=documenter.options,
52+
options=doc_options,
5853
)
54+
result = StringList()
5955
if props is not None:
56+
state = Mock()
57+
bridge = DocumenterBridge(
58+
app.env, LoggingReporter(''), doc_options, 1, state, safe_getattr
59+
)
60+
bridge.result = result
61+
documenter = doccls(bridge, name)
6062
documenter.props = props
6163
documenter._generate()
62-
return bridge.result
64+
return result

tests/test_ext_autodoc/test_ext_autodoc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,6 @@ def _special_getattr(obj, attr_name, *defargs):
598598
def _assert_getter_works(app, directive, objtype, name, *attrs):
599599
getattr_spy.clear()
600600

601-
doccls = app.registry.documenters[objtype]
602-
documenter = doccls(directive, name)
603601
props = _load_object_by_name(
604602
name=name,
605603
objtype=objtype,
@@ -610,9 +608,11 @@ def _assert_getter_works(app, directive, objtype, name, *attrs):
610608
env=app.env,
611609
events=app.events,
612610
get_attr=directive.get_attr,
613-
options=documenter.options,
611+
options=directive.genopt,
614612
)
615613
if props is not None:
614+
doccls = app.registry.documenters[objtype]
615+
documenter = doccls(directive, name)
616616
documenter.props = props
617617
documenter._generate()
618618

0 commit comments

Comments
 (0)