mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #7564 from eric-wieser/fix-missing-__annotations__
autodoc: Fix a logic error that causes annotations not to be shown for descriptors
This commit is contained in:
commit
7888600f96
@ -1604,18 +1604,19 @@ class AttributeDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter):
|
||||
super().add_directive_header(sig)
|
||||
sourcename = self.get_sourcename()
|
||||
if not self.options.annotation:
|
||||
if not self._datadescriptor:
|
||||
# obtain annotation for this attribute
|
||||
annotations = getattr(self.parent, '__annotations__', {})
|
||||
if annotations and 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)
|
||||
# obtain type annotation for this attribute
|
||||
annotations = getattr(self.parent, '__annotations__', {})
|
||||
if annotations and 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)
|
||||
|
||||
# data descriptors do not have useful values
|
||||
if not self._datadescriptor:
|
||||
try:
|
||||
if self.object is INSTANCEATTR:
|
||||
pass
|
||||
|
@ -6,11 +6,20 @@ attr2: str
|
||||
attr3 = '' # type: str
|
||||
|
||||
|
||||
class _Descriptor:
|
||||
def __init__(self, name):
|
||||
self.__doc__ = "This is {}".format(name)
|
||||
def __get__(self):
|
||||
pass
|
||||
|
||||
|
||||
class Class:
|
||||
attr1: int = 0
|
||||
attr2: int
|
||||
attr3 = 0 # type: int
|
||||
|
||||
descr4: int = _Descriptor("descr4")
|
||||
|
||||
def __init__(self):
|
||||
self.attr4: int = 0 #: attr4
|
||||
self.attr5: int #: attr5
|
||||
|
@ -1511,6 +1511,13 @@ def test_autodoc_typed_instance_variables(app):
|
||||
' attr6',
|
||||
'',
|
||||
'',
|
||||
' .. py:attribute:: Class.descr4',
|
||||
' :module: target.typed_vars',
|
||||
' :type: int',
|
||||
'',
|
||||
' This is descr4',
|
||||
'',
|
||||
'',
|
||||
'.. py:data:: attr1',
|
||||
' :module: target.typed_vars',
|
||||
' :type: str',
|
||||
|
Loading…
Reference in New Issue
Block a user