20,718
stars
+4+4+60
3,380
forks
–
watchers
–
prs
–
issues
–
releases
–
discussions
README
Mypy: Static Typing for Python
=======================================
Got a question?
---------------
We are always happy to answer questions! Here are some good places to ask them:
- for general questions about Python typing, try typing discussions
- for anything you're curious about, try gitter chat
If you're just getting started,
the documentation
and type hints cheat sheet
can also help answer questions.
If you think you've found a bug:
- check our common issues page
- search our issue tracker to see if
it's already been reported
To report a bug or request an enhancement:
- report at our issue tracker
- if the issue is with a specific library or function, consider reporting it at
typeshed tracker or the issue
tracker for that library
To discuss a new type system feature:
- discuss at discuss.python.org
- there is also some historical discussion at the typing-sig mailing list and the python/typing repo
What is mypy?
-------------
Mypy is a static type checker for Python.
Type checkers help ensure that you're using variables and functions in your code
correctly. With mypy, add type hints (PEP 484)
to your Python programs, and mypy will warn you when you use those types
incorrectly.
Python is a dynamic language, so usually you'll only see errors in your code
when you attempt to run it. Mypy is a static checker, so it finds bugs
in your programs without even running them!
Here is a small example to whet your appetite:
python
number = input("What is your favourite number?")
print("It is", number + 1) error: Unsupported operand types for + ("str" and "int")
Adding type hints for mypy does not interfere with the way your program would
otherwise run. Think of type hints as similar to comments! You can always use
the Python interpreter to run your code, even if mypy reports errors.
Mypy is designed with gradual typing in mind. This means you can add type
hints to your code base slowly and that you can always fall back to dynamic
typing when static typing is not convenient.
Mypy has a powerful and easy-to-use type system, supporting features such as
type inference, generics, callable types, tuple types, union types,
structural subtyping and more. Using mypy will make your programs easier to
understand, debug, and maintain.
See the documentation for
more examples and information.
In particular, see:
- type hints cheat sheet
- getting started
- list of error codes
Quick start
-----------
Mypy can be installed using pip:
bash
python3 -m pip install -U mypy
If you want to run the latest version of the code, you can install from the
repo directly:
bash
python3 -m pip install -U git+https://github.com/python/mypy.git
Now you can type-check the [statically typed parts] of a program like this:
bash
mypy PROGRAM
You can always use the Python interpreter to run your statically typed
programs, even if mypy reports type errors:
bash
python3 PROGRAM
If you are working with large code bases, you can run mypy in