Adding Static Type Analysis to Python Packages with mypy

Digging into new Python code can be a pain, especially for those used to statically typed languages.

Python 3.5 added support for a type annotation system, described in PEP 484. Python 3.6+ expands this with individual variable annotations (PEP 526). While purely decorative and optional, a tool like mypy can use it to perform static type analysis and catch errors, just like compilers and linters for statically typed languages.

Using it requires a bit of a guarantee from other contributors that they’ll maintain this optional feature, and any gaps in coverage will cause errors to appear for later mypy users, or even worse, errors to not appear as datatypes fall back to Any.

To make sure that packages that are type checked remain so and remove the burden of manually running mypy before and after each contribution, we’ve created an ament_lint plugin for it.

For detailed information about how to use it, check out our blog post on integrating it with your package’s testing pipeline! We’ll also be documenting the process of writing a an ament_lint plugin in an upcoming blog post.

6 Likes