Learn typing

Type hints for Python, checked by mypy and pyright, from basics to generics

16 lessons · 3 hours · 4 levels · covers typing Python 3.13

The typing module and Python's annotation syntax let you describe the types your code expects, which static checkers such as mypy and pyright verify before the code runs. Hints power editor autocompletion and refactoring, document intent, and drive runtime tools like FastAPI and Pydantic. Learning typing well means catching bugs earlier and writing APIs that are clear to use.

Before you start

typing builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 46 min in total
2. PracticalUse it productively at work, with the usual idioms10 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs13 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know16 lessons · 3 hours in total

The typing tree

typingAnnotations and type checkersAnnotations and typecheckersTyping collectionsTyping collectionsUnions, Optional and narrowingUnions, Optional andnarrowingAny, object and gradual typingAny, object andgradual typingTyping functions and callbacksTyping functions andcallbacksTypedDict for dict-shaped dataTypedDict fordict-shaped dataLiteral, Final and enumsLiteral, Final andenumsProtocols: structural typingProtocols: structuraltypingGeneric functions and classesGeneric functionsand classesType aliases and NewTypeType aliases andNewTypeDefining functionsDefining functionsListsListsDictionariesDictionariesClasses and instancesClasses andinstances
Essentials and Practical shown (10 of 16 lessons); the full list is below. Click a lesson to read it.

All 16 lessons, in order

Annotation basics

  1. Annotations and type checkers · Essentials · 9 min

    Annotate variables, parameters and return values, and let a static checker like mypy or pyright verify them.

  2. Typing collections · Essentials · 9 min

    Annotate lists, dicts, sets and tuples with built-in generics, and accept abstract types like Iterable and Mapping.

  3. Unions, Optional and narrowing · Essentials · 10 min

    Write X | None and other unions, and narrow them with checks the type checker understands.

  4. Any, object and gradual typing · Essentials · 9 min

    Know the difference between Any (unchecked) and object (anything, safely), and when to use cast.

  5. Typing functions and callbacks · Essentials · 9 min

    Annotate callback parameters with Callable[[Args], Return] and understand when to reach for a Protocol instead.

Describing data shapes

  1. TypedDict for dict-shaped data · Practical · 11 min

    Describe JSON-like dictionaries with fixed keys using TypedDict, NotRequired and ReadOnly.

  2. Literal, Final and enums · Practical · 9 min

    Restrict values to exact constants with Literal, mark constants with Final, and know when an Enum is better.

  3. Protocols: structural typing · Practical · 12 min

    Type 'anything with these methods' using Protocol, the static version of duck typing.

Generics and reusable types

  1. Generic functions and classes · Practical · 13 min

    Write reusable typed code with type parameters using the Python 3.12 syntax def f[T](...) and class Box[T].

  2. Type aliases and NewType · Practical · 8 min

    Name complex types with the type statement, and create distinct ID types with NewType.

Advanced type-level tools

  1. Overloaded signatures · Advanced · 11 min

    Describe functions whose return type depends on argument types or values with @overload.

  2. Custom narrowing and exhaustiveness · Advanced · 12 min

    Write TypeIs and TypeGuard functions, and use Never with assert_never to make the checker verify every case is handled.

  3. Typing decorators: ParamSpec and Self · Advanced · 13 min

    Preserve wrapped function signatures in decorators with ParamSpec, and type fluent methods with Self.

Type system theory and runtime

  1. Variance: why list[int] isn't list[float] · Expert · 13 min

    Understand covariance, contravariance and invariance, and how they decide which generic types are compatible.

  2. Annotations at runtime · Expert · 12 min

    Inspect hints with get_type_hints, handle forward references, and know how evaluation of annotations works.

  3. Variadic generics and typed **kwargs · Expert · 13 min

    Use TypeVarTuple for shape-like generics, Unpack[TypedDict] for keyword arguments, and type parameter defaults.