Member-only story
Python type hints: Just Do It
How to bring python into the neighborhood of typed programming languages.
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…