I would like to enforce type hinting everywhere in my Python package and configure CI to fail builds in case it's not 100%.
Is there a way for me to report type hint coverage using mypy or any other package in Python similarly to how I can check code coverage with e.g. coverage.py?
Is it also sensible to require a strict 100% of type hint coverage?
Is there a way for me to report type hint coverage using mypy or any other package in Python similarly to how I can check code coverage with e.g. coverage.py?
Yes, there are a lot of flags for disallowing various uses of Any
, as well as missing type annotations. Or you can just pass the --strict
flag to disallow a large number of questionable constructs with a single flag. The precise definition of --strict
may change over time, so it might be a poor fit for CI (i.e. "Why is the build broken, all I did was install a new mypy
?"). There are also a number of flags for generating coverage reports.
Is it also sensible to require a strict 100% of type hint coverage?
This is going to greatly depend on what you want to accomplish with type hinting, as well as the history and current state of your codebase. For example, if you and all of your fellow developers come from a static typing background (i.e. you previously programmed in statically typed languages such as C++ and Java), and you don't have a lot of old Python code to support, then this is likely to work well. On the other hand, if you have a large volume of legacy code (perhaps converted over from Python 2 at the last minute), or if many of your developers are accustomed to dynamic typing, then there may be more of an impedance mismatch. If you're not sure if this will work for you, I would suggest setting up separate CI builds with and without strictness. If the strict one is constantly red while the non-strict one is constantly green, you may have either a technical or cultural problem to overcome before enabling mandatory type hints.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With