(TIL) Python: MyPy variable annotations

less than 1 minute read

In Python 3.6, variables (in global, class or local scope) can now have type annotations using either of the following two forms:

foo: Optional[int]
bar: List[str] = []

Mypy fully supports this syntax, interpreting them as equivalent to:

foo = None  # type: Optional[int]
bar = []  # type: List[str]

Via MyPy .