sphinx的一个扩展:code:`autodoc',在使用python 2样式的类型注释时,用类型注释扩充sphinx文档。
py2annotate的Python项目详细描述
sphinx autodoc
的扩展,在使用python 2样式的类型注释时,用类型注释扩充sphinx文档。
问题
python 2不支持类型注释。因此,使用类型注释编写python 2/python 3不可知代码需要putting the type annotations in comments:
defadd(x,y):# type: (int, int) -> int"""Adds two numbers."""returnx+y
但是现在当使用Sphinx生成文档时,文档不包括类型注释:
来自Signatory项目的示例。
解决方案
这个扩展补救了一些问题,使sphinx文档现在看起来像:
(这与使用python 3样式的类型注释相同,例如defadd(x:int,y:int)->int
)
安装
通过PIP:
pip install py2annotate
它也是一个单一的文件,所以复制粘贴代码,如果你想。
需要Python3.6+。
用法
只需将py2anotate添加到conf.py
:
# conf.py...extensions=['sphinx.ext.autodoc','py2annotate']...
(无论是在autodoc
之前还是之后,这都不重要。)
注意sphinx本身必须使用python 3.6+运行。这是因为py2anotate使用python 3样式类型 内部注释以确定正确的注释。