Type Hints & Annotations
typing Module
The typing module — Optional, Union, Any, Callable, and modern equivalents in Python 3.9+ and 3.10+ for complex type annotations.
Interview: Essential for writing production-quality Python — know both old and modern typing syntax.
The typing module provides types for complex annotations that go beyond simple int/str. While Python 3.9+ and 3.10+ reduced the need for many typing imports, understanding both old and modern syntax is essential for working with different Python versions.
Essential Types
Optional[T]=T | None(3.10+) — value or NoneUnion[int, str]=int | str(3.10+) — one of several typesAny— opt-out of type checking (avoid when possible)Callable[[ArgTypes], Return]— function typeTypeVar('T')— generic type variable
Collection Types
list[int](3.9+) — wasList[int]dict[str, int](3.9+) — wasDict[str, int]tuple[int, str, float]— fixed-length heterogeneous tupletuple[int, ...]— variable-length homogeneous tupleset[str],frozenset[int]— typed sets
Special Types
Literal["GET", "POST"]— restrict to specific valuesFinal[int]— value cannot be reassignedTypeAlias(3.10+) — explicit type alias declarationNever(3.11+) — function never returns (always raises)Self(3.11+) — return type is the class itself
Interview Insight
Know the difference between old (typing.List) and new (list) syntax. Optional[X] and X | None are equivalent. Literal types are great for restricting string parameters to known values.
Use Cases
API design — clear function signatures with Optional, Union, and Literal
Data validation — annotating expected data structures in pipelines
Generic programming — TypeVar for reusable container classes
Configuration — Final for constants that should not be reassigned
Legacy code migration — gradually adding types to existing codebases
Common Mistakes
Using typing.List/Dict in Python 3.9+ — use built-in list/dict directly
Overusing Any — defeats the purpose of type checking entirely
Forgetting Optional for values that can be None — most common mypy error
Not knowing X | Y syntax (3.10+) — still use Union in older codebases
Confusing runtime types with static types — isinstance checks are separate from annotations