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
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Ger
  • 9,076
  • 10
  • 37
  • 48
  • S.Lott's solution here provides an additional work around to this: http://stackoverflow.com/questions/3959615/how-can-i-make-python-sphinx-document-object-attributes-only-declared-in-init – ecoe Jan 11 '14 at 19:56

1 Answers1

4

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).

Community
  • 1
  • 1
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • Looks like the `autoinstanceattribute` no longer works: I have `WARNING: Unknown directive type "autoinstanceattribute".` – Gordon Bai May 26 '21 at 00:06
  • @GordonBai: The bug report was closed on Jul 16, 2020. Does `autoattribute` work? – mzjn May 26 '21 at 06:21
  • Thers was a [issue comment](https://github.com/sphinx-doc/sphinx/issues/904#issuecomment-657100468) mentioning the `autoattribute` directive raises an `AttributeError` for an instance attribute. Then the commenter fixed the issue with a [PR](https://github.com/sphinx-doc/sphinx/pull/7946). However, it still raises `AttributeError` upon my test. I'm using **Sphinx 4.0.2**. – Gordon Bai May 26 '21 at 09:51
  • 2
    @GordonBai: `autoinstanceattribute` no longer works, but `autoattribute` should work in Sphinx 4.1.0. See https://github.com/sphinx-doc/sphinx/issues/9283. – mzjn Jul 12 '21 at 06:34
  • @mjzn Thanks! Yes I'm aware of that issue as it's opened by me :) – Gordon Bai Jul 13 '21 at 14:06