Type Annotations in Python
From the Python cheat sheet ยท Type Hints ยท verified Jul 2026
Type Annotations
Annotate function parameters, return types, and variables.
python
# Function annotations
def greet(name: str, age: int = 0) -> str:
return f"Hello {name}, age {age}"
# Variable annotations
count: int = 0
names: list[str] = ["Alice", "Bob"]
config: dict[str, int] = {"port": 8080}๐ก Type hints are not enforced at runtime โ they're for documentation and tooling
โก Use int | str (3.10+) instead of Union[int, str] โ it's cleaner
๐ Use list[str] directly (3.9+) instead of importing List from typing
๐ข Run mypy or pyright to check types statically: mypy app.py
typestype-hintsannotations