You’ve been adding type hints since Section 5. This section is where we use them properly — running a real type checker over our code, writing reusable generic functions, and using Protocols to describe shapes without inheritance.
A type-checked Python codebase catches bugs that tests miss. For AI and ML projects, where a wrong tensor shape can cost hours of training, that matters.
What’s in this section
- Running
pyrightin strict mode — the type checker most AI teams use - Generic types —
list[int],dict[str, float],tuple[int, str] - Optional values —
X | Noneand how to narrow them - Protocols (structural typing) — duck typing with a type checker
- A note on abstract base classes and multiple inheritance — what they are, when to skip them
By the end, you’ll write Python that’s safer to refactor and easier to read.