Use attributes of `env rather than env.app`

This commit is contained in:
Adam Turner 2024-10-28 13:31:59 +00:00
parent 1e968be6ef
commit c3968e9be9
7 changed files with 27 additions and 20 deletions

View File

@ -267,7 +267,7 @@ class ObjectDescription(SphinxDirective, Generic[ObjDescT]):
finally:
# Private attributes for ToC generation. Will be modified or removed
# without notice.
if self.env.app.config.toc_object_entries:
if self.env.config.toc_object_entries:
signode['_toc_parts'] = self._object_hierarchy_parts(signode)
signode['_toc_name'] = self._toc_entry_name(signode)
else:
@ -288,7 +288,7 @@ class ObjectDescription(SphinxDirective, Generic[ObjDescT]):
content_node = addnodes.desc_content('', *content_children)
node.append(content_node)
self.transform_content(content_node)
self.env.app.emit(
self.env.events.emit(
'object-description-transform', self.domain, self.objtype, content_node
)
DocFieldTransformer(self).transform_all(content_node)

View File

@ -412,7 +412,7 @@ class Include(BaseInclude, SphinxDirective):
# Emit the "include-read" event
arg = [text]
self.env.app.events.emit('include-read', path, docname, arg)
self.env.events.emit('include-read', path, docname, arg)
text = arg[0]
# Split back into lines and reattach the two marker lines
@ -424,7 +424,7 @@ class Include(BaseInclude, SphinxDirective):
return StateMachine.insert_input(self.state_machine, include_lines, source)
# Only enable this patch if there are listeners for 'include-read'.
if self.env.app.events.listeners.get('include-read'):
if self.env.events.listeners.get('include-read'):
# See https://github.com/python/mypy/issues/2427 for details on the mypy issue
self.state_machine.insert_input = _insert_input

View File

@ -403,7 +403,7 @@ class CPPObject(ObjectDescription[ASTDeclaration]):
if not sig_node.get('_toc_parts'):
return ''
config = self.env.app.config
config = self.env.config
objtype = sig_node.parent.get('objtype')
if config.add_function_parentheses and objtype in {'function', 'method'}:
parens = '()'

View File

@ -230,7 +230,7 @@ class JSObject(ObjectDescription[tuple[str, str]]):
if not sig_node.get('_toc_parts'):
return ''
config = self.env.app.config
config = self.env.config
objtype = sig_node.parent.get('objtype')
if config.add_function_parentheses and objtype in {'function', 'method'}:
parens = '()'

View File

@ -411,7 +411,7 @@ class PyObject(ObjectDescription[tuple[str, str]]):
if not sig_node.get('_toc_parts'):
return ''
config = self.env.app.config
config = self.env.config
objtype = sig_node.parent.get('objtype')
if config.add_function_parentheses and objtype in {'function', 'method'}:
parens = '()'

View File

@ -75,7 +75,7 @@ class ReSTMarkup(ObjectDescription[str]):
if not sig_node.get('_toc_parts'):
return ''
config = self.env.app.config
config = self.env.config
objtype = sig_node.parent.get('objtype')
*parents, name = sig_node['_toc_parts']
if objtype == 'directive:option':

View File

@ -584,9 +584,14 @@ class Documenter:
for docstringlines in docstrings:
if self.env.app:
# let extensions preprocess docstrings
self.env.app.emit('autodoc-process-docstring',
self.objtype, self.fullname, self.object,
self.options, docstringlines)
self.env.events.emit(
'autodoc-process-docstring',
self.objtype,
self.fullname,
self.object,
self.options,
docstringlines,
)
if docstringlines and docstringlines[-1]:
# append a blank line to the end of the docstring
@ -793,7 +798,7 @@ class Documenter:
# should be skipped
if self.env.app:
# let extensions preprocess docstrings
skip_user = self.env.app.emit_firstresult(
skip_user = self.env.events.emit_firstresult(
'autodoc-skip-member', self.objtype, membername, member,
not keep, self.options)
if skip_user is not None:
@ -1325,7 +1330,7 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ
kwargs.setdefault('unqualified_typehints', True)
try:
self.env.app.emit('autodoc-before-process-signature', self.object, False)
self.env.events.emit('autodoc-before-process-signature', self.object, False)
sig = inspect.signature(self.object, type_aliases=self.config.autodoc_type_aliases)
args = stringify_signature(sig, **kwargs)
except TypeError as exc:
@ -1564,7 +1569,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
call = None
if call is not None:
self.env.app.emit('autodoc-before-process-signature', call, True)
self.env.events.emit('autodoc-before-process-signature', call, True)
try:
sig = inspect.signature(call, bound_method=True,
type_aliases=self.config.autodoc_type_aliases)
@ -1580,7 +1585,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
new = None
if new is not None:
self.env.app.emit('autodoc-before-process-signature', new, True)
self.env.events.emit('autodoc-before-process-signature', new, True)
try:
sig = inspect.signature(new, bound_method=True,
type_aliases=self.config.autodoc_type_aliases)
@ -1591,7 +1596,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
# Finally, we should have at least __init__ implemented
init = get_user_defined_function_or_method(self.object, '__init__')
if init is not None:
self.env.app.emit('autodoc-before-process-signature', init, True)
self.env.events.emit('autodoc-before-process-signature', init, True)
try:
sig = inspect.signature(init, bound_method=True,
type_aliases=self.config.autodoc_type_aliases)
@ -1603,7 +1608,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
# handle it.
# We don't know the exact method that inspect.signature will read
# the signature from, so just pass the object itself to our hook.
self.env.app.emit('autodoc-before-process-signature', self.object, False)
self.env.events.emit('autodoc-before-process-signature', self.object, False)
try:
sig = inspect.signature(self.object, bound_method=False,
type_aliases=self.config.autodoc_type_aliases)
@ -2198,11 +2203,13 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
args = '()'
else:
if inspect.isstaticmethod(self.object, cls=self.parent, name=self.object_name):
self.env.app.emit('autodoc-before-process-signature', self.object, False)
self.env.events.emit(
'autodoc-before-process-signature', self.object, False
)
sig = inspect.signature(self.object, bound_method=False,
type_aliases=self.config.autodoc_type_aliases)
else:
self.env.app.emit('autodoc-before-process-signature', self.object, True)
self.env.events.emit('autodoc-before-process-signature', self.object, True)
sig = inspect.signature(self.object, bound_method=True,
type_aliases=self.config.autodoc_type_aliases)
args = stringify_signature(sig, **kwargs)
@ -2785,7 +2792,7 @@ class PropertyDocumenter(DocstringStripSignatureMixin, # type: ignore[misc]
return ''
# update the annotations of the property getter
self.env.app.emit('autodoc-before-process-signature', func, False)
self.env.events.emit('autodoc-before-process-signature', func, False)
# correctly format the arguments for a property
return super().format_args(**kwargs)