[Answered ]-Specifying output types for functions in python

1👍

Python doesn’t actually enforce typehints. They’re useful for documentation and linting. To enforce, you’ll need to check with a type checker like mypy.

# test.py
def foo(i: int) -> str:
    return str(i)

def bar(i: int) -> str:
    return None


print(foo(1), bar(1))

When I run python test.py I get 1 None

When I run mypy test.py I get this:

error: Incompatible return value type (got "None", expected "str")

Leave a comment