Command Palette

Search for a command to run...

Hectal
Phase 10Intermediate11 of 18 in Python

Type Hints and Modern Python

Label what your values are, let a checker catch mistakes before you run, read hints at run time, and write Python the way current versions intend.

Python never asks you to say what type a value is. That freedom is great for a ten-line script and painful for a ten-thousand-line codebase, where nobody remembers what a function expects. Type hints are labels you add so people, editors and checker programs can tell. This phase teaches the whole system: the labels, the checkers that read them, generics, shaped dictionaries, protocols, and what hints do (and do not do) while the program runs.

Type hints matter even more in the AI era. The same labels that a checker reads are read by Pydantic to validate data, and are turned into the JSON schemas that tell a language model which tools it may call and what shape its answers must take. A class with typed fields is both documentation and a contract.

The phase ends with pattern matching in depth and an upgrade guide: the old way and the modern way of writing the same thing, side by side, so you can read old code and write current code.

0/9 · 0%
9 topics ~4 h 32 code blocks & diagrams
Start with the first topic
1
10.1

Type Hints: What They Are and Aren't

A type hint is a label that says what kind of value a variable, parameter or return value is meant to be. Python stores the label but never enforces it while the program runs; editors and checker programs read it to catch mistakes early.

27 min 1 diagram 3 code practice

2
10.2

Typing Collections, Optional and Unions

Hints for containers say what is inside: list[int], dict[str, int], tuple[int, str]. X | None means "an X, or nothing", and Sequence, Mapping, Iterable and Callable describe what a function needs without demanding one exact class.

27 min 3 code practice

3
10.3

Running a Type Checker: mypy and pyright

A type checker reads your code without running it and reports every place where the hints contradict each other. mypy and pyright are the two most used; you install one, point it at your files, read its messages, and run it in CI so mistakes cannot be merged.

29 min 1 diagram 2 code practice

4
10.4

Generics and Type Variables

A generic function or class works for many types while keeping track of which: first([1, 2]) gives an int, first(['a']) gives a str. You declare the placeholder with TypeVar or, since Python 3.12, with the clean def f[T](x: T) and class Box[T] syntax.

27 min 3 code practice

5
10.5

TypedDict, NamedTuple, Literal and Final

These tools describe specific shapes: TypedDict labels the keys of a dictionary, NamedTuple gives tuple fields names, Literal allows only listed values, Final marks a constant, and Self, Annotated and @override cover methods that return their own class, extra metadata and overriding.

27 min 4 code practice

6
10.6

Protocols and Structural Typing

A Protocol describes the methods and attributes an object must have, and anything that has them qualifies, with no inheritance. It is duck typing that a checker can verify: "if it quacks like this, it fits".

26 min 1 diagram 2 code practice

7
10.7

Types at Run Time: Annotations, get_type_hints and Validation

Hints are ordinary data that Python keeps, so a program can read them: __annotations__, typing.get_type_hints, get_origin and get_args. Dataclasses and Pydantic do exactly this, and so you can write a tiny validator yourself.

27 min 4 code practice

8
10.8

Pattern Matching in Depth

match can take apart objects as well as values: class patterns pull out attributes, dataclasses work with positional patterns, and nested patterns, as, guards and assert_never make parsing commands, JSON events and typed unions short and safe.

27 min 3 code practice

9
10.9

Writing Modern Python: An Idiom Upgrade

Python has changed a lot since the code in old tutorials was written. This topic puts the old way and the modern way side by side, so you can read old code, write current code, and know what each recent version added.

34 min 1 diagram 4 code practice

Practise this phase

Hands-on exercises: write the code, run it, and press Check to have it tested automatically.