我有一个像这样的三层类结构:
class Super(object):
"""This class is documented."""
class Intermediate(Super):
pass
class Sub(Intermediate):
"""This is also documented."""
我的 index.rst 文件如下所示:
.. automodule:: mymodule
:show-inheritance:
:inherited-members:
Sphinx 为我生成了一份不错的 API 文档。它包括类 Super 和 Sub,并带有适当的注释。它不包括 Intermediate,因为它没有注释而且我没有提供 undoc-members 标志。这是因为我不希望 Intermediate 出现在文档中。
我的问题是:因为我提供了 show-inheritance 标志,Sphinx 显示了每个类的基础; object 用于Super 和Intermediate 用于Sub。由于 Intermediate 没有记录,我不希望它出现在基类列表中。相反,我希望 Sphinx 显示继承树中的下一个记录类 Super。换句话说:我希望 Sphinx 显示 Super,而不是 Intermediate 作为 Sub 的基类。
有人知道怎么做吗?
最佳答案
对于这种想“隐藏”类继承的特殊情况,可以使用autoclass。记录每个可见类,而不是记录整个模块。
例如:
.. currentmodule:: demo
.. autoclass:: Super
:members:
.. autoclass:: Sub
:members:
然后你可以添加 :show-inheritance: 标志来显示你想要的类的继承。
引用文档:
The automodule, autoclass and autoexception directives also support a flag option called show-inheritance. When given, a list of base classes will be inserted just below the class signature (when used with automodule, this will be inserted for every class that is documented in the module).
关于python - Sphinx autodoc show-inheritance : How to skip undocumented, 中间基础?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17254854/