From 5d301b44b948da336435c458245766709b6d2042 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 1 Aug 2020 22:23:33 +0900 Subject: [PATCH] Fix #8032: autodoc: A typehint for inherited ivars is not shown This tries to import the parent object for the instance attribute to get type annotations for the variable. --- CHANGES | 2 ++ sphinx/ext/autodoc/__init__.py | 11 +++++++++++ tests/roots/test-ext-autodoc/target/typed_vars.py | 4 ++++ tests/test_ext_autodoc.py | 14 ++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/CHANGES b/CHANGES index 59551371fad..11f063b3da3 100644 --- a/CHANGES +++ b/CHANGES @@ -61,6 +61,8 @@ Bugs fixed * #7983: autodoc: Generator type annotation is wrongly rendered in py36 * #8030: autodoc: An uninitialized annotated instance variable is not documented when ``:inherited-members:`` option given +* #8032: autodoc: A type hint for the instance variable defined at parent class + is not shown in the document of the derived class * #7839: autosummary: cannot handle umlauts in function names * #7865: autosummary: Failed to extract summary line when abbreviations found * #7866: autosummary: Failed to extract correct summary line when docstring diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 554997ff750..f3d516c58f5 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -2020,11 +2020,22 @@ def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: isattr and member is INSTANCEATTR) + def import_parent(self) -> Any: + try: + parent = importlib.import_module(self.modname) + for name in self.objpath[:-1]: + parent = self.get_attr(parent, name) + + return parent + except (ImportError, AttributeError): + return None + def import_object(self, raiseerror: bool = False) -> bool: """Never import anything.""" # disguise as an attribute self.objtype = 'attribute' self.object = INSTANCEATTR + self.parent = self.import_parent() self._datadescriptor = False return True diff --git a/tests/roots/test-ext-autodoc/target/typed_vars.py b/tests/roots/test-ext-autodoc/target/typed_vars.py index 65302fa44b5..43c58deb05b 100644 --- a/tests/roots/test-ext-autodoc/target/typed_vars.py +++ b/tests/roots/test-ext-autodoc/target/typed_vars.py @@ -25,3 +25,7 @@ def __init__(self): self.attr5: int #: attr5 self.attr6 = 0 # type: int """attr6""" + + +class Derived(Class): + pass diff --git a/tests/test_ext_autodoc.py b/tests/test_ext_autodoc.py index be46c1490fb..8fa61e044e8 100644 --- a/tests/test_ext_autodoc.py +++ b/tests/test_ext_autodoc.py @@ -1576,6 +1576,20 @@ def test_autodoc_typed_instance_variables(app): ' This is descr4', '', '', + '.. py:class:: Derived()', + ' :module: target.typed_vars', + '', + '', + ' .. py:attribute:: Derived.attr2', + ' :module: target.typed_vars', + ' :type: int', + '', + '', + ' .. py:attribute:: Derived.descr4', + ' :module: target.typed_vars', + ' :type: int', + '', + '', '.. py:data:: attr1', ' :module: target.typed_vars', ' :type: str',