summaryrefslogtreecommitdiff
path: root/sphinx/ext/autodoc/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/ext/autodoc/__init__.py')
-rw-r--r--sphinx/ext/autodoc/__init__.py36
1 files changed, 32 insertions, 4 deletions
diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py
index fd5d2bd05..b9404804b 100644
--- a/sphinx/ext/autodoc/__init__.py
+++ b/sphinx/ext/autodoc/__init__.py
@@ -10,6 +10,7 @@
:license: BSD, see LICENSE for details.
"""
+import importlib
import re
import warnings
from types import ModuleType
@@ -31,6 +32,7 @@ from sphinx.util import logging
from sphinx.util import rpartition
from sphinx.util.docstrings import extract_metadata, prepare_docstring
from sphinx.util.inspect import getdoc, object_description, safe_getattr, stringify_signature
+from sphinx.util.typing import stringify as stringify_typehint
if False:
# For type annotation
@@ -1262,12 +1264,22 @@ class DataDocumenter(ModuleLevelDocumenter):
super().add_directive_header(sig)
sourcename = self.get_sourcename()
if not self.options.annotation:
+ # obtain annotation for this data
+ annotations = getattr(self.parent, '__annotations__', {})
+ if self.objpath[-1] in annotations:
+ objrepr = stringify_typehint(annotations.get(self.objpath[-1]))
+ self.add_line(' :type: ' + objrepr, sourcename)
+ else:
+ key = ('.'.join(self.objpath[:-1]), self.objpath[-1])
+ if self.analyzer and key in self.analyzer.annotations:
+ self.add_line(' :type: ' + self.analyzer.annotations[key],
+ sourcename)
+
try:
objrepr = object_description(self.object)
+ self.add_line(' :value: ' + objrepr, sourcename)
except ValueError:
pass
- else:
- self.add_line(' :annotation: = ' + objrepr, sourcename)
elif self.options.annotation is SUPPRESS:
pass
else:
@@ -1306,6 +1318,12 @@ class DataDeclarationDocumenter(DataDocumenter):
"""Never import anything."""
# disguise as a data
self.objtype = 'data'
+ try:
+ # import module to obtain type annotation
+ self.parent = importlib.import_module(self.modname)
+ except ImportError:
+ pass
+
return True
def add_content(self, more_content: Any, no_docstring: bool = False) -> None:
@@ -1434,12 +1452,22 @@ class AttributeDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter):
sourcename = self.get_sourcename()
if not self.options.annotation:
if not self._datadescriptor:
+ # obtain annotation for this attribute
+ annotations = getattr(self.parent, '__annotations__', {})
+ if self.objpath[-1] in annotations:
+ objrepr = stringify_typehint(annotations.get(self.objpath[-1]))
+ self.add_line(' :type: ' + objrepr, sourcename)
+ else:
+ key = ('.'.join(self.objpath[:-1]), self.objpath[-1])
+ if self.analyzer and key in self.analyzer.annotations:
+ self.add_line(' :type: ' + self.analyzer.annotations[key],
+ sourcename)
+
try:
objrepr = object_description(self.object)
+ self.add_line(' :value: ' + objrepr, sourcename)
except ValueError:
pass
- else:
- self.add_line(' :annotation: = ' + objrepr, sourcename)
elif self.options.annotation is SUPPRESS:
pass
else: