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

  1. Running pyright in strict mode — the type checker most AI teams use
  2. Generic typeslist[int], dict[str, float], tuple[int, str]
  3. Optional valuesX | None and how to narrow them
  4. Protocols (structural typing) — duck typing with a type checker
  5. 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.

Toggle theme (T)