14

I want to document some classes which all derive from the same base class with some common attributes and I would like to repeat the documentation for every attribute in the subclasses, so that I can see all the attributes for a class in a single place.

So for instance I have this code:

class Base(object):

    """Base class."""

    #: First attribute
    a = int
    #: Second attribute
    b = str

class FirstChild(Base):

    """First Child of Base."""

    #: Child attribute
    c = float

class SecondChild(Base):

    """Second Child of Base."""

    pass

and I have this rst:

.. automodule:: example
   :members:
   :show-inheritance:

The output will be like this:

class class example.Base

   Bases: "object"

   Base class.

   a
      First attribute
      alias of "int"

   b
      Second attribute
      alias of "str"

class class example.FirstChild

   Bases: "example.Base"

   First Child of Base.

   c
      Child attribute
      alias of "float"

class class example.SecondChild

   Bases: "example.Base"

   Second Child of Base.

Is there a way to generate documentation such that the child classes will also have the inherited attributes?

For instance:

class class example.FirstChild

   Bases: "example.Base"

   First Child of Base.

   a
      First attribute
      alias of "int"

   b
      Second attribute
      alias of "str"

   c
      Child attribute
      alias of "float"

class class example.SecondChild

   Bases: "example.Base"

   Second Child of Base.

   a
      First attribute
      alias of "int"

   b
      Second attribute
      alias of "str"
Kjir
  • 4,437
  • 4
  • 29
  • 34
  • Possible duplicate of [Docstring inheritance for properties using sphinx's autodoc](http://stackoverflow.com/questions/5516032/docstring-inheritance-for-properties-using-sphinxs-autodoc) – Louis Sep 14 '16 at 12:38

1 Answers1

21

You need to add the :inherited-members: option, quote from the docs:

For classes and exceptions, members inherited from base classes will be left out when documenting all members, unless you give the inherited-members flag option, in addition to members.

Max Tepkeev
  • 2,636
  • 1
  • 14
  • 12
  • Works perfectly fine for me here. It's hard to tell why it doesn't work for you, have you tried to upgrade Sphinx or to use a default theme and see if it works with it. – Max Tepkeev May 21 '15 at 16:06
  • That is weird: in my test posted above it works, but in my actual project it doesn't. I really can't explain why since there are no substantial differences in either conf.py or the other files. The only difference I can think of is that in my project the class is "nested": `.. autoclass:: www.oe.oe_wsme.wstypes.BaseModel` – Kjir May 22 '15 at 07:20
  • 1
    Probably your inherited members have no docstring defined, in that case try to add ` :undoc-members:` as well. – Sergey Lyapustin May 02 '17 at 13:33