Type Hints — They Do Nothing at Runtime
Why Python type hints are just "hints" and how Pydantic overcomes this
def add(a: int, b: int) -> int:
return a + b
add('hello', 'world') # No error! Returns 'helloworld'
Python runtime completely ignores type hints. Stored in __annotations__ dict but never checked. A design decision — maintaining dynamic typing flexibility while adding tooling support.
So What Are They For?
- mypy/pyright — catches type mismatches before execution
- IDE autocomplete — VS Code shows methods for the type on
. - Documentation — readers immediately see arg/return types
How Pydantic Does Runtime Type Checking
Pydantic reads __annotations__, generates per-field validators, and validates on __init__. V2 runs validation logic in Rust (pydantic-core).
Python type hints do nothing by themselves, but Pydantic reads that "does-nothing" metadata to build runtime validation.
Key Points
Python runtime ignores type hints — only stored in __annotations__
mypy/pyright statically catches type mismatches
Pydantic reads __annotations__ to build runtime validation
FastAPI auto-validates request data types through Pydantic