What Are Type Hints in Python?
Type hints are optional annotations that document the expected types of variables, function parameters, and return values, without being enforced by the inte...
Published November 21, 2026
Type hints are optional annotations that state the expected types of variables, parameters and return values, like def greet(name: str) -> str. Python ignores them at runtime, but editors and tools such as mypy use them to catch bugs.
The concept
Hints improve documentation, autocomplete and static checking. They are not enforced when the code runs.
The typing module offers List, Dict, Optional, Union and more; Python 3.9+ lets you use built-ins like list[int] and 3.10+ the X | None syntax.
Checker finds it before runtime
mypy app.py
app.py:5: error: Argument 1 to "greet" has incompatible type "int"; expected "str"
Practical examples
Functions
def add(a: int, b: int = 0) -> int:
return a + bCollections and optionals
def find(users: list[dict[str, str]], name: str) -> dict[str, str] | None:
return next((u for u in users if u['name'] == name), None)Dataclass
from dataclasses import dataclass
@dataclass
class User:
id: int
name: strRun the checker
pip install mypy
mypy src/Common mistakes
- Believing hints are enforced at runtime.
- Using Any everywhere.
- Forgetting Optional for values that can be None.
Key takeaways
- Optional, static-analysis aid.
- Use built-in generics in modern Python.
- Run mypy or pyright.
FAQ
Do hints slow code?
No, they are ignored at runtime.
Runtime validation?
Use pydantic or beartype if you need enforcement.