noio noio - 3 months ago 23
Python Question

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
Some Documentation


I include it like

.. autoclass:: core.SomeClass
:members:


And my code looks like

class SomeClass(object):
def __init__(self):
self.attribute = "value" #: Some Documentation


Is there a way to either make the "= None" report the real value, or make it disappear?

Answer

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.