Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to denote return type tuple in Google-style Pydoc for Pycharm?

In Pycharm I want to have a documented function that returns a tuple so that I can get code completion on it. The style of comments is Google-style.


This works correctly:

def func():
    """

    Returns:
        str: something

    """
    pass

Typing func(). correctly shows methods for str.


However, typing this does not work anymore:

def func():
    """

    Returns:
        (int, str): something

    """
    pass

a, b = func()

Typing b. does not offer anything.


I know that PyCharm is capable of parsing tuples, because this code works:

def func():
    """

    :rtype: (int, str)
    """
    pass

a, b = func()

However, this is not according to the Google style.


How do I document a function according to the standard so that Pycharm will pick up on the return types?

like image 265
Martin Melka Avatar asked Dec 09 '16 07:12

Martin Melka


People also ask

What is Google style docstrings?

Docstrings may extend over multiple lines. Sections are created with a section header and a colon followed by a block of indented text. Example: Examples can be given using either the ``Example`` or ``Examples`` sections.

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 I use docstring in PyCharm?

Press Ctrl+Alt+S and go to Editor | General |Smart Keys. Select the Insert type placeholders checkbox in the Smart Keys page of the editor settings. Place the caret at the function name, and press Alt+Enter . In the list of intention actions that opens, choose Insert documentation string stub.


2 Answers

Here is how I'd do it:

from typing import Tuple

def func() -> Tuple[int, str]:
    """ (...)

    Returns:
        A tuple containing, respectively, an int (<meaning of the
        returned int>) and a string (<meaning of the returned string>).
    """

PyCharm (and Sphinx, if you're using it to parse your docstrings) will correctly know the return type of your function and you'll have a clean and nice description of the function's return value.

like image 187
Talendar Avatar answered Sep 19 '22 07:09

Talendar


Would the type hints as specified by PEP 484 not be valid?

Python 3 docs for typing module

Python PEP 484

from typing import Tuple

def func():
    """

    :rtype: Tuple[int, str]
    """
    pass

a, b = func()
like image 30
Scott P. Avatar answered Sep 22 '22 07:09

Scott P.