diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-10-04 22:41:44 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-10-04 22:41:44 +0900 |
commit | 598b85da75bc533004d68b95a5056016dc16df1c (patch) | |
tree | e08eae0ea5f098257f01081e4c792bcfeb92eba6 /sphinx/ext/autodoc/__init__.py | |
parent | 11633f2e53bf01f6844256558444991836815690 (diff) | |
parent | 38bb3774643d779b708970f941f2b16d1ab81b89 (diff) | |
download | sphinx-git-598b85da75bc533004d68b95a5056016dc16df1c.tar.gz |
Merge branch '3.x' into master
Diffstat (limited to 'sphinx/ext/autodoc/__init__.py')
-rw-r--r-- | sphinx/ext/autodoc/__init__.py | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 9325c0f4c..983c43b24 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1198,7 +1198,8 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ try: self.env.app.emit('autodoc-before-process-signature', self.object, False) - sig = inspect.signature(self.object, follow_wrapped=True) + sig = inspect.signature(self.object, follow_wrapped=True, + type_aliases=self.env.config.autodoc_type_aliases) args = stringify_signature(sig, **kwargs) except TypeError as exc: logger.warning(__("Failed to get a function signature for %s: %s"), @@ -1247,7 +1248,9 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ if overloaded: __globals__ = safe_getattr(self.object, '__globals__', {}) for overload in self.analyzer.overloads.get('.'.join(self.objpath)): - overload = evaluate_signature(overload, __globals__) + overload = evaluate_signature(overload, __globals__, + self.env.config.autodoc_type_aliases) + sig = stringify_signature(overload, **kwargs) sigs.append(sig) @@ -1256,7 +1259,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) + sig = inspect.signature(func, type_aliases=self.env.config.autodoc_type_aliases) except TypeError as exc: logger.warning(__("Failed to get a function signature for %s: %s"), self.fullname, exc) @@ -1284,6 +1287,11 @@ class SingledispatchFunctionDocumenter(FunctionDocumenter): Retained for backwards compatibility, now does the same as the FunctionDocumenter """ + def __init__(self, *args: Any, **kwargs: Any) -> None: + warnings.warn("%s is deprecated." % self.__class__.__name__, + RemovedInSphinx50Warning, stacklevel=2) + super().__init__(*args, **kwargs) + class DecoratorDocumenter(FunctionDocumenter): """ @@ -1377,7 +1385,8 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type: if call is not None: self.env.app.emit('autodoc-before-process-signature', call, True) try: - sig = inspect.signature(call, bound_method=True) + sig = inspect.signature(call, bound_method=True, + type_aliases=self.env.config.autodoc_type_aliases) return type(self.object), '__call__', sig except ValueError: pass @@ -1392,7 +1401,8 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type: if new is not None: self.env.app.emit('autodoc-before-process-signature', new, True) try: - sig = inspect.signature(new, bound_method=True) + sig = inspect.signature(new, bound_method=True, + type_aliases=self.env.config.autodoc_type_aliases) return self.object, '__new__', sig except ValueError: pass @@ -1402,7 +1412,8 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type: if init is not None: self.env.app.emit('autodoc-before-process-signature', init, True) try: - sig = inspect.signature(init, bound_method=True) + sig = inspect.signature(init, bound_method=True, + type_aliases=self.env.config.autodoc_type_aliases) return self.object, '__init__', sig except ValueError: pass @@ -1413,7 +1424,8 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type: # the signature from, so just pass the object itself to our hook. self.env.app.emit('autodoc-before-process-signature', self.object, False) try: - sig = inspect.signature(self.object, bound_method=False) + sig = inspect.signature(self.object, bound_method=False, + type_aliases=self.env.config.autodoc_type_aliases) return None, None, sig except ValueError: pass @@ -1460,7 +1472,8 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type: method = safe_getattr(self._signature_class, self._signature_method_name, None) __globals__ = safe_getattr(method, '__globals__', {}) for overload in self.analyzer.overloads.get(qualname): - overload = evaluate_signature(overload, __globals__) + overload = evaluate_signature(overload, __globals__, + self.env.config.autodoc_type_aliases) parameters = list(overload.parameters.values()) overload = overload.replace(parameters=parameters[1:], @@ -1798,11 +1811,13 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type: else: 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) + sig = inspect.signature(self.object, bound_method=False, + type_aliases=self.env.config.autodoc_type_aliases) else: self.env.app.emit('autodoc-before-process-signature', self.object, True) sig = inspect.signature(self.object, bound_method=True, - follow_wrapped=True) + follow_wrapped=True, + type_aliases=self.env.config.autodoc_type_aliases) args = stringify_signature(sig, **kwargs) except TypeError as exc: logger.warning(__("Failed to get a method signature for %s: %s"), @@ -1862,7 +1877,9 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type: if overloaded: __globals__ = safe_getattr(self.object, '__globals__', {}) for overload in self.analyzer.overloads.get('.'.join(self.objpath)): - overload = evaluate_signature(overload, __globals__) + overload = evaluate_signature(overload, __globals__, + self.env.config.autodoc_type_aliases) + if not inspect.isstaticmethod(self.object, cls=self.parent, name=self.object_name): parameters = list(overload.parameters.values()) @@ -1875,7 +1892,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) + sig = inspect.signature(func, type_aliases=self.env.config.autodoc_type_aliases) except TypeError as exc: logger.warning(__("Failed to get a method signature for %s: %s"), self.fullname, exc) @@ -1902,6 +1919,11 @@ class SingledispatchMethodDocumenter(MethodDocumenter): Retained for backwards compatibility, now does the same as the MethodDocumenter """ + def __init__(self, *args: Any, **kwargs: Any) -> None: + warnings.warn("%s is deprecated." % self.__class__.__name__, + RemovedInSphinx50Warning, stacklevel=2) + super().__init__(*args, **kwargs) + class AttributeDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter): # type: ignore """ @@ -2212,6 +2234,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value('autodoc_mock_imports', [], True) app.add_config_value('autodoc_typehints', "signature", True, ENUM("signature", "description", "none")) + app.add_config_value('autodoc_type_aliases', {}, True) app.add_config_value('autodoc_warningiserror', True, True) app.add_config_value('autodoc_inherit_docstrings', True, True) app.add_event('autodoc-before-process-signature') |