Member-only story

Python type hints: Just Do It

Tudor Surdoiu
4 min readMar 5, 2022

--

How to bring python into the neighborhood of typed programming languages.

Photo by David Clode on Unsplash

Small and large python-based projects can benefit from using type hinting in their codebase, both from the perspective of software robustness and development experience. The very act of writing type hints will force you to think about the various dynamic flows of the program which will lead to a cleaner architecture and will help catch errors.

Type hinting is not enforced by the python runtime, it is only used to perform static analysis during the development, its usage is not mandatory and the benefits come through third-party tools like IDEs/Code-Editors, type checkers, etc.

In this article, we will only cover a small part of the type hinting expressiveness, for the full specification you can check this.

Basic syntax

Type hints usage is pretty basic, it involves a colon and a specific type, the format is like this: <variable_name> : <type>. When a variable is not declared with a type the type checking tool will try to infer the type from the expression/function that assigns a value.

Build-in types

The most used types are the ones for primitives and data containers like lists, tuples, and dictionaries. For the data containers, you can also specify…

--

--

Tudor Surdoiu
Tudor Surdoiu

Written by Tudor Surdoiu

Bio digital jazz writer, sometimes knocking on the sky and listening to the sound.

No responses yet