Mypy on steroids

Mypy is a nifty static type checker for Python that will help you nip nasty production bugs in the bud. Especially when you're already in the habit of generously using type hints in your code. Head over to their docs to get started if you're not using Mypy already.

Mypy is kind of slow though. On a code base of 200 Python files it already takes 2 seconds to check the code. Luckily there's a quick fix! You can run Mypy in daemon mode, and it will (partially) cache previous run results and be up to 20 times faster 🚀

To speed things up, just change the Mypy command from mypy <arguments> <file(s)> like this:
mypy --check-untyped-defs hello_world.py 
To dmypy run -- <arguments> <file(s)> like this:
dmypy run -- --check-untyped-defs hello_world.py 
The first time you run the command in daemon mode, it will still be slow as it needs to generate the cached data. Subsequent calls will be much faster though. For the 200 file code base the run time went down from 2 seconds to just 100ms.


related blogs: