Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Gaurav Singhal

Explore Python Libraries: Mypy

Gaurav Singhal

  • Jul 1, 2020
  • 6 Min read
  • Jul 1, 2020
  • 6 Min read
Data Analytics
Machine Learning


Mypy is an open-source type checker compatible with Python 3.5+. It is maintained by the Dropbox team, and it is used most commonly to statically type-check code.

In this guide, you will explore the exciting, new Python library Mypy. You will also learn to annotate your code in Python 3.5+ and become familiar with Mypy's duck typing compatibility.

What is Mypy?

Mypy is the Python library used to check type annotation when you want to statically type code in Python. it works as an optional static type checker for Python. With Mypy, you can combine the benefits of dynamic and static programming in Python. You can type-check basically without running the actual code as Mypy has no runtime overhead.

You may still be wondering about the benefits of statically typing a code. Let's look at some benefits of static typing or type hinting.

Benefits of Static Typing

Static typing plays a very important role when typing a production code that will be maintained for years. Some benefits of static typing include:

  • Once you have type information for the code, it becomes much easier to understand and use the code.

  • If you can understand the code perfectly, it becomes a lot easier to maintain it.

  • It makes debugging code easier because you always know every function's input and output types.

  • You know where and how the objects in the code are used without actually running the code.

  • The code looks much cleaner in static typing, and the packaging problem of the code or program is also solved.


There are no dependencies for installing Mypy, assuming that you have Python and pip installed on your system.

Using pip

To install using pip, open the terminal and run the following command:

1pip install mypy

Using conda

To install using conda, open the terminal and run the following command:

1conda install -c conda-forge/label/cf201901 mypy

Type Annotation Using Python

When you run code over Mypy without any annotation, it doesn't throw an error as you have to teach Mypy to detect errors using type annotation in the code.

Let's look at a simple code with a type annotation.

2def adding_numbers(a ,b):
3    return a+b

Running the above code with Python:

1$ python
2> 9

If you convert with type annotations, it will look like this:

2def adding_numbers(a: int, b: int) -> int:
3    return a+b

In the above code, you are teaching Mypy that the adding_numbers function can accept and return an integer.

Now let's run this code.

1$ python
2> 45

As you can see, even after typing annotations in the code, you don't get an error as the interpreter is ignoring those type annotations.

To run a code with Mypy, simply run your .py extension file with Mypy's interpreter.


Now let's run this same code with Mypy.

1# annotating our fucntion with int type.
2def adding_numbers(a : int, b: int) -> int:
3    return a+b

1# annotating our fucntion with str type.
2def adding_numbers(a : str, b: str) -> str:
3    return a+b

Here, you get errors without having to set up any kind of test harness over the code or by writing any test that would exercise this code. You get them by only running a test analyzer (Mypy) over the code.

So Mypy asks us to annotate our function signatures to validate our assumptions about input and output types.

Nominal Types

There are a lot of nominal types used for type checking. Here I explain only those which are most commonly used.

  • Built-in Python types (int, float, type, object, etc.)
  • Generic containers. Here are a few examples :

    • Tuple type

      1  t : Tuple(int, float) = 0, 1,1.2  
    • Dict type

      1 d : dict(str, int) = {'a' : 1, 'b' : 2}
      2 d : mutablemappingstr[ str, int ] = {'a':1, 'b' : 20}
    • List Type

      1l : List[int] = [1,2,3]
    • Iterable type

      1i : Iterable[Text] = [u '1'. u '2', u '3']
  • Alias Types

    1vector = list[float]
  • Distinct Types
1UserId = NewType('UserId', int)
2some_id = UserId(524313)

Duck Type Compatibility

Sometimes certain types in Python are compatible even if they are not subclasses of each other. For example, int objects are valid whenever float objects are expected. Mypy supports this via duck type compatibility, which is supported only for a small set of built-in-types.

  • float is duck type compatible with complex
  • int is duck type compatible with float and complex
  • In Python 2, str is a duck type compatible with Unicode.
1# importing math library
2import math
4# annotate the fucntion which we have defined
5def degrees_to_radians(degrees: float) -> float:
6    return math.pi * degrees / 180
8n = 90

This code annotates the input with float type and gives the input in int. Still, Mypy is not throwing a type error.


In this guide, we looked at the basics of Mypy and how to write a static code in Python. We described the benefits of static typing and why it's important when writing a production code that will be maintained for years. We also learned how to annotate code in Python 3.5+ and how to check the annotated code with Mypy. Finally, we also looked at the duck typing compatibility of Mypy.

I hope that through this guide you came to understand the basics of Mypy and type checking in Python and are motivated to go deeper with this wonderful type checking tool. For more information, you can always check out the Mypy documentation.