summaryrefslogtreecommitdiff
path: root/sphinx/ext/autodoc/__init__.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-11-10 23:21:51 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-11-10 23:21:51 +0900
commit1254b8011347b309af122dbe91a9439cc56f4381 (patch)
tree241b3f47ed71c3500330690118de5a7c7e55e2d4 /sphinx/ext/autodoc/__init__.py
parent7299d589c61d3558efb9777d03454f8e2a3dafd1 (diff)
parent229e11c488fc1fbd15b0a209782aa94dc6abdf58 (diff)
downloadsphinx-git-1254b8011347b309af122dbe91a9439cc56f4381.tar.gz
Merge branch '3.x'
Diffstat (limited to 'sphinx/ext/autodoc/__init__.py')
-rw-r--r--sphinx/ext/autodoc/__init__.py78
1 files changed, 40 insertions, 38 deletions
diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py
index 9a6417a2e..cf92b0137 100644
--- a/sphinx/ext/autodoc/__init__.py
+++ b/sphinx/ext/autodoc/__init__.py
@@ -323,6 +323,7 @@ class Documenter:
def __init__(self, directive: "DocumenterBridge", name: str, indent: str = '') -> None:
self.directive = directive
+ self.config = directive.env.config
self.env = directive.env # type: BuildEnvironment
self.options = directive.genopt
self.name = name
@@ -393,7 +394,7 @@ class Documenter:
modname = None
parents = []
- with mock(self.env.config.autodoc_mock_imports):
+ with mock(self.config.autodoc_mock_imports):
self.modname, self.objpath = self.resolve_name(modname, parents, path, base)
if not self.modname:
@@ -411,11 +412,11 @@ class Documenter:
Returns True if successful, False if an error occurred.
"""
- with mock(self.env.config.autodoc_mock_imports):
+ with mock(self.config.autodoc_mock_imports):
try:
ret = import_object(self.modname, self.objpath, self.objtype,
attrgetter=self.get_attr,
- warningiserror=self.env.config.autodoc_warningiserror)
+ warningiserror=self.config.autodoc_warningiserror)
self.module, self.parent, self.object_name, self.object = ret
return True
except ImportError as exc:
@@ -539,8 +540,7 @@ class Documenter:
warnings.warn("The 'ignore' argument to autodoc.%s.get_doc() is deprecated."
% self.__class__.__name__,
RemovedInSphinx50Warning, stacklevel=2)
- docstring = getdoc(self.object, self.get_attr,
- self.env.config.autodoc_inherit_docstrings,
+ docstring = getdoc(self.object, self.get_attr, self.config.autodoc_inherit_docstrings,
self.parent, self.object_name)
if docstring:
tab_width = self.directive.state.document.settings.tab_width
@@ -682,7 +682,7 @@ class Documenter:
else:
isattr = False
- doc = getdoc(member, self.get_attr, self.env.config.autodoc_inherit_docstrings,
+ doc = getdoc(member, self.get_attr, self.config.autodoc_inherit_docstrings,
self.parent, self.object_name)
if not isinstance(doc, str):
# Ignore non-string __doc__
@@ -810,7 +810,7 @@ class Documenter:
documenter = classes[-1](self.directive, full_mname, self.indent)
memberdocumenters.append((documenter, isattr))
- member_order = self.options.member_order or self.env.config.autodoc_member_order
+ member_order = self.options.member_order or self.config.autodoc_member_order
memberdocumenters = self.sort_members(memberdocumenters, member_order)
for documenter, isattr in memberdocumenters:
@@ -1194,7 +1194,7 @@ class DocstringSignatureMixin:
return super().get_doc(ignore) # type: ignore
def format_signature(self, **kwargs: Any) -> str:
- if self.args is None and self.env.config.autodoc_docstring_signature: # type: ignore
+ if self.args is None and self.config.autodoc_docstring_signature: # type: ignore
# only act if a signature is not explicitly given already, and if
# the feature is enabled
result = self._find_signature()
@@ -1213,7 +1213,7 @@ class DocstringStripSignatureMixin(DocstringSignatureMixin):
feature of stripping any function signature from the docstring.
"""
def format_signature(self, **kwargs: Any) -> str:
- if self.args is None and self.env.config.autodoc_docstring_signature: # type: ignore
+ if self.args is None and self.config.autodoc_docstring_signature: # type: ignore
# only act if a signature is not explicitly given already, and if
# the feature is enabled
result = self._find_signature()
@@ -1240,13 +1240,12 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ
(inspect.isroutine(member) and isinstance(parent, ModuleDocumenter)))
def format_args(self, **kwargs: Any) -> str:
- if self.env.config.autodoc_typehints in ('none', 'description'):
+ if self.config.autodoc_typehints in ('none', 'description'):
kwargs.setdefault('show_annotation', False)
try:
self.env.app.emit('autodoc-before-process-signature', self.object, False)
- sig = inspect.signature(self.object,
- type_aliases=self.env.config.autodoc_type_aliases)
+ sig = inspect.signature(self.object, type_aliases=self.config.autodoc_type_aliases)
args = stringify_signature(sig, **kwargs)
except TypeError as exc:
logger.warning(__("Failed to get a function signature for %s: %s"),
@@ -1255,7 +1254,7 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ
except ValueError:
args = ''
- if self.env.config.strip_signature_backslash:
+ if self.config.strip_signature_backslash:
# escape backslashes for reST
args = args.replace('\\', '\\\\')
return args
@@ -1274,7 +1273,7 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ
sigs = []
if (self.analyzer and
'.'.join(self.objpath) in self.analyzer.overloads and
- self.env.config.autodoc_typehints == 'signature'):
+ self.config.autodoc_typehints == 'signature'):
# Use signatures for overloaded functions instead of the implementation function.
overloaded = True
else:
@@ -1298,7 +1297,7 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ
__globals__ = safe_getattr(self.object, '__globals__', {})
for overload in self.analyzer.overloads.get('.'.join(self.objpath)):
overload = evaluate_signature(overload, __globals__,
- self.env.config.autodoc_type_aliases)
+ self.config.autodoc_type_aliases)
sig = stringify_signature(overload, **kwargs)
sigs.append(sig)
@@ -1308,7 +1307,7 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ
def annotate_to_first_argument(self, func: Callable, typ: Type) -> None:
"""Annotate type hint to the first argument of function if needed."""
try:
- sig = inspect.signature(func, type_aliases=self.env.config.autodoc_type_aliases)
+ sig = inspect.signature(func, type_aliases=self.config.autodoc_type_aliases)
except TypeError as exc:
logger.warning(__("Failed to get a function signature for %s: %s"),
self.fullname, exc)
@@ -1440,7 +1439,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
self.env.app.emit('autodoc-before-process-signature', call, True)
try:
sig = inspect.signature(call, bound_method=True,
- type_aliases=self.env.config.autodoc_type_aliases)
+ type_aliases=self.config.autodoc_type_aliases)
return type(self.object), '__call__', sig
except ValueError:
pass
@@ -1456,7 +1455,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
self.env.app.emit('autodoc-before-process-signature', new, True)
try:
sig = inspect.signature(new, bound_method=True,
- type_aliases=self.env.config.autodoc_type_aliases)
+ type_aliases=self.config.autodoc_type_aliases)
return self.object, '__new__', sig
except ValueError:
pass
@@ -1467,7 +1466,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
self.env.app.emit('autodoc-before-process-signature', init, True)
try:
sig = inspect.signature(init, bound_method=True,
- type_aliases=self.env.config.autodoc_type_aliases)
+ type_aliases=self.config.autodoc_type_aliases)
return self.object, '__init__', sig
except ValueError:
pass
@@ -1479,7 +1478,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
self.env.app.emit('autodoc-before-process-signature', self.object, False)
try:
sig = inspect.signature(self.object, bound_method=False,
- type_aliases=self.env.config.autodoc_type_aliases)
+ type_aliases=self.config.autodoc_type_aliases)
return None, None, sig
except ValueError:
pass
@@ -1489,7 +1488,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
return None, None, None
def format_args(self, **kwargs: Any) -> str:
- if self.env.config.autodoc_typehints in ('none', 'description'):
+ if self.config.autodoc_typehints in ('none', 'description'):
kwargs.setdefault('show_annotation', False)
try:
@@ -1513,13 +1512,13 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
sigs = []
overloads = self.get_overloaded_signatures()
- if overloads and self.env.config.autodoc_typehints == 'signature':
+ if overloads and self.config.autodoc_typehints == 'signature':
# Use signatures for overloaded methods instead of the implementation method.
method = safe_getattr(self._signature_class, self._signature_method_name, None)
__globals__ = safe_getattr(method, '__globals__', {})
for overload in overloads:
overload = evaluate_signature(overload, __globals__,
- self.env.config.autodoc_type_aliases)
+ self.config.autodoc_type_aliases)
parameters = list(overload.parameters.values())
overload = overload.replace(parameters=parameters[1:],
@@ -1540,6 +1539,9 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
qualname = '.'.join([cls.__qualname__, self._signature_method_name])
if qualname in analyzer.overloads:
return analyzer.overloads.get(qualname)
+ elif qualname in analyzer.tagorder:
+ # the constructor is defined in the class, but not overrided.
+ return []
except PycodeError:
pass
@@ -1575,7 +1577,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
if lines is not None:
return lines
- content = self.env.config.autoclass_content
+ content = self.config.autoclass_content
docstrings = []
attrdocstring = self.get_attr(self.object, '__doc__', None)
@@ -1587,7 +1589,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
if content in ('both', 'init'):
__init__ = self.get_attr(self.object, '__init__', None)
initdocstring = getdoc(__init__, self.get_attr,
- self.env.config.autodoc_inherit_docstrings,
+ self.config.autodoc_inherit_docstrings,
self.parent, self.object_name)
# for new-style classes, no __init__ means default __init__
if (initdocstring is not None and
@@ -1598,7 +1600,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
# try __new__
__new__ = self.get_attr(self.object, '__new__', None)
initdocstring = getdoc(__new__, self.get_attr,
- self.env.config.autodoc_inherit_docstrings,
+ self.config.autodoc_inherit_docstrings,
self.parent, self.object_name)
# for new-style classes, no __new__ means default __new__
if (initdocstring is not None and
@@ -1863,7 +1865,7 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
return ret
def format_args(self, **kwargs: Any) -> str:
- if self.env.config.autodoc_typehints in ('none', 'description'):
+ if self.config.autodoc_typehints in ('none', 'description'):
kwargs.setdefault('show_annotation', False)
try:
@@ -1877,11 +1879,11 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
if inspect.isstaticmethod(self.object, cls=self.parent, name=self.object_name):
self.env.app.emit('autodoc-before-process-signature', self.object, False)
sig = inspect.signature(self.object, bound_method=False,
- type_aliases=self.env.config.autodoc_type_aliases)
+ type_aliases=self.config.autodoc_type_aliases)
else:
self.env.app.emit('autodoc-before-process-signature', self.object, True)
sig = inspect.signature(self.object, bound_method=True,
- type_aliases=self.env.config.autodoc_type_aliases)
+ type_aliases=self.config.autodoc_type_aliases)
args = stringify_signature(sig, **kwargs)
except TypeError as exc:
logger.warning(__("Failed to get a method signature for %s: %s"),
@@ -1890,7 +1892,7 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
except ValueError:
args = ''
- if self.env.config.strip_signature_backslash:
+ if self.config.strip_signature_backslash:
# escape backslashes for reST
args = args.replace('\\', '\\\\')
return args
@@ -1918,7 +1920,7 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
sigs = []
if (self.analyzer and
'.'.join(self.objpath) in self.analyzer.overloads and
- self.env.config.autodoc_typehints == 'signature'):
+ self.config.autodoc_typehints == 'signature'):
# Use signatures for overloaded methods instead of the implementation method.
overloaded = True
else:
@@ -1944,7 +1946,7 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
__globals__ = safe_getattr(self.object, '__globals__', {})
for overload in self.analyzer.overloads.get('.'.join(self.objpath)):
overload = evaluate_signature(overload, __globals__,
- self.env.config.autodoc_type_aliases)
+ self.config.autodoc_type_aliases)
if not inspect.isstaticmethod(self.object, cls=self.parent,
name=self.object_name):
@@ -1958,7 +1960,7 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
def annotate_to_first_argument(self, func: Callable, typ: Type) -> None:
"""Annotate type hint to the first argument of function if needed."""
try:
- sig = inspect.signature(func, type_aliases=self.env.config.autodoc_type_aliases)
+ sig = inspect.signature(func, type_aliases=self.config.autodoc_type_aliases)
except TypeError as exc:
logger.warning(__("Failed to get a method signature for %s: %s"),
self.fullname, exc)
@@ -2110,11 +2112,11 @@ class AttributeDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter):
# Disable `autodoc_inherit_docstring` temporarily to avoid to obtain
# a docstring from the value which descriptor returns unexpectedly.
# ref: https://github.com/sphinx-doc/sphinx/issues/7805
- orig = self.env.config.autodoc_inherit_docstrings
- self.env.config.autodoc_inherit_docstrings = False # type: ignore
+ orig = self.config.autodoc_inherit_docstrings
+ self.config.autodoc_inherit_docstrings = False # type: ignore
return super().get_doc(ignore)
finally:
- self.env.config.autodoc_inherit_docstrings = orig # type: ignore
+ self.config.autodoc_inherit_docstrings = orig # type: ignore
def add_content(self, more_content: Any, no_docstring: bool = False) -> None:
if not self._datadescriptor:
@@ -2223,11 +2225,11 @@ class SlotsAttributeDocumenter(AttributeDocumenter):
self.objtype = 'attribute'
self._datadescriptor = True
- with mock(self.env.config.autodoc_mock_imports):
+ with mock(self.config.autodoc_mock_imports):
try:
ret = import_object(self.modname, self.objpath[:-1], 'class',
attrgetter=self.get_attr,
- warningiserror=self.env.config.autodoc_warningiserror)
+ warningiserror=self.config.autodoc_warningiserror)
self.module, _, _, self.parent = ret
return True
except ImportError as exc: