How can I show inherited members of a class in my Sphinx documentation?

本小妞迷上赌 提交于 2021-02-08 12:45:15

问题


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"

回答1:


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.



来源:https://stackoverflow.com/questions/30344289/how-can-i-show-inherited-members-of-a-class-in-my-sphinx-documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!