Type hints
Type hints are optional annotations that describe the intended types of parameters, returns, and variables. They document the contract for humans and checkers; the interpreter does not enforce them at runtime.
Detailed Explanation
def with_tax(cents: int) -> int: tells a reader, and a tool such as mypy or Pyright, that cents should be an integer and the function should return one. At runtime the annotation is stored on the function and ignored unless you install a validator. with_tax("1000") still runs and then fails inside the body, or worse, silently concatenates if you wrote + on strings.
Hints exist because Python is dynamically typed and large codebases still need a shared idea of what a function accepts. They are not a second language. They describe the types you already intended.
The syntax grew in layers. Built-in generics (list[int], dict[str, int]) work on 3.9+. On 3.8 you wrote List[int] from typing. Unions are int | None on 3.10+, or Optional[int] before. from __future__ import annotations postpones evaluation of annotations so you can forward-reference names and keep import cost down.
None as a return hint means the function should return None. Any turns checking off for that value. object means "I will not use any attributes". Prefer object to Any when you truly do not care. Prefer a union or a protocol when you do.
Variable annotations (name: str = "ada") declare a type without changing assignment. They are useful on class attributes and on empty containers: rows: list[str] = [] so a checker does not infer list[Never].
Hints cannot express every runtime fact. "This list is non-empty", "this dict has these keys" (use TypedDict), "this object has read()" (use Protocol), "this callable takes a User" (use Callable[[User], None]) are the extensions you reach for next.
Keep hints honest. list when you mean Sequence rejects tuples that would have worked. dict when you mean Mapping rejects a mapping view. Annotate the contract, not the concrete type you happened to construct inside the function.
Code Examples
def with_tax(cents: int, rate: float = 0.2) -> int:
return int(cents * (1 + rate))
print(with_tax(1000)) # 1200
print(with_tax.__annotations__) # {'cents': <class 'int'>, ...}from collections.abc import Sequence
def first(items: Sequence[str]) -> str | None:
return items[0] if items else None
print(first(("ada", "linus"))) # ada <- tuple is a Sequence# Runtime does not check. A checker would flag this call.
def parse_cents(raw: str) -> int:
return int(raw)
print(parse_cents("2500")) # 2500
# parse_cents(2500) # works at runtime, fails a type checkCommon Gotchas
Hints are not runtime checks
a wrong argument is still a wrong argument. Add validation at the boundary (Pydantic, marshmallow, hand-written checks) if you need enforcement.
`list[int] = []` on a class attribute is shared
the annotation does not create a per-instance list. Set it in `__init__`.
Circular imports from typing only
use `from __future__ import annotations` and `if TYPE_CHECKING:` imports for types you do not need at runtime.
`None` means `None`, not "no hint"
omit the return annotation if you do not want to claim a type. `-> None` is a claim.