Practical tips for introducing mypy to existing Python projects. Covers ensuring mypy is actually running and finding all source files, where to manually supply types (function arguments and return types), the importance of Optional for nullable values, whether to include tests in type checking, handling third-party stubs for libraries like SQLAlchemy and boto, escape hatches like typing.cast and type: ignore, recommended mypy.ini configuration options, debugging type errors with reveal_type and reveal_locals, choosing abstract vs concrete types for arguments and return values, using typed dataclasses and TypedDict, generics and type variables, and the difference between nominal subtyping (ABCs) and structural subtyping (Protocols).
Table of contents
First ensure that mypy really is being runWhere to supply types manuallyOptional comes up a lotConsider whether to include your testsBe selective about the usage of third-party stubsEscape hatches are occasionally necessaryPrioritise a few choice strictness optionsHow to debug type issuesAbstract, concrete, mutable and immutableTyped dataclassesTypedDictGenerics and type variablesABCs vs ProtocolsThe final tipContact/etcSee alsoSort: