Python - enforcing type hints with mypy


Python tip:

To enforce type hints, you can use mypy, a static type checker for Python.

For example, if we use the List[float] type hint for the function parameter and then provide a dictionary in a function call:

def all_miles_runned(runs: List[float]) -> float:
    return sum(runs)

print(all_miles_runned({1653476791: 5, 1653908791: 7.2, 1654081591: 8.3}))


"""
with mypy

$ pip install mypy
$ python -m mypy miles_runned.py
miles_runned.py:11: error: 
    Argument 1 to "all_miles_runned" has incompatible type "Dict[int, float]"; 
    expected "List[float]"
"""