I have a class like this:
class MyBase(object):
x = 3
"""Documentation for property x"""
and another class that inherits it:
class MyObj(MyBase):
x = 0
When I use sphinx's autodoc to generate documentation, MyObj.x
is not documented. Is there any way to inherit the docstring from MyBase.x
? I found DocInherit but since this uses a decorator, it only works for class methods. Any way to do this with properties?
As Thomas already stated, attributes do not have docstrings in Python. Sphinx however provides it's own processing allowing for attributes to be documented.
This results in:
I found a workaround using the property function:
This is nice in that given an instance variable:
one can call
help(m)
and get proper documentation of propertyx
and sphinx also picks this up correctly.As far as I know, docstrings for attributes are not part of Python. When I try it,
MyBase.x.__doc__
does not get set to the string beneath it. Docstrings only work on classes, functions and methods. If Sphinx picks up the string underneathx = 3
as a docstring, it's probably doing its own processing of the source code to get that.If you only care for building Documentation via Sphinx. you can use: ":inherited-members:"
This will also add the doc strings of inherited members in Sphinx Documentation.
http://sphinx-doc.org/ext/autodoc.html