什么是数据结构类型(如列表)的Sphinx文档字符串标准?

时间:2015-11-02 16:36:43

标签: python python-sphinx

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

1 个答案:

答案 0 :(得分:3)

Python 3.5类型提示

虽然Sphinx尚未支持,但有一天可能会使Sphinx类型的注释过时。 https://docs.python.org/3/library/typing.html

目前,我建议使用与该模块完全相同的语法,它将:

  • 稍后在
  • 上使移植更容易,并且可能自动化
  • 指定一种独特的明确定义的方式

示例:

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]

str or int部分可以用UnionHow to express multiple types for a single parameter or a return value in docstrings that are processed by Sphinx?

表示