From e91bb08b51937164a9338e346dd95de87f33ec09 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 22 Dec 2020 02:21:27 +0900 Subject: [PATCH] Fix #8567: autodoc: Instance attributes are incorrectly added to Parent class The instance attributes on subclasses are shown on the document of parent class unexpectedly because of autodoc modifies `__annotations__` in place. This fix creates a copy of `__annotations__` attribute and attach it to the subclass. --- CHANGES | 1 + sphinx/ext/autodoc/__init__.py | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 6125b83a1da..7241391ae02 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,7 @@ Bugs fixed * #8559: autodoc: AttributeError is raised when using forward-reference type annotations * #8568: autodoc: TypeError is raised on checking slots attribute +* #8567: autodoc: Instance attributes are incorrectly added to Parent class Testing -------- diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 4bf97cc851a..5e078263a61 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1856,7 +1856,8 @@ def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: def update_annotations(self, parent: Any) -> None: """Update __annotations__ to support type_comment and so on.""" try: - annotations = inspect.getannotations(parent) + annotations = dict(inspect.getannotations(parent)) + parent.__annotations__ = annotations analyzer = ModuleAnalyzer.for_module(self.modname) analyzer.analyze() @@ -2292,7 +2293,8 @@ def isinstanceattribute(self) -> bool: def update_annotations(self, parent: Any) -> None: """Update __annotations__ to support type_comment and so on.""" try: - annotations = inspect.getannotations(parent) + annotations = dict(inspect.getannotations(parent)) + parent.__annotations__ = annotations for cls in inspect.getmro(parent): try: