如何为Sphinx的Python文档字符串中的变量指定类型?

Tor*_*ger 8 python docstring python-sphinx autodoc

您可以在Python文档字符串中指定参数类型,如下所示:

def __init__(self, canvas, segments):
    """Class constructor.

    :param canvas: the PDF canvas object
    :param segment: The layer segments to be drawn.

    :type canvas: `canvas.Canvas`
    :type segments: list of str
    """
    ...
Run Code Online (Sandbox Code Playgroud)

使用Sphinx的autodoc功能,可以生成参数列表,并且每个参数都可以使用其类型进行正确标记.

但是如何使用实例属性执行此操作?像这样的东西

class Path(object):
    """
    :ivar edge_indices: The indices within `textured_points` of the places.

    :type edge_indices: list of int
    """
Run Code Online (Sandbox Code Playgroud)

不起作用.人们可以在单词之后加上单词类型,:ivar但在这里,它由三个单词组成,因此不起作用.

Tim*_*Tim 6

我有同样的问题。答案是vartype

class Foo:
    """
    :ivar edge_indices: description
    :vartype edge_indices: list of int
    """
Run Code Online (Sandbox Code Playgroud)