Search code examples
pythonpython-sphinxdocstringautodoc

How does one specify a type for variables in Python docstrings for Sphinx?


You can specify types of parameters in Python docstrings like this:

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
    """
    ...

With Sphinx' autodoc feature, this yields the parameters list, and each parameter is properly tagged with their types.

But how do I do this with instance attributes? Something like this

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

    :type edge_indices: list of int
    """

does not work. One can put a single-word type after the :ivar but here, it consists of three words, so that would not work.


Solution

  • I had this same issue. The answer is vartype:

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