QuickDevGuide

Home › Python

Type Hints — Python

Type hints document intent and let tools like mypy, pyright and your IDE catch bugs before runtime. Python itself does not enforce them. Syntax (3.10+) Hint Meaning int, str, list[int], dict[str, float] built-in generics (3.9+) int None optional value (3.10+, replaces Optional[int]) tuple[int, str] / tuple[int, ...] fixed / variable length Callable[[int, int], int] function signature Literal["r", "w"] specific values only TypedDict dict with known keys Protocol structural interface (duck typing) TypeVar / def fT -> T (3.12) generics Self (3.11) method returning its own class Runtime validation For untrusted input (APIs, config), use a validation library such as Pydantic, which turns type hints into runtime checks — FastAPI is built on it.

Key Takeaways