diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-02-01 13:38:32 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-02-03 01:18:38 +0900 |
commit | 20126433d6598a53eee0067bfb0ae641128b864f (patch) | |
tree | 79d013e5c0f42dbee8170ea74b4afbd061a4916d | |
parent | 2ed26b437744da564e456ed2adc8d50eee3b5166 (diff) | |
download | sphinx-git-20126433d6598a53eee0067bfb0ae641128b864f.tar.gz |
autodoc: Show type annotation for instance variables
-rw-r--r-- | sphinx/ext/autodoc/__init__.py | 29 | ||||
-rw-r--r-- | tests/test_autodoc.py | 4 |
2 files changed, 19 insertions, 14 deletions
diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 682fa1cdd..3fcbf9a2e 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1234,13 +1234,11 @@ class DataDocumenter(ModuleLevelDocumenter): super().add_directive_header(sig) sourcename = self.get_sourcename() if not self.options.annotation: - try: - 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) - except ValueError: - pass + # 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) try: objrepr = object_description(self.object) @@ -1419,13 +1417,16 @@ class AttributeDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter): sourcename = self.get_sourcename() if not self.options.annotation: if not self._datadescriptor: - try: - 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) - except ValueError: - pass + # 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) diff --git a/tests/test_autodoc.py b/tests/test_autodoc.py index c8ab55479..04768b638 100644 --- a/tests/test_autodoc.py +++ b/tests/test_autodoc.py @@ -1411,11 +1411,13 @@ def test_autodoc_typed_instance_variables(app): ' ', ' .. py:attribute:: Class.attr2', ' :module: target.typed_vars', + ' :type: int', ' :value: None', ' ', ' ', ' .. py:attribute:: Class.attr3', ' :module: target.typed_vars', + ' :type: int', ' :value: None', ' ', ' attr3', @@ -1423,6 +1425,7 @@ def test_autodoc_typed_instance_variables(app): ' ', ' .. py:attribute:: Class.attr4', ' :module: target.typed_vars', + ' :type: int', ' :value: None', ' ', ' attr4', @@ -1438,6 +1441,7 @@ def test_autodoc_typed_instance_variables(app): '', '.. py:data:: attr2', ' :module: target.typed_vars', + ' :type: str', " :value: None", '', ' attr2', |