Skip to content

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

Closed
@jenshnielsen

Description

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']

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions