如何在父类文档字符串中引用子类属性?

时间:2020-05-16 11:06:47

标签: python parent-child python-sphinx docstring cross-reference

我正在使用:inherited-members:来显示父方法。

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

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

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

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

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

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

狮身人面像有可能吗?

1 个答案:

答案 0 :(得分:0)

您可以在文档字符串中使用Sphinx Cross-referencing syntax,并在roles中使用适当的Python domain。之后,.rst中的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:

结果:

parent_child_image