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

Sho*_*kit 6 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
Run Code Online (Sandbox Code Playgroud)

Cir*_*四事件 4

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]
Run Code Online (Sandbox Code Playgroud)

然后当你有 3.5 时,你会写:

def f(list_of_int : List[int]) -> int:
    return list_of_int[0] + list_of_int[1]
Run Code Online (Sandbox Code Playgroud)

str or int部分可以用以下形式表示UnionHow toexpress multiple types for a single argument or a return value in docstrings that areprocessed by Sphinx?