A Gradual Journey to Typed Python

2 minute read Published:

Python as a gradually typed language yeilds higher development velocity and reduced maintenance.

Python is much loved by developers for its ease of development, its low time to minimum viable product and, some might say, its dynamic typing. I’ve been using dynamically typed Python for the better part of a decade now, for those same reasons. Dynamically typed Python is rapid to develop in, features amorphous blobs of data and works great for writing code quickly.

Gradual typing and static analysis split the difference between the reduced velocity of a strongly typed language and the likewise reduced velocity of maintaining code where the input and output data types are unclear. Gradually typed Python makes reasoning about larger, and older, codebases easier. When I started using types in Python, I was just sprinkling in type annotations here and there. The Python type hinting system can do much more.

Static Analysis of Gradually Typed Python

Formatting tools like Black and isort, along with static analysis tools like Flake8 are great for keeping python code consistent in form and free of unused variables. But Python type hinting can do more. Mypy provides many of the compile time checks you’d get in a strongly typed, compiled language like Rust, which are useful when developing and add no overhead at runtime.

Command Line Interfaces via Typed Python

Click is one of my favorite command line interface libraries. Recently I stumbled upon Typer, Typer is an abstraction on top of Click which uses type annotations and doctrings to generate beautiful command line interfaces with much less boilerplate code. Using Typer, a command line interface is easy to build in just a few lines.

De/Serialization and Validation with Types

Pydantic, especially when combined with mypy is a sure fire way to get lightning fast data models with low cost of development, and lower probability of bugs for serialization, deserizaliztion, and by nature, data validation.

Conclusion

Using the type hinting system has made my code better, made me a better developer, and allowed the sort of semantics used in languages like Rust in Python. I think it can do the same for the reader, and is definitely worth using. I hope to put out some more blogs in the near future on my Python toolbox and maybe even a sample app putting it all together.