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

时间:2014-12-16 19:26:41

标签: python python-sphinx docstring 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
    """
    ...

使用Sphinx' autodoc功能,这会生成参数列表,并且每个参数都会使用其类型进行正确标记。

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

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

    :type edge_indices: list of int
    """

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

1 个答案:

答案 0 :(得分:4)

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

class Foo:
    """
    :ivar edge_indices: description
    :vartype edge_indices: list of int
    """