What is the Sphinx docstring standard for data structure types such as lists?

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

Does Sphinx have a supported standard for documenting the arguments or return value types that are not a simple, single object?

For instance, in the following, arg1 is a str, arg2 is a list of str, and arg3 is either a str or int. How can you specify collection or composite types in Sphinx? Or is there no common standard for this?

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:

Python 3.5 type hints, while not yet supported by Sphinx, will likely make Sphinx type annotations obsolete one day. https://docs.python.org/3/library/typing.html

For now, I recommend using the exact same syntax as that module, which will:

  • make porting easier, and possibly automatable, later on
  • specifies a unique well defined way to do things

Example:

def f(list_of_int):     """     :type list_of_int: List[int]     :rtype: int     """     return list_of_int[0] + list_of_int[1]

Then when you have 3.5, you will write just:

def f(list_of_int : List[int]) -> int:     return list_of_int[0] + list_of_int[1]

The str or int part can be expressed with Union: How to express multiple types for a single parameter or a return value in docstrings that are processed by Sphinx?



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!