Search code examples
pythonpycharmtuplestype-hintingnumpydoc

How to document multiple return values in Numpydoc format?


I am trying to document a tuple return value using numpy docstring format, but can't make it work with pycharm type hinting.

I have tried multiple ways, and even found one that was working for the type, but did not allow me to add a description for each of its elements.

Example of a function to document:

def function():
    foo = 42
    bar = {
        example : 1337,
        dictionary : 46,
    }
    return foo, bar

Now, one way I could go about to document it is:

def function():
    """
    This is the function summary.

    Returns
    -------
    foobar : tuple[int,[dict[string, int]]
        This is a description of the return type
    """
    foo = 42
    bar = {
        'example' : 1337,
        'dictionary' : 46,
    }
    return foo, bar

This would grant me a description and the right return type hinting, but not individual descriptions for each elements, which I would like.

Here is a non-working example of what I am trying to achieve:

def function():
    """
    This is the function summary.

    Returns
    -------
    foo : int
        This is an int
    bar : [dict[string, int]
        This is a dictionary
    """
    foo = 42
    bar = {
        'example' : 1337,
        'dictionary' : 46,
    }
    return foo, bar

Solution

  • If function return value is annotated as tuple[int, dict[string, int]], its documentation is rendered correctly, but there is an issue with inferring type for function()[1]["key"]. Feel free to file an issue in public PyCharm tracker https://youtrack.jetbrains.com/issues/PY.