类型提示指南

Basic Types

x: int = 5Integer
name: str = 'hello'String
flag: bool = TrueBoolean
pi: float = 3.14Float
data: bytes = b'hello'Bytes
x: None = NoneNone 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 | NoneOptional (Python 3.10+)
from typing import Optional; x: Optional[int]Optional (older)
val: int | str | floatUnion type
from typing import Union; val: Union[int, str]Union (older)

Callables & Special

from typing import CallableCallable type
fn: Callable[[int, str], bool]Function signature
from typing import AnyAny type (escape hatch)
from typing import TypeVar; T = TypeVar('T')Generic type variable
from typing import ProtocolStructural subtyping