Do Python function type annotations enforce types at runtime?
Answer
No. Annotations like `def add(a: int, b: int) -> int:` are purely informational to the interpreter — Python never checks, coerces or rejects arguments based on them at call time, so calling `add("2", "3")` runs without error and returns `"23"`. Since Python 3.14 (PEP 649/749), annotations are evaluated lazily by default rather than being computed and stored eagerly at function-definition time — the underlying expressions are evaluated on demand, through `__annotations__` or the `annotationlib` module, which avoids forward-reference problems and reduces import-time overhead compared to earlier versions. Type enforcement only happens if a separate static type checker such as mypy or pyright analyzes the code ahead of time, if the function explicitly validates its inputs itself at runtime, or if some other runtime consumer — like a web framework validating request payloads — chooses to read and act on the annotations itself.
Strong answer includes
- annotations are purely informational, never enforced or coerced at call time
- Python 3.14 evaluates annotations lazily by default (PEP 649/749), not eagerly at definition time
- enforcement requires a separate static checker, explicit runtime validation, or a runtime consumer that inspects the annotations
Practical example
def add(a: int, b: int) -> int: return a + bhappily executes
add("2", "3")and returns the concatenated string `"23"` , since string `+` is valid and the annotations were never checked.