Optional static typing for Python 3 and 2 (PEP 484)
We don't have a mailing list; but we are always happy to answer questions on gitter chat. If you are sure you've found a bug please search our issue trackers for a duplicate before filing a new issue:
Mypy is an optional static type checker for Python. You can add type hints (PEP 484) to your Python programs, and use mypy to type check them statically. Find bugs in your programs without even running them!
You can mix dynamic and static typing in your programs. You can always fall back to dynamic typing when static typing is not convenient, such as for legacy code.
Here is a small example to whet your appetite (Python 3):
from typing import Iterator
def fib(n: int) -> Iterator[int]: a, b = 0, 1 while a < n: yield a a, b = b, a + b
See the documentation for more examples.
For Python 2.7, the standard annotations are written as comments:
python def is_palindrome(s): # type: (str) -> bool return s == s[::-1]
Mypy is in development; some features are missing and there are bugs. See 'Development status' below.
You need Python 3.5 or later to run mypy. You can have multiple Python versions (2.x and 3.x) installed on the same system without problems.
In Ubuntu, Mint and Debian you can install Python 3 like this:
$ sudo apt-get install python3 python3-pip
For other Linux flavors, macOS and Windows, packages are available at
Mypy can be installed using pip:
$ python3 -m pip install -U mypy
If you want to run the latest version of the code, you can install from git:
$ python3 -m pip install -U git+git://github.com/python/mypy.git
Now, if Python on your system is configured properly (else see "Troubleshooting" below), you can type-check the statically typed parts of a program like this:
$ mypy PROGRAM
You can always use a Python interpreter to run your statically typed programs, even if they have type errors:
$ python3 PROGRAM
You can also try mypy in an online playground (developed by Yusuke Miyazaki).
Mypy can be integrated into popular IDEs:
Mypy can also be set up as a pre-commit hook using pre-commit mirrors-mypy.
Documentation and additional information is available at the web site:
Or you can jump straight to the documentation:
Depending on your configuration, you may have to run
$ python3 -m pip install -U mypy
This should automatically install the appropriate version of mypy's parser, typed-ast. If for some reason it does not, you can install it manually:
$ python3 -m pip install -U typed-ast
mypycommand isn't found after installation: After
python3 -m pip install, the
mypyscript and dependencies, including the
typingmodule, will be installed to system-dependent locations. Sometimes the script directory will not be in
PATH, and you have to add the target directory to
PATHmanually or create a symbolic link to the script. In particular, on macOS, the script may be installed under
In Windows, the script is generally installed in
\PythonNN\Scripts. So, type check a program like this (replace
\Python34with your Python installation path):
C:\>\Python34\python \Python34\Scripts\mypy PROGRAM
pip3in a v2 environment will not configure the environment to run installed modules from the command line.
$ python3 -m pip install -U virtualenv $ python3 -m virtualenv env
If you want to contribute, first clone the mypy git repository:
$ git clone https://github.com/python/mypy.git
From the mypy directory, use pip to install mypy:
$ cd mypy $ python3 -m pip install -U .
python3with your Python 3 interpreter. You may have to do the above as root. For example, in Ubuntu:
$ sudo python3 -m pip install -U .
Now you can use the
mypyprogram just as above. In case of trouble see "Troubleshooting" above.
NOTE: Installing with sudo can be a security risk. Please try with the--userflag first. $ python3 -m pip install --user -U .
The basic way to run tests:
$ pip3 install -r test-requirements.txt $ python2 -m pip install -U typing $ ./runtests.py
For more on the tests, such as how to write tests and how to control which tests to run, see Test README.md.
Mypy is beta software, but it has already been used in production for several years at Dropbox and in many other organizations, and it has an extensive test suite.
See the roadmap if you are interested in plans for the future.
Follow mypy's updates on the blog: https://mypy-lang.blogspot.com/
Please report any bugs and enhancement ideas using the mypy issue tracker: https://github.com/python/mypy/issues
If you have any questions about using mypy or types, please ask in the typing gitter instead: https://gitter.im/python/typing
We have built a compiled version of mypy using the mypyc compiler for mypy-annotated Python code. It is approximately 4 times faster than interpreted mypy and is available (and the default) for 64-bit Windows, macOS, and Linux.
To install an interpreted mypy instead, use:
$ python3 -m pip install --no-binary mypy -U mypy
If you wish to test out the compiled version of a development version of mypy, you can directly install a binary from https://github.com/mypyc/mypy_mypyc-wheels/releases/latest.
Any help in testing, development, documentation and other tasks is highly appreciated and useful to the project. There are tasks for contributors of all experience levels. If you're just getting started, ask on the gitter chat for ideas of good beginner issues.
For more details, see the file CONTRIBUTING.md.
Mypy is licensed under the terms of the MIT License (see the file LICENSE).