1 / 11
Learn to write typed Python that tools can check: annotations, None and unions, collections and TypedDict, Literal and enums, typed dataclasses and money as Decimal, NewType, narrowing with TypeGuard and TypeIs, Protocols, generics and the Python 3.12 PEP 695 syntax, ParamSpec decorators…
Type Hints, mypy, Protocols, Generics, and Data Validation with Pydantic
Read before you buy: the PDF here is a free sample — the first 20 pages of the 557-page book (payments, sales and reviews on this demo site are sample data). Get the full e-book at thenexbooks on Gumroad.
Turn "it works on my machine" Python into code whose shape you can trust — checked before it runs and validated when data arrives.
Python does not make you declare types, and that freedom is wonderful until a codebase grows. Can this argument be None? Is this amount a float, a Decimal, or a string straight from a CSV file? Type hints let you write the answers down where tools can check them. mypy reads them and reports mistakes before the program runs; Pydantic uses the same hints to validate the data that arrives from files, environment variables, and other systems.
This book teaches typed Python from the first annotation to a strictly checked, validated service. Instead of listing the typing module, you build one real program step by step, run the checker, read its actual error messages, and fix them — so you learn how to type your own code, not just what the syntax looks like.
What's inside
- Why type hints matter, what they do and do not do at runtime, and setting up Python 3.12, mypy, and Ruff for a project
- Annotating functions and variables, handling None safely with unions and narrowing, and typing lists, dicts, tuples, and TypedDict
- Modeling a domain with Literal, enums, Final, typed dataclasses, NewType IDs, type aliases, and money as Decimal with explicit rounding
- Typing classes: ClassVar, Self, @override, abstract classes, and the inheritance rules mypy enforces
- Type narrowing with isinstance, match, assert_never, TypeGuard, and TypeIs
- Protocols for structural typing, generics with TypeVar, the new Python 3.12 generic syntax and type statement, and variance explained clearly
- Callables, ParamSpec, and signature-preserving decorators, plus overloads, Any vs object, Never, cast, and type: ignore used responsibly
- mypy in practice: strict mode, per-module configuration, error codes, stubs and py.typed, untyped libraries, and a step-by-step migration of a legacy module
- Typing generators, context managers, and async code, and a field guide to the mypy errors you will meet most
- Pydantic v2 from the ground up: models, lax and strict modes, constraints, field and model validators, custom types, serialization, JSON Schema, TypeAdapter, and settings
- Validating real input: CSV files with row-level error reports, JSON payloads, discriminated unions, and TOML and YAML config files
- Types in production: annotations at runtime, honest performance measurement, team conventions, tests for types, and CI with mypy and Ruff
One running example, start to finish. You type and validate Wrenhall Payroll, the payroll and invoicing service of a fictional small consultancy: four employees, two clients, weekly timesheets, overtime, withholding and pension deductions, payslips, and client invoices with sales tax — with money as Decimal throughout. It starts as a few loosely typed functions and ends as a package that passes mypy --strict, validates every file it reads with Pydantic, and has tests and CI checks that keep it that way. The data is small enough to check by hand, and every output and every mypy message in the book comes from actually running the code.
Current and honest. The book targets Python 3.12 with a recent mypy and Pydantic v2, marks features from Python 3.13 and 3.14 clearly, and shows how to use some of them today through typing_extensions. It contains no invented benchmarks or statistics. Every chapter includes runnable code, real checker output, Tips, Notes, Warnings, exercises, and Key Takeaways, and the appendices give you a typing cheat sheet, templates and reference tables, a production checklist, and a glossary.
Format: PDF, 557 pages (A4), linked table of contents and bookmarks. English. First Edition, 2026. Published by thenexbooks. For Python developers who already know the language basics. This book is independent and is not affiliated with, sponsored by, or endorsed by the Python Software Foundation or the mypy, Pydantic, or Ruff projects.
Table of Contents
Part I — Foundations of Typed Python
- Ch 1 · Why Type Hints? Static Types in a Dynamic Language
- Ch 2 · Setting Up: Python 3.12, mypy, Ruff, and Your Editor
- Ch 3 · Annotating Variables and Functions
- Ch 4 · None, Optional, and Union: Handling Absence Safely
- Ch 5 · Collections, Tuples, and TypedDict
Part II — Modeling the Domain with Types
- Ch 6 · Literal, Enum, and Final: Constraining Values
- Ch 7 · Typed Dataclasses: Records, Value Objects, and Money as Decimal
- Ch 8 · NewType, Type Aliases, and Domain Types
- Ch 9 · Typing Classes: Inheritance, ClassVar, Self, and @override
- Ch 10 · Type Narrowing: isinstance, match, TypeGuard, and TypeIs
Part III — Advanced Static Typing
- Ch 11 · Protocols: Structural Typing and Duck Typing Done Right
- Ch 12 · Generics and TypeVar: Writing Reusable Typed Code
- Ch 13 · Python 3.12 Generic Syntax: PEP 695 and the type Statement
- Ch 14 · Callables, ParamSpec, and Typed Decorators
- Ch 15 · Overloads, Any, object, Never, and cast: Precision and Escape Hatches
Part IV — mypy in Practice
- Ch 16 · Configuring mypy: Strict Mode, Per-Module Settings, and Error Codes
- Ch 17 · Typing Third-Party Code: Stubs, py.typed, and typeshed
- Ch 18 · Gradual Typing a Legacy Codebase
- Ch 19 · Typing Iterators, Generators, Context Managers, and Async Code
- Ch 20 · Reading and Fixing mypy Errors: A Field Guide
Part V — Data Validation with Pydantic
- Ch 21 · Pydantic v2 Models: Types That Validate at Runtime
- Ch 22 · Validators, Constraints, and Custom Types
- Ch 23 · Serialization, JSON Schema, and Settings
- Ch 24 · Validating Real Input: CSV Files, JSON APIs, and Config Files
Part VI — Types in Production
- Ch 25 · Types at Runtime: Annotations, Introspection, and Runtime Checks
- Ch 26 · Performance: What Types and Validation Really Cost
- Ch 27 · Team Conventions and CI: mypy, Ruff, Tests, and Type Coverage
- Ch 28 · Case Study: The Fully Typed Wrenhall Payroll Service
Appendices
A · Typing Cheat Sheet
B · Templates and Reference
C · Production Checklist
D · Glossary
Book details
| Detail | |
|---|---|
| Pages | 557 |
| Format | PDF (A4) |
| Language | English |
| Level | Intermediate |
| Edition | 1st Edition (2026) |
| Download on this site | Free sample, first 20 pages (PDF) |
| Publisher | thenexbooks |
Files you get
No reviews yet. Only buyers can review.
More from thenexbooks