Python theorytheory 0/50 · 0%
Types · medium

26. Type hints

Optional static type annotations checked by external tools.

Type hints document expected types without being enforced at runtime by the interpreter itself; tools like `mypy` check them statically.

python
from typing import Optional

def get_balance(address: str) -> float:
    return 0.0

def find_user(id: int) -> Optional[dict]:
    return None

Balances = dict[str, float]   # type alias (Python 3.9+)

Since Python 3.9, built-in generics (`list[int]`, `dict[str, int]`) work directly without importing from `typing`. Hints improve editor autocompletion and catch bugs before runtime, but Python remains dynamically typed regardless.

Check your understanding

  1. 1. Are type hints enforced at runtime by default?

  2. 2. What does `Optional[dict]` mean?

  3. 3. Since which version can you write `list[int]` without importing List?