Hynek Schlawack recently
described
graduality as Python’s super power: the ability to prototype in
the REPL, and gradually add linting, type checking, and other
practices to refine your code into maintainable, production-ready
software. You can also apply graduality within tools, activating
checks one at a time and fixing the resulting errors as you go.
One place you can apply graduality with Mypy is in the type hints
for third party packages. The default course of action is to add
type hints for a whole package at once. You do this either through
the package itself if it has a py.typed
file, or with a *-stubs
package (both specified in PEP
561). But when you add full
hints for a package used widely in your code base, you might find
yourself facing an insurmountable mountain of errors.
Instead, you can gradually add type hints for the package, piece by
piece, fixing small batches of errors as you go. This iterative
approach is more psychologically pleasing, and it reduces the chance
for mistakes.