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

AttributeError when trying to use autoattribute directive for an instance variable #9283

Closed
g6ai opened this issue May 29, 2021 · 1 comment

Comments

@g6ai
Copy link

g6ai commented May 29, 2021

Describe the bug
I got AttributeError when trying to use autoattribute directive for an instance variable. This is a follow-up to #904 (comment).

To Reproduce
Steps to reproduce the behavior:

# index.rst
.. autoclass:: example.Foo

   .. autoattribute:: ivar
# example.py
class Foo:
    def __init__(self):
        self.ivar = None
$ make html
WARNING: autodoc: failed to import attribute 'Foo.ivar' from module 'example'; the following exception was raised:
Traceback (most recent call last):
  File "/Users/gbai/.pyenv/versions/miniconda3-latest/envs/sphinx/lib/python3.9/site-packages/sphinx/util/inspect.py", line 412, in safe_getattr
    return getattr(obj, name, *defargs)
AttributeError: type object 'Foo' has no attribute 'ivar'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/gbai/.pyenv/versions/miniconda3-latest/envs/sphinx/lib/python3.9/site-packages/sphinx/ext/autodoc/importer.py", line 110, in import_object
    obj = attrgetter(obj, mangled_name)
  File "/Users/gbai/.pyenv/versions/miniconda3-latest/envs/sphinx/lib/python3.9/site-packages/sphinx/ext/autodoc/__init__.py", line 318, in get_attr
    return autodoc_attrgetter(self.env.app, obj, name, *defargs)
  File "/Users/gbai/.pyenv/versions/miniconda3-latest/envs/sphinx/lib/python3.9/site-packages/sphinx/ext/autodoc/__init__.py", line 2606, in autodoc_attrgetter
    return safe_getattr(obj, name, *defargs)
  File "/Users/gbai/.pyenv/versions/miniconda3-latest/envs/sphinx/lib/python3.9/site-packages/sphinx/util/inspect.py", line 428, in safe_getattr
    raise AttributeError(name) from exc
AttributeError: ivar

Environment info

  • OS: macOS 10.14.6
  • Python version: 3.9.5
  • Sphinx version: 4.0.2
  • Sphinx extensions: sphinx.ext.autodoc
  • Extra tools:
@g6ai g6ai added the type:bug label May 29, 2021
tk0miya added a commit to tk0miya/sphinx that referenced this issue May 30, 2021
…ommented

Autoattribute directive should check the existence of instance attribute
that is defined inside __init__() but not having comments before
accessing it.
@tk0miya
Copy link
Member

tk0miya commented May 30, 2021

Thank you for reporting. This is caused if the instance attribute does not have comment. #9285 will fix this.

As a workaround, you can avoid the error via adding comment to the attribute.

Thanks,

@tk0miya tk0miya added this to the 4.1.0 milestone May 30, 2021
@tk0miya tk0miya closed this as completed in 9a9433e Jun 3, 2021
tk0miya added a commit that referenced this issue Jun 3, 2021
…ng_comment

Fix #9283: autodoc: failed to build doc for attribute not commented
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 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