Sphinx 是否有支持的标准来记录不是简单的单个对象的参数或返回值类型?
例如,在下面的例子中,arg1 是一个 str,arg2 是一个 str 列表,arg3 是一个 str 或 int。如何在 Sphinx 中指定集合或复合类型?或者说这方面没有共同标准?
def function(arg1, arg2, arg3):
"""
:param arg1: Argument 1
:type arg1: str
:param arg2: Argument 2
:type arg2: list[str]
:param arg3: Argument 3
:type arg3: str or int
"""
pass
Python 3.5 类型提示
虽然 Sphinx 尚未支持,但有一天 Sphinx 类型注释可能会过时。https://docs.python.org/3/library/typing.html https://docs.python.org/3/library/typing.html
目前,我建议使用与该模块完全相同的语法,这将:
- 使移植更容易,并可能在以后实现自动化
- 指定一种独特且定义明确的做事方式
Example:
def f(list_of_int):
"""
:type list_of_int: List[int]
:rtype: int
"""
return list_of_int[0] + list_of_int[1]
然后当你有 3.5 时,你会写:
def f(list_of_int : List[int]) -> int:
return list_of_int[0] + list_of_int[1]
The str or int
部分可以表示为Union
: 如何在 Sphinx 处理的文档字符串中表达单个参数或返回值的多种类型? https://stackoverflow.com/questions/34647966/how-to-express-multiple-types-for-a-single-parameter-or-a-return-value-in-docstr/40801906#40801906
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)