Change the future

Mypy: Optional Static Typing for Python

Jukka Lehtosalo

Audience level:
Intermediate
Category:
Core Python (Language, Stdlib)

Description

Mypy is an experimental Python variant that supports seamless mixing of dynamic and static typing. The implementation can type check programs with optional type annotations and translate them to readable Python 3. The long-term goal of the project is to develop an ahead-of-time compiler that generates efficient native code.

Abstract

Mypy is an experimental variant of Python that supports writing programs that seamlessly mix dynamic and static typing. Mypy lets you add optional type annotations to Python code, type check your programs and translate them to readable Python 3 for execution.

I will give an informal overview of mypy and dynamic and static typing, and explain why having both dynamic and static typing in a programming language can be useful for Python developers. Static typing can, for example, make projects with multiple developers easier to maintain and refactor, it can improve efficiency and enable powerful IDE features such as precise code completion. I will also discuss what kinds of projects are likely to get the biggest benefits from static typing.

The mypy implementation is in development, but it is already self-hosting: the type checker and translator is implemented in mypy. The long-term goal of the project is to develop an ahead-of-time compiler that generates efficient native code and a new VM that supports efficient multi-threading without the GIL.

I will also contrast mypy with earlier projects with similar goals, such as PyPy and Cython.