Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance attributes are incorrectly added to Parent class with Sphinx 3.4.0 #8567

Closed
jenshnielsen opened this issue Dec 21, 2020 · 3 comments

Comments

@jenshnielsen
Copy link
Contributor

Describe the bug

Trying to build the docs of QCoDeS microsoft/Qcodes#2549 with Sphinx 3.4.0 we are
seeing a failure where an instance attribute on a class cannot be resolved. This attribute is not defined on that class
but only on a different subclass. e.g. visabackend is defined on VisaInstrument but not on the parent class of VisaInstrument called Instrument but Sphinx tries to get that attribute on a non related subclass of Instrument ATS

This happens because Sphinx modifies __attributes__ in place. If a subclass does not define new class attributes it will reuse the __attributes__ dict of the super class as seen in the following simple example:

class X:
    a: int = 1 
    def __init__(self):
        self.b: int = 2
            
class Y(X):
    def __init__(self):
        self.d: int = 4

id(Y.__annotations__)
3051582812872

id(X.__annotations__)
3051582812872

id(Y.__annotations__) == id(X.__annotations__)
True

And AttributeDocumenter.update_annotations modifies the annotation dictionary of the subclass in place causing the annotation dictionary of the super class to be modified with it.

Replacing this with something that does

annotations = deepcopy(inspect.getannotations(parent))

for cls in inspect.getmro(parent):
   .... (existing code to update annotations)

parent.__annotations__ = annotations

Seems to resolve the issue

To Reproduce


$ sudo apt install pandoc
$ git clone https://github.com/QCoDeS/Qcodes.git
$ cd qcodes
$ pip install -r requirements.txt -r docs_requirements.txt
$ cd docs
$ make htmlfast

Expected behavior
Only the relevant attributes are attempted to be imported and the docs build correctly.

Your project
github.com/qcodes/qcodes

Screenshots
If applicable, add screenshots to help explain your problem.

Environment info

  • OS: Linux Ubnutu 18.04 (Github actions )
  • Python version: 3.
  • Sphinx version: 3.4.0
  • Sphinx extensions: e.g. ['nbsphinx', 'sphinx.ext.autodoc', 'sphinx.ext.autosummary',
    'sphinx.ext.napoleon', 'sphinx-jsonschema', 'sphinx.ext.doctest',
    'sphinx.ext.intersphinx', 'sphinx.ext.todo',
    'sphinx.ext.coverage', 'sphinx.ext.mathjax',
    'sphinx.ext.viewcode', 'sphinx.ext.githubpages',
    'sphinx.ext.todo']
@jenshnielsen
Copy link
Contributor Author

Note that DataDocumenter does something similar so that is likely to have the same issue (thou I have not observed it)

@tk0miya tk0miya added this to the 3.4.1 milestone Dec 21, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this issue Dec 21, 2020
…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.
@tk0miya
Copy link
Member

tk0miya commented Dec 21, 2020

Thank you for your good report. I posted a fix for this now. It will be released in this week (after other bugs will be also fixed).

tk0miya added a commit to tk0miya/sphinx that referenced this issue Dec 21, 2020
…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.
@jenshnielsen
Copy link
Contributor Author

@tk0miya Thank you for the quick response :)

tk0miya added a commit to tk0miya/sphinx that referenced this issue Dec 21, 2020
…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.
tk0miya added a commit that referenced this issue Dec 22, 2020
Fix #8567: autodoc: Instance attributes are incorrectly added to Parent class
@tk0miya tk0miya closed this as completed Dec 22, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants