Sphinx reST Python文档字符串中是否有用于yield的字段?
我正在尝试使用reST风格的文档字符串,也就是下面这样的格式:
def foo(bar):
"""a method that takes a bar
:param bar: a Bar instance
:type bar: Bar
请问有没有标准的方法来记录yields
?我查看了这个链接 http://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#info-field-lists,还有这个问题 [ https://stackoverflow.com/questions/5334531/python-documentation-standard-for-docstring ],但是没有找到合适的答案。我想象的格式是这样的:
:yields: transformed bars
:yield type: Baz
2 个回答
5
我看过其他的回答,但我觉得没有回答到问题的核心。
虽然不是最好的方法,但记录一个生成器的方式是使用 :return
,就像文档中的其他部分一样。可以在描述中说明它是一个生成器。
在Google/Numpy风格的文档中,生成的值会被转换成返回值的描述。
12
Python 3.5 的 Iterator[]
注解
他们提供了一种标准化的 Iterator[]
语法,具体内容可以在这里查看:https://docs.python.org/3/library/typing.html#typing.Generator
在 Python 3 之前,我建议你使用这种语法,这样以后迁移代码会更简单:
from typing import List
def f():
"""
:rtype: Iterator[:class:`SomeClass`]
"""
yield SomeClass()
在 Python 3 之后,可以使用 https://pypi.python.org/pypi/sphinx-autodoc-annotation,语法如下:
from typing import Iterator
def f() -> Iterator[SomeClass]:
yield SomeClass()