10

We can indicate the types of function parameters 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 generates the following document:

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

In python 3, the types can be indicated by type hint as well:

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

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

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

Would it be possible to generate the documentation as f1 using annotation instead of docstring? I'm using python 3.6. Thank you!

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Jinho Choi
  • 1,841
  • 2
  • 10
  • 12

1 Answers1

4

Not yet, from what I'm aware Sphinx yet doesn't support this. The bug referenced in a comment was about the representation of the type-hints and not their positioning.

I do know there's currently an extension for Sphinx that takes care of this for you though, called sphinx-autodoc-typehints. You could probably use that for the time being.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • Wonderful, for the time being, I'll use this plugin. Hopefully, this feature will be integrated natively to Sphinx. Thanks. – Jinho Choi Feb 16 '17 at 21:13
  • In case anyone also wants to use [Napoleon](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/), [this answer](https://stackoverflow.com/a/51312475/435253) has more details on how to make the two play nice together. – ssokolow Dec 29 '19 at 07:56