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

autodocs does not work well with instance attributes. #904

Closed
shimizukawa opened this issue Jan 3, 2015 · 11 comments
Closed

autodocs does not work well with instance attributes. #904

shimizukawa opened this issue Jan 3, 2015 · 11 comments

Comments

@shimizukawa
Copy link
Member

Instance attributes set in an init method can be documented with #: comments. When using the autoclass directive with :members: and no list of members, these attributes are picked up by Sphinx, but they are documented with a value of "None", even though there is no such default value.

Additionally, if using :members: with an explicit member list or if using the autoattribute directive, the attribute is not documented and Sphinx prints a traceback on the terminal (but continue with the rest of the document). For example:

Traceback (most recent call last):                                              
  File "/home/simon/.virtualenvs/tinycss/lib/python3.2/site-packages/sphinx/util/inspect.py", line 64, in safe_getattr
    return getattr(obj, name, *defargs)
AttributeError: type object 'Stylesheet' has no attribute 'rules'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/simon/.virtualenvs/tinycss/lib/python3.2/site-packages/sphinx/ext/autodoc.py", line 326, in import_object
    obj = self.get_attr(obj, part)
  File "/home/simon/.virtualenvs/tinycss/lib/python3.2/site-packages/sphinx/ext/autodoc.py", line 232, in get_attr
    return safe_getattr(obj, name, *defargs)
  File "/home/simon/.virtualenvs/tinycss/lib/python3.2/site-packages/sphinx/util/inspect.py", line 70, in safe_getattr
    raise AttributeError(name)
AttributeError: rules

/home/simon/projects/tinycss/docs/parsing.rst:25: WARNING: autodoc can't import/find attribute 'tinycss.css21.Stylesheet.rules', it reported error: "rules", please check your spelling and sys.path

@shimizukawa
Copy link
Member Author

From Antonio Valentino on 2012-07-08 15:51:57+00:00

Same problem for me.
It seems that also the autoattribute directive seems to be broken.

The following doc

{{{
#!rst

Test 7

.. currentmodule:: module

.. autoclass:: Foo

Attributes

.. autoattribute:: Foo.bar

.. autoattribute:: Foo.flox

.. autoattribute:: Foo.baz

.. autoattribute:: Foo.qux

.. autoattribute:: Foo.spam

Test 8

.. currentmodule:: module

.. autoclass:: Foo

.. autoattribute:: bar

.. autoattribute:: flox

.. autoattribute:: baz

.. autoattribute:: qux

.. autoattribute:: spam

}}}

do not build correctly and gives the following error:

{{{
$ make html
sphinx-build -b html -d _build/doctrees . _build/html
Making output directory...
Running Sphinx v1.2pre/348224ae1fd5
loading pickled environment... not yet created
building [html]: targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
Traceback (most recent call last):oattr
File "/home/antonio/projects/sphinx/sphinx/ext/autodoc.py", line 326, in import_object
obj = self.get_attr(obj, part)
File "/home/antonio/projects/sphinx/sphinx/ext/autodoc.py", line 232, in get_attr
return safe_getattr(obj, name, *defargs)
File "/home/antonio/projects/sphinx/sphinx/util/inspect.py", line 70, in safe_getattr
raise AttributeError(name)
AttributeError: qux
Traceback (most recent call last):
File "/home/antonio/projects/sphinx/sphinx/ext/autodoc.py", line 326, in import_object
obj = self.get_attr(obj, part)
File "/home/antonio/projects/sphinx/sphinx/ext/autodoc.py", line 232, in get_attr
return safe_getattr(obj, name, *defargs)
File "/home/antonio/projects/sphinx/sphinx/util/inspect.py", line 70, in safe_getattr
raise AttributeError(name)
AttributeError: spam
Traceback (most recent call last):
File "/home/antonio/projects/sphinx/sphinx/ext/autodoc.py", line 326, in import_object
obj = self.get_attr(obj, part)
File "/home/antonio/projects/sphinx/sphinx/ext/autodoc.py", line 232, in get_attr
return safe_getattr(obj, name, *defargs)
File "/home/antonio/projects/sphinx/sphinx/util/inspect.py", line 70, in safe_getattr
raise AttributeError(name)
AttributeError: qux
Traceback (most recent call last):
File "/home/antonio/projects/sphinx/sphinx/ext/autodoc.py", line 326, in import_object
obj = self.get_attr(obj, part)
File "/home/antonio/projects/sphinx/sphinx/ext/autodoc.py", line 232, in get_attr
return safe_getattr(obj, name, *defargs)
File "/home/antonio/projects/sphinx/sphinx/util/inspect.py", line 70, in safe_getattr
raise AttributeError(name)
AttributeError: spam

/home/antonio/tmp/sphinx-autoattribute-issue/doc/test_autoattr.rst:86: WARNING: autodoc can't import/find attribute 'module.Foo.qux', it reported error: "qux", please check your spelling and sys.path
/home/antonio/tmp/sphinx-autoattribute-issue/doc/test_autoattr.rst:88: WARNING: autodoc can't import/find attribute 'module.Foo.spam', it reported error: "spam", please check your spelling and sys.path
/home/antonio/tmp/sphinx-autoattribute-issue/doc/test_autoattr.rst:13: WARNING: autodoc can't import/find attribute 'module.Foo.qux', it reported error: "qux", please check your spelling and sys.path
/home/antonio/tmp/sphinx-autoattribute-issue/doc/test_autoattr.rst:15: WARNING: autodoc can't import/find attribute 'module.Foo.spam', it reported error: "spam", please check your spelling and sys.path
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] test_autoattr
writing additional files... (1 module code pages) _modules/index
genindex search
copying static files... done
dumping search index... done
dumping object inventory... done
build succeeded, 4 warnings.

Build finished. The HTML pages are in _build/html.
}}}

The python code I'm trying to document is the one used in the sphinx documentation as example (http://sphinx.pocoo.org/ext/autodoc.html?highlight=autoattribute#directive-autoattribute):

{{{
#!python

class Foo:
"""Docstring for class Foo."""

#: Doc comment for class attribute Foo.bar.
#: It can have multiple lines.
bar = 1

flox = 1.5   #: Doc comment for Foo.flox. One line only.

baz = 2
"""Docstring for class attribute Foo.baz."""

def __init__(self):
    #: Doc comment for instance attribute qux.
    self.qux = 3

    self.spam = 4
    """Docstring for instance attribute spam."""

}}}

@shimizukawa
Copy link
Member Author

From Alexander Stepanov on 2012-11-06 10:07:22+00:00

I have same problem :(

@shimizukawa
Copy link
Member Author

From Jon Waltman on 2012-11-09 06:11:37+00:00

autodoc: Handle explicit instance attributes in :members: (re #904)

→ <<cset 6f9e541>>

@shimizukawa
Copy link
Member Author

From Jon Waltman on 2012-11-09 06:18:12+00:00

The case where you give an explicit :members: list
containing instance attributes should be fixed now.

they are documented with a value of "None",
even though there is no such default value.

Instance attributes are not evaluated and getattr'd like other
attributes so no value is available to document. Autodoc just
displays them as None.

if using the autoattribute directive, the attribute is
not documented and Sphinx prints a traceback

The autoattribute directive still doesn't work for instance
attributes but their is an undocumented autoinstanceattribute
directive that does work.

I'm not sure if this is the intended behavior and
autoinstanceattribute just needs to be documented or if
autoattribute should also handle instance attributes.

@shimizukawa
Copy link
Member Author

From Alexander Stepanov on 2012-11-13 12:35:15+00:00

Jonathan Beavers Waltman

Instance attributes are not evaluated and getattr'd like other attributes so no value is available to document. Autodoc just displays them as None.

So what is a value of printing "None" near each attribute if it is only possible value? It's ugly!

@shimizukawa
Copy link
Member Author

From Johannes Dewender on 2013-01-24 13:59:07+00:00

There is a :novalue: option proposed in pull request #109. This would keep autodoc from printing values for these attributes.

@alxroyer
Copy link

Just wanted to share a hack on that topic.

I managed to avoid displaying values for all attributes with a single piece of code in my conf.py file.

⚠️ Disables class attribute values by the way,
but I consider in the end that loosing this info is less a pain than having lots of false info for the generally more numerous instance attributes.

# Hack:
# Replace the `sphinx.ext.autodoc.object_description()` function
# in the `sphinx.ext.autodoc` module.
def no_object_description(obj):
    raise ValueError("No value for data/attributes")
import sphinx.ext.autodoc
sphinx.ext.autodoc.object_description = no_object_description

Don't know whether there is a simple and safer way to do this?
I tried to play around :annotation: (see bitbucket#109, or with the autodoc-process-signature handler, but without success.
Let me know.

@tk0miya tk0miya added this to the 3.2.0 milestone Jun 10, 2020
@tk0miya
Copy link
Member

tk0miya commented Jul 11, 2020

About None value, it was fixed since 3.1.0 (refs: #2044 and #7515).

But the second problem that the autoattribute directive raises an AttributeError for an instance variable is still not fixed.

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

   .. autoattribute:: ivar
# example.py
class Foo:
    def __init__(self):
        #: ivar
        self.ivar = None
WARNING: autodoc: failed to import attribute 'Foo.ivar' from module 'example'; the following exception was raised:
Traceback (most recent call last):
  File "/Users/tkomiya/work/sphinx/sphinx/util/inspect.py", line 324, 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/tkomiya/work/sphinx/sphinx/ext/autodoc/importer.py", line 71, in import_object
    obj = attrgetter(obj, attrname)
  File "/Users/tkomiya/work/sphinx/sphinx/ext/autodoc/__init__.py", line 245, in get_attr
    return autodoc_attrgetter(self.env.app, obj, name, *defargs)
  File "/Users/tkomiya/work/sphinx/sphinx/ext/autodoc/__init__.py", line 2063, in autodoc_attrgetter
    return safe_getattr(obj, name, *defargs)
  File "/Users/tkomiya/work/sphinx/sphinx/util/inspect.py", line 340, in safe_getattr
    raise AttributeError(name) from exc
AttributeError: ivar

tk0miya added a commit to tk0miya/sphinx that referenced this issue Jul 12, 2020
…utofunction

So far, autofunction only checks the existence of the class variable.
As a result, it crashes with AttributeError if an instance variable is
specified.

This adds an exisitence for instance attributes via ModuleAnalyzer
before getattr-check.
@g6ai
Copy link

g6ai commented May 26, 2021

@tk0miya Thanks for the contribution! I still encounter AttributeError when trying to use autoattribute directive for an instance variable, with Sphinx 4.0.2

@tk0miya
Copy link
Member

tk0miya commented May 29, 2021

@g6ai Could you file a new issue, please? I'd like to know what is happened on your PC.

@g6ai
Copy link

g6ai commented May 29, 2021

@tk0miya Please see issue #9283 I just opened.

@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

4 participants