Skip to content

Commit

Permalink
Fix sphinx-doc#8567: autodoc: Instance attributes are incorrectly add…
Browse files Browse the repository at this point in the history
…ed 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.
  • Loading branch information
tk0miya committed Dec 21, 2020
1 parent 31cad2e commit e91bb08
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -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
--------
Expand Down
6 changes: 4 additions & 2 deletions sphinx/ext/autodoc/__init__.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e91bb08

Please sign in to comment.