python 3 中的类型提示可以用来生成文档字符串吗?
Can type hint in python 3 be used to generate docstring?
我们可以在python中使用docstring指明函数参数的类型:
def f1(a):
"""
:param a: an input.
:type a: int
:return: the input integer.
:rtype: int
"""
return a
对于 f1
,autodoc 生成以下文档:
fun1(a)
Parameters : a (int) – an input.
Returns : the input integer.
Return type: int
在python3中,类型也可以通过类型提示来指示:
def f2(a: int):
"""
:param a: an input.
:return: the input integer.
:rtype: int
"""
return a
当我们运行 autodoc时,它通过参数声明来放置类型,而不是在描述中:
f2(a: int)
Parameters : a – an input.
Returns : the input integer.
Return type: int
是否可以使用注释而不是文档字符串将文档生成为 f1
?我正在使用 python 3.6。谢谢!
还没有,据我所知,Sphinx 尚不支持此功能。评论中提到的错误是关于 type-hints 的表示而不是它们的定位。
我知道目前有一个名为 sphinx-autodoc-typehints 的 Sphinx 扩展可以为您解决这个问题。您暂时可以使用它。
我们可以在python中使用docstring指明函数参数的类型:
def f1(a):
"""
:param a: an input.
:type a: int
:return: the input integer.
:rtype: int
"""
return a
对于 f1
,autodoc 生成以下文档:
fun1(a)
Parameters : a (int) – an input.
Returns : the input integer.
Return type: int
在python3中,类型也可以通过类型提示来指示:
def f2(a: int):
"""
:param a: an input.
:return: the input integer.
:rtype: int
"""
return a
当我们运行 autodoc时,它通过参数声明来放置类型,而不是在描述中:
f2(a: int)
Parameters : a – an input.
Returns : the input integer.
Return type: int
是否可以使用注释而不是文档字符串将文档生成为 f1
?我正在使用 python 3.6。谢谢!
还没有,据我所知,Sphinx 尚不支持此功能。评论中提到的错误是关于 type-hints 的表示而不是它们的定位。
我知道目前有一个名为 sphinx-autodoc-typehints 的 Sphinx 扩展可以为您解决这个问题。您暂时可以使用它。