sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

sphinx autosummary: "failed to import Child.model" (inherited instance attribute)

Open AntonioCarta opened this issue 3 years ago • 9 comments

Describe the bug

I'm having trouble with sphinx and autosummary. Apparently, sphinx is unable to document inherited instance attributes. For some reason, the resulting error is an import error.

python code:

class Base:
    """Base class."""

    def __init__(self, **kwargs):
        """Init."""
        self.model = None
        """An attribute."""


class Child(Base):
    """Child class."""

    def __init__(self, **kwargs):
        """Init."""
        super().__init__()
        pass

rst documentation file:

.. autosummary::
    :toctree: generated

    mylib.Base
    mylib.Child

when I run sphinx-build with make html I get the following error:

.../new_docs/generated/mylib.Child.rst:24: WARNING: autosummary: failed to import Child.model

Notice that everything else is imported correctly, so this is not a path issue. Is this a bug? According to #3257 it should be supported. I'm using sphinx-build 4.3.0.

How to Reproduce

MWE to reproduce the error: sphinx-demo.zip unzip the archive

$ cd docs
$ make html

Expected behavior

inherited instance attributes are documented correctly and without warnings.

Your project

sphinx-demo.zip

Screenshots

No response

OS

WSL 2 (Ubuntu)

Python version

3.9.7

Sphinx version

4.3.0

Sphinx extensions

sphinx.ext.autosummary

Extra tools

No response

Additional context

No response

AntonioCarta avatar Nov 22 '21 13:11 AntonioCarta

Same problem here. Is this a regression ?

smarie avatar Apr 09 '22 12:04 smarie

It looks like this was supposed to be fixed by #9146, but I'm also seeing this problem. In my case, I was able to fix this using a jinja filter to simply remove the instance attributes from the docs, but it looks like a very clunky solution - and ideally those instance attributes should be documented.

melissawm avatar Apr 18 '22 18:04 melissawm

@melissawm could you share your jinja filter? It would help as a temporary fix until this is fixed.

AntonioCarta avatar May 11 '22 08:05 AntonioCarta

A workaround for me was to redefine the fields in the child class. Not sure whether that's an option for you, but it gets rid of the warning, which was important for us, since we alert on that.

janlugt avatar Jun 28 '22 12:06 janlugt

I had a look at this trying to figure out what goes wrong. As far as I can see the issue is that import_ivar_by_name only returns a result if qualname (aka. the name of the Class we are trying to document) is in the name found by ModuleAnalyzer. However for an inherited attribute the name found will be the name of the super class so nothing useful is returned an an ImportError is raised

jenshnielsen avatar Jul 16 '22 11:07 jenshnielsen

Sure @AntonioCarta here you go!

melissawm avatar Jul 18 '22 16:07 melissawm

@AntonioCarta @janlugt @melissawm Any change that any of you could test my suggested fix in #10691 I have been using it for a while and it seems to resolve my issue

jenshnielsen avatar Aug 12 '22 15:08 jenshnielsen

I have the same problem and can confirm that #10691 fixes it for me. Thanks! Hope this gets merged soon!

mbraakhekke avatar Aug 13 '22 12:08 mbraakhekke

@jenshnielsen Very keen to have this cut into a new version asap as well, so thank you first for making the PR and second pushing to get it tested/released! This bug spams our doc builds with all of these silly import errors because of our heavy use of derivative dataclasses.

Can affirm that copy/pasting the diff to import_ivar_by_name makes the errors go away for us too.

taylorm0192 avatar Aug 14 '22 05:08 taylorm0192

One more for "I have this problem and #10691 is the fix". Thanks @jenshnielsen

leotrs avatar Dec 19 '22 20:12 leotrs

We also have this problem and #10691 fixes it

oselcuk-iqm avatar Jan 23 '23 11:01 oselcuk-iqm

One more for "I have this problem and https://github.com/sphinx-doc/sphinx/pull/10691 is the fix". Thanks @jenshnielsen

12rambau avatar Mar 07 '23 12:03 12rambau