🏷️

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?

  1. mypy/pyright — catches type mismatches before execution
  2. IDE autocomplete — VS Code shows methods for the type on .
  3. 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

1

Python runtime ignores type hints — only stored in __annotations__

2

mypy/pyright statically catches type mismatches

3

Pydantic reads __annotations__ to build runtime validation

4

FastAPI auto-validates request data types through Pydantic

Use Cases

mypy in CI — catch type errors before deployment FastAPI + Pydantic — runtime request/response type validation