...

/

Type Hinting

Type Hinting

Learn about typing in Python and use MyPy to fix typing in suggested functions.

Overview

Strong static typing catches many errors that can be missed in a dynamic typing environment. Python’s type hinting can slightly reduce the need for runtime checks. It also improves code understanding for other engineers.

Let’s consider an example below. First, we’ll execute the code below to see if it works successfully or not with the broken type annotation.

Press + to interact
from typing import List
def sum_odds(numbers: List[int]):
return sum(x for x in numbers if x % 2)
print(sum_odds([1, 2., 3, 4]))

The above example works fine, but its type annotation is broken. Let's verify it by running the same code with the mypy command.

Press + to interact
from typing import List
def sum_odds(numbers: List[int]):
return sum(x for x in numbers if x % 2)
print(sum_odds([1, 2., 3, 4]))

Python’s typing

...
Access this course and 1400+ top-rated courses and projects.