Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type annotations with google style docstrings

Tags:

python

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.

like image 791
fishstix44 Avatar asked Sep 10 '25 19:09

fishstix44


1 Answers

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.

    """
like image 55
Kris Gesling Avatar answered Sep 12 '25 09:09

Kris Gesling