如何在 parent class 文档字符串中引用 child class 属性?

How to refer child class attributes in the parent class docstring?

我正在使用 :inherited-members: 来展示 parent 方法。

在为 child 生成的文档中,是否可以引用 child class?

class Parent:
   """This is a {Child class name} class

   This is in the {Child group attribute} group
   """
   group = ''
   ...

class Child(Parent):
   group = 'TheChild'
   ...

在为 child 生成的文档中,我希望看到:

> class Child(Parent)
>   Bases: Parent
>   This is a Child class.
>   It is in the TheChild group

Sphinx 可以吗?

您可以在 .rst 中使用狮身人面像 Cross-referencing syntax inside the docstrings, with the appropriate roles from the Python domain. After that the autodoc directives 来处理剩下的事情。

例子parent_child.py

class Parent:
    """
    This is a :py:class:`Child` class.

    This is in the :py:data:`Child.group` attribute.
    """
    group = 'TheParent'


class Child(Parent):
    group = 'TheChild'

parent_child.rst

Parent\_Child
=============

.. autoclass:: parent_child.Parent
    :members:
    :undoc-members:
    :show-inheritance:

.. autoclass:: parent_child.Child
    :members:
    :undoc-members:
    :show-inheritance:

结果: