Recipe
Python Typing Primer
Ship safer Python with type hints — no runtime cost, all editor power.
Why Type Hints
Type hints are optional annotations that tools like mypy, Pyright, and your editor use to catch bugs before they hit production. They are ignored at runtime — zero performance penalty.
Basic Syntax
def greet(name: str) -> str:
return f"Hello, {name}"
age: int = 42
scores: list[float] = [95.5, 88.0]Common Types
int,float,str,boollist[T],dict[K, V],tuple[T, ...]Optional[T]— shorthandT | NoneUnion[A, B]— shorthandA | BCallable[[Args], Ret]for function signatures
TypedDict & Dataclasses
from typing import TypedDict
class User(TypedDict):
id: int
email: str
verified: boolStrict Mode
Add # pyright: strict at the top of a file to enforce full coverage. Run mypy --strict in CI to block untyped code from merging.
Pro tip: Use reveal_type(x) in mypy to inspect inferred types during development.