A type hint indicates what type a variable is expected to be.
The type hints indicate that for our
def add(a: int, b: int) -> int: return a + b
addfunction the parameters
bshould be integers, and the function should return an integer when called.
It's important to note these are just hints and are not enforced at runtime.
The above code won't error even though it doesn't follow the function's type hints; the two strings will be concatenated as normal.
add("hello ", "world")
Third party tools like mypy can validate your code to ensure it is type hinted correctly. This can help you identify potentially buggy code, for example it would error on the second example as our
add function is not intended to concatenate strings.