Basic Types
x: int = 5 | Integer |
name: str = 'hello' | String |
flag: bool = True | Boolean |
pi: float = 3.14 | Float |
data: bytes = b'hello' | Bytes |
x: None = None | None type |
Collections
items: list[int] | List of ints (Python 3.9+) |
mapping: dict[str, int] | Dict with typed keys/values |
pair: tuple[int, str] | Fixed-length tuple |
unique: set[str] | Set of strings |
point: tuple[float, ...] | Variable-length tuple |
Optional & Union
x: int | None | Optional (Python 3.10+) |
from typing import Optional; x: Optional[int] | Optional (older) |
val: int | str | float | Union type |
from typing import Union; val: Union[int, str] | Union (older) |
Callables & Special
from typing import Callable | Callable type |
fn: Callable[[int, str], bool] | Function signature |
from typing import Any | Any type (escape hatch) |
from typing import TypeVar; T = TypeVar('T') | Generic type variable |
from typing import Protocol | Structural subtyping |