Write Safer Python Code using Static Type Checking

Mypy - Compile-Time Static Type Checker for Python

Sami Islam
7 min readSep 19, 2020
Photo by David Clode on Unsplash

Having a background using statically typed programming languages, I have grown to depend a lot on the compiler doing the heavy work for me. I find it utmost comforting to know that I cannot make any silly mistakes. It leaves me with more time to concentrate on the important stuff.

That’s why, when I found out about Mypy - the compile-time static type checker for Python - I was ecstatic! In this article I am going to describe the following aspects of using Mypy to analyse Python code:

  • Installing, running, and configuring MyPy in Visual Studio Code
  • Analyse built-in types in Python
  • Analyse function declarations in Python
  • Type inference - bidirectional
  • Types - Class (ClassVar + forward reference), Any, Tuple, Callable (lambdas), Union, Optional, NoReturn, NewType, TypedDict, Type alias
  • Library stubs and typeshed
  • Final

I use Visual Studio Code, the pipenv (v2020.6.2) Python package manager, and Python v3.8.2. Here is a great article on pipenv from Erik van Baaren:

--

--