How to specify type of variables in Python docstrings for Sphinx?

You can specify parameter types in Python docstrings as follows:

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

Using the autodoc Sphinx function, this gives a list of parameters, and each parameter is correctly labeled with their types.

But how to do this with instance attributes? Something like that

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

does not work. After :ivar you can put a single-word type, but here it consists of three words, so this will not work.

+6
source share
2 answers

I had the same problem. vartype answer:

 class Foo: """ :ivar edge_indices: description :vartype edge_indices: list of int """ 
+2
source

Read http://www.sphinx-doc.org/en/stable/domains.html#info-field-lists and find the variables, attributes are variables from the Sphinx point of view.

My impression is that you will have to document them at the class level instead of the method level.

0
source

Source: https://habr.com/ru/post/979728/


All Articles