7

I wrote a python class and I made the documentation with sphinx. For example, the class looks like :

class Aclass(object): """ my class """ def __init__(self): """ constructor """ self.a = None """ doc for attribute a """ self._prop = None def _get_prop(self): """ getter prop """ return self._prop def _set_prop(self, val): """ setter prop """ self._prop = val prop = property(_get_prop, _set_prop) """ a property """ def square(self): """ return square of a """ return self.a**2 

Now, in order to do the documentation, in the rst file I wrote :

.. autoclass:: aclass.Aclass :members: 

All its ok, and a, prop and square appears in the doc.

enter image description here

But If I try to document attributes and methods separatly, sphinx says that it cannont find attribute a but it works for prop.

.. autoattribute:: aclass.Aclass.prop .. autoattribute:: aclass.Aclass.a 

The error message is :

Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 326, in import_object obj = self.get_attr(obj, part) File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 232, in get_attr return safe_getattr(obj, name, *defargs) File "/usr/lib/python2.7/dist-packages/sphinx/util/inspect.py", line 70, in safe_getattr raise AttributeError(name) AttributeError: a /home/gvallver/dev/sphinx/doc/source/index.rst:17: WARNING: autodoc can't import/find attribute 'aclass.Aclass.a', it reported error: "a", please check your spelling and sys.path 

I read somewhere Sphinx values for attributes reported as None that sphinx do not isntantiate the class, thus there is a difference between class attribute (as prop) and instance attribute (as a). But how can I refer to instance attribute in the doc ?

Actually, instance attributes are found if they are not explicitly asked in the rst file. For example, this will work :

.. autoclass:: aclass.Aclass :members: 

But this do not

.. autoclass: aclass.Aclass :members: a 
1

1 Answer 1

5

There is a bug report about this (created 2012-03-30; still open 2015-12-12): https://github.com/sphinx-doc/sphinx/issues/904.

  1. The problem with an explicit :members: list containing instance attributes was fixed in this commit (included in Sphinx 1.2b1).

  2. As mentioned in a comment (from Jon Waltman), there is an undocumented autoinstanceattribute directive.

    Using .. autoinstanceattribute:: aclass.Aclass.a does work (tested with Sphinx 1.1.3 and 1.2b1).

Sign up to request clarification or add additional context in comments.

5 Comments

Looks like the autoinstanceattribute no longer works: I have WARNING: Unknown directive type "autoinstanceattribute".
@GordonBai: The bug report was closed on Jul 16, 2020. Does autoattribute work?
Thers was a issue comment mentioning the autoattribute directive raises an AttributeError for an instance attribute. Then the commenter fixed the issue with a PR. However, it still raises AttributeError upon my test. I'm using Sphinx 4.0.2.
@GordonBai: autoinstanceattribute no longer works, but autoattribute should work in Sphinx 4.1.0. See github.com/sphinx-doc/sphinx/issues/9283.
@mjzn Thanks! Yes I'm aware of that issue as it's opened by me :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.