Can I type a hint in python 3 to create a docstring?

We can specify function parameter types using docstring in python:

def f1(a): """ :param a: an input. :type a: int :return: the input integer. :rtype: int """ return a 

For f1 , autodoc creates the following document:

 fun1(a) Parameters : a (int) – an input. Returns : the input integer. Return type: int 

In python 3, types can also be specified using type hints:

 def f2(a: int): """ :param a: an input. :return: the input integer. :rtype: int """ return a 

When we run autodoc, it puts the type in the parameter declaration, but not in the description:

 f2(a: int) Parameters : a – an input. Returns : the input integer. Return type: int 

Is it possible to create documentation as f1 using annotation instead of docstring? I am using python 3.6. Thanks!

+6
source share
1 answer

So far, from what I know, Sphinx does not yet support this. The error that was mentioned in the comment concerned the presentation of type hints, and not their positioning.

Currently, I know that the extension for Sphinx that will take care of this for you is called sphinx-autodoc-typehints . You can probably use this now.

+1
source

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


All Articles