Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 390
Torsten Bronger Avatar asked Dec 16 '14 19:12

Torsten Bronger


People also ask

Can docstrings be assigned to variables?

Yes, you can do that! You can actually 'document' lambdas and variables in a module by attaching docstrings to them.

What are the three types of docstrings?

Let us know the most commonly used docstring formats out there in the wild, which are namely- Google, NumPy, and Sphinx docstring formats.

How do you write a docstring in Python?

Declaring Docstrings: The docstrings are declared using ”'triple single quotes”' or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.


1 Answers

I had this same issue. The answer is vartype:

class Foo:
    """
    :ivar edge_indices: description
    :vartype edge_indices: list of int
    """
like image 94
Tim Avatar answered Oct 23 '22 00:10

Tim