When using google style docstrings and type annotations there's a double up of the type hints.
Is there any community consensus on how to avoid this?
Annoying double up of types:
def sum(a: int, b: int) -> int:
""" blah blah
Args:
a (int): value 1
b (int): value 2
Returns:
int: summed values
"""
return a + b
Should types be left out of the docstring like this?
def sum(a: int, b: int) -> int:
""" blah blah
Args:
a: value 1
b: value 2
Returns:
summed values
"""
return a + b
I haven't seen anyone else mention this, but I can't be the only one unsure about the best approach here.
Yes, you only need the type hints OR the annotations in the Args and Returns, not both.
References
According to the Google Python Style Guide: "The description should include required type(s) if the code does not contain a corresponding type annotation."
The Sphinx Docs also encourage this in their example code:
def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
"""Example function with PEP 484 type annotations.
Args:
param1: The first parameter.
param2: The second parameter.
Returns:
The return value. True for success, False otherwise.
"""
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