我希望能够从文档字符串自动生成我的代码文档。我有一些基本的类用于存储一些数据:
class DataHolder:
"""
Class to hold some data
Attributes:
batch: Run without GUI
debug (bool): Show debug messages
"""
batch: bool = False
debug: bool = False
name: str = 'default'
"""Object name"""
version: int = 0
"""int: Object version"""
我的rst
文件:
DataHolder
==========
.. autoclass:: data_holder.DataHolder
:members:
我用了不同的方式记录下每个属性以展示它们的区别,以下是输出结果:
看起来Sphinx无法将Attributes
部分与实际属性连接起来,这就是为什么它无法显示它们的默认值。
我想要实现的最终输出效果是像version
字段那样,使用为batch
定义的docstring。 我希望显示属性名称、默认值和类型,但从类型注释中提取。看起来Sphinx在这种情况下忽略了类型注释。
我的Sphinx扩展程序:
extensions = [
'sphinx.ext.viewcode',
'sphinx.ext.autodoc',
'sphinxcontrib.napoleon',
]
我该怎么做才能实现这样的行为?我找不到任何关于这种用例的好例子。
sphinx.ext.napoleon
与Sphinx打包在一起。请参见https://pypi.org/project/sphinxcontrib-napoleon/。 - mzjn.. autoattribute::
吗?例如,可以参考这个答案。 - DalyaG