From fc9b7affc5dc9491fc8d174ec09fe7613e392946 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sun, 24 Jul 2022 12:44:43 +0100 Subject: [PATCH] Add annotations --- sphinx/ext/autodoc/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index aba804efb82..676483d0313 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1269,7 +1269,11 @@ def format_args(self, **kwargs: Any) -> str: if self.config.autodoc_typehints_format == "short": kwargs.setdefault('unqualified_typehints', True) + __annotations__ = getattr(self.object, "__annotations__", {}) try: + if inspect.isNewType(self.object): + self.object.__annotations__["x"] = self.object.__supertype__ + self.object.__annotations__["return"] = self.object self.env.app.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) @@ -1279,6 +1283,8 @@ def format_args(self, **kwargs: Any) -> str: return None except ValueError: args = '' + finally: + self.object.__annotations__ = __annotations__ if self.config.strip_signature_backslash: # escape backslashes for reST @@ -1483,6 +1489,10 @@ def get_user_defined_function_or_method(obj: Any, attr: str) -> Any: call = None if call is not None: + __annotations__ = getattr(self.object, "__annotations__", {}) + if inspect.isNewType(self.object): + call.__annotations__["x"] = self.object.__supertype__ + call.__annotations__["return"] = self.object self.env.app.emit('autodoc-before-process-signature', call, True) try: sig = inspect.signature(call, bound_method=True, @@ -1490,6 +1500,8 @@ def get_user_defined_function_or_method(obj: Any, attr: str) -> Any: return type(self.object), '__call__', sig except ValueError: pass + finally: + self.object.__annotations__ = __annotations__ # Now we check if the 'obj' class has a '__new__' method new = get_user_defined_function_or_method(self.object, '__new__')