列表等数据结构类型的Sphinx文档字符串标准是什么? [英] What is the Sphinx docstring standard for data structure types such as lists?
本文介绍了列表等数据结构类型的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
推荐答案
类型提示
虽然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
部分可以用Union
:How to express multiple types for a single parameter or a return value in docstrings that are processed by Sphinx?
这篇关于列表等数据结构类型的Sphinx文档字符串标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文