autodoc: Show type annotation for instance variables

This commit is contained in:
Takeshi KOMIYA 2020-02-01 13:38:32 +09:00
parent 2ed26b4377
commit 20126433d6
2 changed files with 19 additions and 14 deletions

View File

@ -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)

View File

@ -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',