Type Hints
- Annotations. A hint like def total(items: list[Order]) -> Decimal documents intent that tools can actually check.
- Optional and unions. The form str | None says a value may be missing: and the checker forces you to handle the None branch.
- Hints are not enforcement. Hints do nothing at runtime by default: their power comes from static analysis reading them.
Generics and Protocols
- Generics. A generic like list[str] or dict[str, int] names what is inside the container, not just the container.
- TypeVar. A TypeVar links types across a signature: first(items: list[T]) -> T promises it returns the same kind it took.
- Protocols. A Protocol declares structural typing: anything with the right methods matches, duck typing made checkable.
Static Type Checking
- Running a checker. A checker like mypy reads hints across the whole codebase and reports contradictions before anything runs.
- Gradual typing. Typing is gradual: annotate the core and the boundaries first, and let coverage grow with the code.
- Types as design pressure. Hard-to-type code is usually hard-to-reason code: the checker’s complaints are design feedback.
Before and after this level
- Before: Level 9: Iteration & Pythonic Design: Iterators and Generators, Context Managers, Decorators and Reusable Behaviour.
- Next: Level 11: Concurrency: Threads and Processes, asyncio and Async Programming, Choosing the Right Concurrency Model.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Python Level 10: Types & Modern Python cover?
Type Hints, Generics and Protocols, Static Type Checking: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 9: Iteration & Pythonic Design, which covers Iterators and Generators, Context Managers, Decorators and Reusable Behaviour.