Skip to content Skip to sidebar Skip to footer

Sphinx Values For Attributes Reported As None

When I use Sphinx autodoc to document a class, the values for the attributes are always reported, (as it says it should here, under #437) but always as '= None' Attribute = None

Solution 1:

There will be an :annotation: option (see pull-request) in the upcoming version 1.2 of sphinx (and in the second beta).

For autodata/autoattribute you can then force a specific value or suppress it. So in order to print no value for the attribute you would put:

.. autoclass:: core.SomeClass

   .. autoattribute:: attribute
      :annotation:

Currently it only works with autodata/autoattribute directly and not recursively with automodule/autoclass.

Solution 2:

I am pretty sure this has to do with the fact that your attribute is an instance attribute. It does not get a value until the class is instantiated. Sphinx imports modules in order to inspect them, but it does not instantiate any classes.

So the "real value" is not known by Sphinx, and None is output. I don't think you can make it go away easily (but I suppose anything is possible if you are prepared to patch the Sphinx source code...). If you don't like this, you could document attributes in the docstring of the class instead.

Class attributes that are documented using the same markup scheme (described here) do get their values displayed in the rendered output. But there is no clear indication that makes it easy for the reader to distinguish between class and instance attributes. Maybe Sphinx could be a little more helpful here.

Solution 3:

For the current version of Sphinx, you can put a monkeypatch in the conf.py of your project that fixes this problem:

from sphinx.ext.autodoc import (
    ClassLevelDocumenter, InstanceAttributeDocumenter)

defiad_add_directive_header(self, sig):
    ClassLevelDocumenter.add_directive_header(self, sig)

InstanceAttributeDocumenter.add_directive_header = iad_add_directive_header

This is discussed in Sphinx issue #2044

Solution 4:

This still seems to be an issue. I worked around it with:

classValueDoc:def__init__(self, text):
        self.text = text

    def__repr__(self):
        returnself.text

And then define the attribute at the class level like:

#: documentation for foofoo = ValueDoc('text to show up after =')

Solution 5:

I wasn't able to get annotations working for instance attributes. I chose to just hide the attribute values in my theme.

Example html

<dlclass="attribute"><dt><codeclass="descName">Attribute</code><emclass="property"> = None</em></dt></dl>

Theme CSS to hide = None

dddl.attributeem.property { display: none }

Post a Comment for "Sphinx Values For Attributes Reported As None"