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?
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.
Let us know the most commonly used docstring formats out there in the wild, which are namely- Google, NumPy, and Sphinx docstring formats.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With