core Estimated learning time: 4 h

2.13 Type hints, mypy and Pydantic

Your editor catches errors before your user does.

Before:00. Orientation & SetupUnlocks:03. Data Handling & Analysis04. Classical AI — Agents, Search & Knowledge Representation

Type hints turn the editor into a proof-reader and make signatures self-documenting; Pydantic extends the same idea to runtime validation of external data, which is exactly where hints alone cannot help. Together they catch a class of bugs before execution. It sits in the core run because the codebases ahead are annotated. The boundary to keep straight: annotations are not enforced at runtime — a hint is a promise to the reader and the checker, not a guard, and Pydantic exists for the places a guard is needed.

Work through these

  • Basic annotations, Optional, Union, Literal

    Annotations that say what a function expects and returns, including optional values, alternatives and specific literal choices. They are checked by tools rather than by Python itself.

  • Generics, TypeVar, Protocol

    Annotations that work for any type, and a way to state required behaviour without inheritance. These are what make type hints usable in library code.

  • Running mypy in CI

    Running the type checker automatically on every change turns hints from documentation into a gate. This is where the effort starts paying back.

  • Pydantic models for runtime validation

    Type hints are not enforced at run time, and a validation library fills that gap by checking data as it arrives. It is the standard answer for data coming in from outside.

Sign in to keep your progress.

Free resources

Links last checked 29 Aug 2026.

Stuck here?

Ask a mentor. A real person answers, and they can see exactly which topic you're on. Usually within a couple of working days.

Checking your session…

Topics shown in module order.