Type hints are optional annotations; enforcement is done by static type checkers, not at runtime
Type hints are annotations attached to variables, function parameters, and return values. They are stored in annotations and are not checked when the code runs. A function declared def add(a: int, b: int) -> int: still accepts strings at runtime. The value of type hints comes from static analysis tools like mypy, pyright, and pyre, which read the annotations and report inconsistencies before the code runs. They also power IDE autocompletion, refactoring, and documentation. Since Python 3.5 (PEP 484), the syntax has evolved, and from future import annotations (PEP 563) makes annotations lazy strings, which avoids evaluating them at definition time. The trade-off is that you must run a type checker in CI to get any benefit, and annotations add a small maintenance cost. The most common misconception is that Python enforces types at runtime; it does not. You can use libraries like pydantic or typeguard to add runtime validation, but that is separate from the language.
Annotations are metadata, not contracts. Python does not raise TypeError for a wrong argument type.
Static checkers (mypy, pyright) enforce types at development time, not at runtime.
from future import annotations postpones evaluation of annotations, making them strings and avoiding import cycles for type-only imports.
typing.get_type_hints resolves forward references and returns the evaluated annotations.
Trade-off: type hints improve safety and tooling but require discipline and a type checker in CI. Untyped code is still valid Python.
Common mistake: assuming that def f(x: int) -> int: raises on f('a'). It does not.
Common mistake: using mutable defaults in annotations or misunderstanding that Optional[X] means X or None, not an optional parameter.
Version note: PEP 484 (3.5) introduced the typing module. PEP 563 (3.7) added postponed evaluation via future. PEP 604 (3.10) added the | operator. PEP 585 (3.9) allowed built-in generics like list[int].
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience