ReviseAlgo Logo

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.

Last Updated: June 12, 2026 9 min read

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 None
  • Union[int, str] = int | str (3.10+) — one of several types
  • Any — opt-out of type checking (avoid when possible)
  • Callable[[ArgTypes], Return] — function type
  • TypeVar('T') — generic type variable

Collection Types

  • list[int] (3.9+) — was List[int]
  • dict[str, int] (3.9+) — was Dict[str, int]
  • tuple[int, str, float] — fixed-length heterogeneous tuple
  • tuple[int, ...] — variable-length homogeneous tuple
  • set[str], frozenset[int] — typed sets

Special Types

  • Literal["GET", "POST"] — restrict to specific values
  • Final[int] — value cannot be reassigned
  • TypeAlias (3.10+) — explicit type alias declaration
  • Never (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