如何在我的Sphinx文档中显示一个类的继承成员?

15

我想记录一些从同一个基类派生的类,这些类有一些共同的属性,我希望在子类中为每个属性重复记录文档,以便我可以在一个地方查看一个类的所有属性。

例如,我有以下代码:

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

我有这个rst文件:

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

输出结果将如下所示:
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.

有没有一种方法可以生成文档,使子类也能够继承属性?
例如:
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个回答

22

您需要添加:inherited-members:选项,引用自文档:

对于类和异常,在记录所有成员时将忽略从基类继承的成员,除非您除了成员之外还提供了继承成员标志选项。


这对我来说完全正常。很难说为什么它在你这里不起作用,你尝试升级Sphinx或使用默认主题看看是否可以解决问题了吗? - Max Tepkeev
很奇怪:在我上面发布的测试中它可以工作,但在我的实际项目中却不能。我真的无法解释为什么,因为conf.py或其他文件中没有实质性的区别。我唯一能想到的区别是,在我的项目中,该类是“嵌套”的:.. autoclass:: www.oe.oe_wsme.wstypes.BaseModel - Kjir
1
可能你继承的成员没有定义文档字符串,在这种情况下,请尝试添加 :undoc-members: - Sergey Lyapustin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接