Skip to content

Commit dcfaa98

Browse files
authored
Remove unused items in _documenters (#13963)
1 parent 2e8cedb commit dcfaa98

File tree

4 files changed

+17
-174
lines changed

4 files changed

+17
-174
lines changed

sphinx/ext/autodoc/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@
2626
from sphinx.ext.autodoc._documenters import (
2727
AttributeDocumenter,
2828
ClassDocumenter,
29-
ClassLevelDocumenter,
3029
DataDocumenter,
3130
DecoratorDocumenter,
32-
DocstringSignatureMixin,
3331
Documenter,
3432
ExceptionDocumenter,
3533
FunctionDocumenter,
3634
MethodDocumenter,
3735
ModuleDocumenter,
38-
ModuleLevelDocumenter,
3936
PropertyDocumenter,
4037
TypeAliasDocumenter,
4138
autodoc_attrgetter,
@@ -97,9 +94,6 @@
9794
'ObjectMember',
9895
'py_ext_sig_re',
9996
'special_member_re',
100-
'ModuleLevelDocumenter',
101-
'ClassLevelDocumenter',
102-
'DocstringSignatureMixin',
10397
'autodoc_attrgetter',
10498
'Documenter',
10599
)

sphinx/ext/autodoc/_documenters.py

Lines changed: 7 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from sphinx.ext.autodoc._member_finder import _filter_members, _get_members_to_document
2020
from sphinx.ext.autodoc._renderer import _add_content, _directive_header_lines
2121
from sphinx.ext.autodoc._sentinels import ALL
22-
from sphinx.ext.autodoc.importer import _load_object_by_name, _resolve_name
22+
from sphinx.ext.autodoc.importer import _load_object_by_name
2323
from sphinx.ext.autodoc.mock import ismock
2424
from sphinx.locale import _, __
2525
from sphinx.pycode import ModuleAnalyzer
@@ -82,9 +82,6 @@ class Documenter:
8282
#: true if the generated content may contain titles
8383
titles_allowed: ClassVar = True
8484

85-
__uninitialized_global_variable__: ClassVar[bool] = False
86-
"""If True, support uninitialized (type annotation only) global variables"""
87-
8885
option_spec: ClassVar[OptionSpec] = {
8986
'no-index': bool_option,
9087
'no-index-entry': bool_option,
@@ -106,15 +103,7 @@ def __init__(
106103
self.options: _AutoDocumenterOptions = directive.genopt
107104
self.name = name
108105
self.indent: Final = indent
109-
# the module and object path within the module, and the fully
110-
# qualified name (all set after resolve_name succeeds)
111-
self.modname: str = ''
112106
self.module: ModuleType | None = None
113-
self.objpath: list[str] = []
114-
self.fullname = ''
115-
# the object to document (set after import_object succeeds)
116-
self.object: Any = None
117-
self.object_name = ''
118107
# the parent/owner of the object to document
119108
self.parent: Any = None
120109
# the module analyzer to get at attribute docs, or None
@@ -132,11 +121,6 @@ def __init__(
132121
self.options.special_members += ['__new__', '__init__']
133122
self.options = self.options.merge_member_options()
134123

135-
@property
136-
def documenters(self) -> dict[str, type[Documenter]]:
137-
"""Returns registered Documenter classes"""
138-
return self.env._registry.documenters
139-
140124
def add_line(self, line: str, source: str, *lineno: int, indent: str) -> None:
141125
"""Append one line of generated reST to the output."""
142126
if line.strip(): # not a blank line
@@ -145,6 +129,10 @@ def add_line(self, line: str, source: str, *lineno: int, indent: str) -> None:
145129
self.directive.result.append('', source, *lineno)
146130

147131
def _load_object_by_name(self) -> Literal[True] | None:
132+
"""Import the object given by *self.name*.
133+
134+
Returns True if parsing and resolving was successful, otherwise None.
135+
"""
148136
if self._load_object_has_been_called:
149137
return True
150138

@@ -165,107 +153,11 @@ def _load_object_by_name(self) -> Literal[True] | None:
165153
props, module, parent = ret
166154

167155
self.props = props
168-
self.modname = props.module_name
169-
self.objpath = list(props.parts)
170-
self.fullname = props.full_name
171156
self.module = module
172157
self.parent = parent
173-
self.object_name = props.object_name
174-
self.object = props._obj
175158
self._load_object_has_been_called = True
176159
return True
177160

178-
def resolve_name(
179-
self, modname: str | None, parents: Any, path: str, base: str
180-
) -> tuple[str | None, list[str]]:
181-
"""Resolve the module and name of the object to document given by the
182-
arguments and the current module/class.
183-
184-
Must return a pair of the module name and a chain of attributes; for
185-
example, it would return ``('zipfile', ['ZipFile', 'open'])`` for the
186-
``zipfile.ZipFile.open`` method.
187-
"""
188-
ret = _resolve_name(
189-
objtype=self.objtype,
190-
module_name=modname,
191-
path=path,
192-
base=base,
193-
parents=parents,
194-
current_document=self._current_document,
195-
ref_context_py_module=self.env.ref_context.get('py:module'),
196-
ref_context_py_class=self.env.ref_context.get('py:class', ''),
197-
)
198-
if ret is not None:
199-
module_name, parts = ret
200-
return module_name, list(parts)
201-
202-
msg = 'must be implemented in subclasses'
203-
raise NotImplementedError(msg)
204-
205-
def parse_name(self) -> bool:
206-
"""Determine what module to import and what attribute to document.
207-
208-
Returns True and sets *self.modname*, *self.objpath*, *self.fullname*,
209-
*self.args* and *self.retann* if parsing and resolving was successful.
210-
"""
211-
return self._load_object_by_name() is not None
212-
213-
def import_object(self, raiseerror: bool = False) -> bool:
214-
"""Import the object given by *self.modname* and *self.objpath* and set
215-
it as *self.object*.
216-
217-
Returns True if successful, False if an error occurred.
218-
"""
219-
return self._load_object_by_name() is not None
220-
221-
def get_real_modname(self) -> str:
222-
"""Get the real module name of an object to document.
223-
224-
It can differ from the name of the module through which the object was
225-
imported.
226-
"""
227-
return self.props._obj___module__ or self.props.module_name
228-
229-
def check_module(self) -> bool:
230-
"""Check if *self.object* is really defined in the module given by
231-
*self.modname*.
232-
"""
233-
if self.options.imported_members:
234-
return True
235-
236-
subject = inspect.unpartial(self.props._obj)
237-
modname = self.get_attr(subject, '__module__', None)
238-
return not modname or modname == self.props.module_name
239-
240-
def format_args(self, **kwargs: Any) -> str:
241-
"""Format the argument signature of *self.object*.
242-
243-
Should return None if the object does not have a signature.
244-
"""
245-
return ''
246-
247-
def format_name(self) -> str:
248-
"""Format the name of *self.object*.
249-
250-
This normally should be something that can be parsed by the generated
251-
directive, but doesn't need to be (Sphinx will display it unparsed
252-
then).
253-
"""
254-
# normally the name doesn't contain the module (except for module
255-
# directives of course)
256-
return self.props.dotted_parts or self.props.module_name
257-
258-
def _call_format_args(self, **kwargs: Any) -> str:
259-
if kwargs:
260-
try:
261-
return self.format_args(**kwargs)
262-
except TypeError:
263-
# avoid chaining exceptions, by putting nothing here
264-
pass
265-
266-
# retry without arguments for old documenters
267-
return self.format_args()
268-
269161
def add_directive_header(self, *, indent: str) -> None:
270162
"""Add the directive header and options to the generated content."""
271163
domain_name = getattr(self, 'domain', 'py')
@@ -298,7 +190,7 @@ def get_sourcename(self) -> str:
298190
obj_module = inspect.safe_getattr(self.props._obj, '__module__', None)
299191
obj_qualname = inspect.safe_getattr(self.props._obj, '__qualname__', None)
300192
if obj_module and obj_qualname:
301-
# Get the correct location of docstring from self.object
193+
# Get the correct location of docstring from props._obj
302194
# to support inherited methods
303195
fullname = f'{self.props._obj.__module__}.{self.props._obj.__qualname__}'
304196
else:
@@ -801,22 +693,6 @@ class ClassDocumenter(Documenter):
801693
'noindex': bool_option,
802694
}
803695

804-
def get_canonical_fullname(self) -> str | None:
805-
__modname__ = safe_getattr(
806-
self.props._obj, '__module__', self.props.module_name
807-
)
808-
__qualname__ = safe_getattr(self.props._obj, '__qualname__', None)
809-
if __qualname__ is None:
810-
__qualname__ = safe_getattr(self.props._obj, '__name__', None)
811-
if __qualname__ and '<locals>' in __qualname__:
812-
# No valid qualname found if the object is defined as locals
813-
__qualname__ = None
814-
815-
if __modname__ and __qualname__:
816-
return f'{__modname__}.{__qualname__}'
817-
else:
818-
return None
819-
820696

821697
class ExceptionDocumenter(ClassDocumenter):
822698
"""Specialized ClassDocumenter subclass for exceptions."""
@@ -831,8 +707,6 @@ class DataDocumenter(Documenter):
831707

832708
props: _AssignStatementProperties
833709

834-
__uninitialized_global_variable__ = True
835-
836710
objtype = 'data'
837711
option_spec: ClassVar[OptionSpec] = dict(Documenter.option_spec)
838712
option_spec['annotation'] = annotation_option
@@ -858,12 +732,6 @@ class AttributeDocumenter(Documenter):
858732
option_spec['annotation'] = annotation_option
859733
option_spec['no-value'] = bool_option
860734

861-
@staticmethod
862-
def is_function_or_method(obj: Any) -> bool:
863-
return (
864-
inspect.isfunction(obj) or inspect.isbuiltin(obj) or inspect.ismethod(obj)
865-
)
866-
867735

868736
class PropertyDocumenter(Documenter):
869737
"""Specialized Documenter subclass for properties."""
@@ -887,18 +755,6 @@ class TypeAliasDocumenter(Documenter):
887755
}
888756

889757

890-
class DocstringSignatureMixin:
891-
"""Retained for compatibility."""
892-
893-
894-
class ModuleLevelDocumenter(Documenter):
895-
"""Retained for compatibility."""
896-
897-
898-
class ClassLevelDocumenter(Documenter):
899-
"""Retained for compatibility."""
900-
901-
902758
def autodoc_attrgetter(
903759
obj: Any, name: str, *defargs: Any, registry: SphinxComponentRegistry
904760
) -> Any:
@@ -924,7 +780,7 @@ def _document_members(
924780
for documenter, is_attr in member_documenters:
925781
assert documenter.props.module_name
926782
# We can directly call ._generate() since the documenters
927-
# already called parse_name() and import_object() before.
783+
# already called ``_load_object_by_name()`` before.
928784
#
929785
# Note that those two methods above do not emit events, so
930786
# whatever objects we deduced should not have changed.

sphinx/ext/autosummary/__init__.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,7 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
399399
documenter = self.create_documenter(
400400
obj, parent, full_name, registry=self.env._registry
401401
)
402-
if not documenter.parse_name():
403-
logger.warning(
404-
__('failed to parse name %s'),
405-
real_name,
406-
location=self.get_location(),
407-
)
408-
items.append((display_name, '', '', real_name))
409-
continue
410-
if not documenter.import_object():
402+
if documenter._load_object_by_name() is None:
411403
logger.warning(
412404
__('failed to import object %s'),
413405
real_name,
@@ -417,10 +409,11 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
417409
continue
418410

419411
# try to also get a source code analyzer for attribute docs
412+
real_module = (
413+
documenter.props._obj___module__ or documenter.props.module_name
414+
)
420415
try:
421-
documenter.analyzer = ModuleAnalyzer.for_module(
422-
documenter.get_real_modname()
423-
)
416+
documenter.analyzer = ModuleAnalyzer.for_module(real_module)
424417
# parse right now, to get PycodeErrors on parsing (results will
425418
# be cached anyway)
426419
documenter.analyzer.find_attr_docs()
@@ -438,10 +431,10 @@ def get_items(self, names: list[str]) -> list[tuple[str, str | None, str, str]]:
438431
signatures = tuple(
439432
f'{args} -> {retann}' if retann else str(args)
440433
for args, retann in _format_signatures(
441-
config=documenter.config,
442-
events=documenter._events,
434+
config=self.env.config,
435+
events=self.env.events,
443436
get_attr=documenter.get_attr,
444-
options=documenter.options,
437+
options=_AutoDocumenterOptions(),
445438
parent=documenter.parent,
446439
props=documenter.props,
447440
show_annotation=False,

tests/test_ext_autodoc/test_ext_autodoc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
inherited_members_option,
2121
)
2222
from sphinx.ext.autodoc._docstrings import _get_docstring_lines
23-
from sphinx.ext.autodoc._documenters import ModuleLevelDocumenter, autodoc_attrgetter
23+
from sphinx.ext.autodoc._documenters import Documenter, autodoc_attrgetter
2424
from sphinx.ext.autodoc._property_types import (
2525
_ClassDefProperties,
2626
_FunctionDefProperties,
@@ -527,7 +527,7 @@ def foo(self):
527527
assert get_docstring_lines('function', J().foo, config=config) == expected
528528

529529

530-
class _MyDocumenter(ModuleLevelDocumenter):
530+
class _MyDocumenter(Documenter):
531531
objtype = 'integer'
532532
directivetype = 'integer'
533533
priority = 100

0 commit comments

Comments
 (0)