Mypy Frequently Asked Questions
Why have both dynamic and static typing?
Dynamic typing can be flexible, powerful, convenient and easy. But it's
not always the best approach; there are good reasons why many developers
choose to use staticaly typed languages.
Here are some potential benefits of mypy-style static typing:
- Static typing can make programs easier to understand and maintain.
Type declarations can serve as machine-checked documentation (and
optionally, automatic runtime assertions). This is important as code is
typically read much
more often than modified, and this is especially important for large
and complex programs.
- Static typing can help you find bugs earlier and with less
testing and debugging due to compile-time and runtime
type checking. In large and complex projects this can be a major
- Static typing can help you find difficult-to-find bugs before your
code goes into production. This can improve reliability and
reduce the number of security issues.
- Static typing makes it practical to build very useful development
tools that can improve programming productivity or software quality,
including IDEs with precise and reliable code completion, static
analysis tools, etc.
- You can get the benefits of both dynamic and static typing in a single
language. Dynamic typing can be perfect for a small project
or for writing the UI of your program, for example.
As your program grows, you can adapt tricky application logic to static
typing to help maintenance, or to improve efficiency.
- Static typing makes it relatively easy to compile mypy programs
native code. Only performance-critical code needs to be statically typed
to get most of the benefits. Also dynamically typed can be
faster than in Python, as a side effect of other mypy design decisions.
- Static typing makes it possible to run efficiently on the JVM, .NET
and Dalvik/Android (in the future; current development efforts target
- Static typing enables efficient ahead-of-time and method-at-a-time
compilation to native code. This means fast program startup, stable
performance without unpredictable slowdowns caused the JIT compiler, and
predictable performance for complex programs.
See also the front page.
Would my project benefit from static typing?
For many projects dynamic typing is perfectly fine (we think that Python
is a great language). But sometimes your projects demand bigger
guns, and that's when mypy may come in handy.
If some of these ring true for your projects, mypy (and static
typing) may be useful:
- Your project is large or complex.
- Your codebase must be maintained for a long time.
- Multiple developers are working on the same code.
- Running tests takes a lot of time or work (type checking may help you
find errors early in development, reducing the number of testing
- Some project members (devs or management) don't like dynamic typing,
but others prefer dynamic typing and Python syntax. Mypy could be a
solution that everybody finds easy to accept.
- Efficiency is important.
- You want to future-proof your project even if currently none of the
above really apply.
Can I run my existing Python programs with mypy?
It depends. Compatibility is much better when using Python semantics, but
obviously this won't give you performance benefits.
The ultimate goal is to be
able to use arbitrary dynamically typed Python code and modules in mypy
programs when using Python semantics, though this is still some way off.
Even when using the native semantics (which is still early in
development), small programs may run with no modifications, but large
probably need some changes. We are planning to support importing normal
Python modules in mypy programs, so you won't have to migrate your entire
program to get benefits.
See also the question about differences
between mypy and Python.
All of my code is still in Python 2. What are my options?
Mypy currently supports Python 3 syntax. However, we are planning to add
Python 2 support in the near future.
Is mypy free?
Yes. Mypy is free software, and it can also be used for commercial and
proprietary projects. Mypy is available under the MIT license.
Why not use structural subtyping?
Mypy primarily uses
. Some argue that structural subtyping is better suited for
languages with duck typing such as Python. Mypy uses nominal subtyping
primarily for these reasons:
- It is easy to generate short and informative error messages when
using a nominal type
system. This is especially important when using type inference.
- Python supports basically nominal isinstance tests and they are
widely used in programs. It is not clear how to support isinstance in
a purely structural type system while remaining compatible with Python.
- Many programmers are already familiar with nominal subtyping as it is
used in languages such as Java, C++ and C#, but only relatively
few languages use structural subtyping.
- Nominal subtyping is used in the JVM and .NET. Nominal subtyping makes
it relatively easy and efficient to interact with the JVM and Java
libraries (and .NET).
However, structural subtyping can be useful in some contexts.
Structural subtyping is a very potential feature to be added to mypy in the
future, even though we expect that most mypy programs will primarily use
What kind of performance can I expect with the native semantics?
We believe that, in principle, the mypy design makes it possible to run
straight statically typed mypy code pretty much as efficiently as
Java using the
techniques developed in the mypy project, but we hope to have much
program startup speed than Java.
It is always tricky to give precise performance
figures, as relative performance can vary a lot from program to program, but
let's give it a try. Some programs could run 50 times faster than
CPython (or more), but programs that do a lot of I/O or spend most of the
time in libraries
implemented in C might see little or no benefit. Some big
performance boosts might be seen in programs that make heavy use of
object-oriented techniques and programs that do a lot of number crunching.
Practical considerations such as
the availability of contributors
determine how close to the expected performance you can
get in the near future. For example, we are planning to implement a C or
LLVM back end
initially, and although this can save a lot of development time, this may
restrict performance somewhat (by a factor of 2, perhaps).
Are you developing a custom JIT compiler or something else?
The initial compiler will compile into C or use LLVM for the back end
(ahead-of-time compilation to native code, no traditional VM).
Later on, we are planning to use a JIT compiler that uses type feedback or
trace complation for additional
speedups for dynamically typed code, but this won't probably happen any time
The first prototypes will have no real back end at all but will translate
into Python after type checking. Obviously at this stage there will be
no performance benefit over CPython/PyPy yet, but these prototypes allow
you to get a feel of how the static type checking will work. The translator
Python is a also an easy way to try mypy if you care more about the
potential maintainability, reliability and productivity benefits of static
typing instead of improved performance.
A JVM back end would be nice as well, but it's not the initial focus.
However, we try to avoid making design decisions that compromise efficiency
on the JVM.
How are going to get rid of the GIL? Previous attempts have had mixed
We don't believe that it is practical to adapt the CPython VM incrementally
for running parallel workloads efficiently within a single process. Instead,
we are going to get rid of the GIL by having a new VM for running mypy code.
The VM will be based on the
VM, but it will use
compiled native code from the start
(the Alore VM uses a bytecode interpeter). It will use a garbage
collector instead of reference counting (CPython primarily uses reference
counting), and it will also
support a modified
C Python extension API that will support proper multithreading without a GIL.
The CPython VM would still be used to run Python code and modules,
and it will
still be limited to running a single thread at a time (most of the time).
Objects passed between the VMs will have to be copied or accessed using
proxy objects, so there will be some performance overhead compared to native
We plan to port commonly used, performance-sensitive Python modules to mypy
and the modified C API to minimize performance bottlenecks.
Why a new implementation? Why not add static typing to CPython or
Mypy already supports static type checking on top of any Python 3.x
implementation, but the
types are erased during program evaluation and thus there is no performance
Adding static typing to a language in a way that improves efficiency
for a wide variety of code is a pretty significant change, which
requires modifications throughout an implementation.
Modifying an existing Python implementation would
result in a major redesign (or too many compromises to our liking).
Instead, we decided to
use the an existing language
implementation as our basis. This way there's
less painful rework, and we can get started much more quickly.
We are going to port Python libraries instead of rewriting them,
however, as this will save a big chunk of implementation work.
However, we are also considering an approach similar to Cython or Nuitka
where mypy modules can be compiled to CPython extension modules, with some
performance improvements. This would restrict the available performance
benefits, since speeding up many primitive operations and memory allocations,
may not be practical, but it could speed up
many programs significantly, while still being fully
compatible with existing Python modules.
I like Python as it is. I don't need static typing or better performance.
That wasn't really a question, was it? But here is a response anyway.
Mypy is not aimed at replacing Python; the goal is to have a more Pythonic
alternative when pure Python is not a realistic option.
Many developers have to use Java,
C++ or another statically typed language even though they would prefer
Python, if it was suitable for the project.
Would mypy be good for mobile app development?
Certainly, mypy has many features that make it a good candidate for
developing mobile apps:
- Performance is often important since smartphones and tablets typically
have much less processing power compared to laptops, desktops and
servers. Wasted cycles also consume the battery quickly.
- Users expect to be able to start mobile apps quickly and competitive
need to be smooth with very little lag. Mypy supports ahead-of-time
compilation, which is a good match for these requirements.
- It should be possible to run mypy programs efficiently on the Dalvik
VM in Android. This is important for efficient access to the Android
However, mobile platforms are not the initial focus in mypy development
— we don't have the resources to do everything at once. Any help is
How is mypy different from Python?
The obvious difference is the availability of static type checking.
The mypy tutorial mentions some modifications
to Python code that may be required to make code type check without
errors, such as the
need to make attributes explicit and more explicit protocol representation.
When using the Python semantics, mypy runtime semantics are identical to
Python semantics. The type checker may give warnings for some valid Python
code, but the code is still always runnable.
Also, some Python features and
syntax are still not supported by mypy, but this is gradually improving.
Static typing and the goal of high performance
also result in other, more subtle changes in the native semantics.
Mypy is unlikely to support arbitrary runtime redefinition of methods and
functions, as these features are expensive to support with
ahead-of-time compilation and/or on the JVM.
Mypy will support modular,
efficient type checking, and this seems to rule out some language features,
such as arbitrary runtime addition of methods or base types to classes.
However, it is
likely that many of these features will be supported in a restricted form
(for example, runtime modification is only supported for classes or methods
registered as dynamic or 'patchable').
Also some language features that are evaluated at runtime in Python may
happen during compilation in mypy when using the native semantics.
For example, mypy base classes may be
bound during compilation (or program loading, before evaluation), unlike
How is mypy different from PyPy?
This answer relates to PyPy as a Python implementation. See also the
answer related to RPython below.
The main difference
between mypy and PyPy is the availability of
static typing in mypy.
Also while PyPy focuses on efficiency, mypy aims to address many concerns,
including scalability to large and complex projects,
maintainability, programmer productivity and efficiency.
PyPy is an implementation of Python that conforms
closely to the main Python implementation, more closely than the mypy
Even though mypy aims to support
most Python features at some point, mypy may make changes to features
that are in conflict with other goals of mypy (such as support of static
typing and high runtime efficiency), whereas PyPy tries to preserve the
original Python semantics. Still, many programs written with CPython in mind
have to be modified to work properly with PyPy. (See also to the answer to
the question above).
Like PyPy, mypy will also have a new, efficient virtual machine,
but it's design will be different from PyPy.
Statically typed mypy code is expected to often be
significantly more efficient than PyPy; this includes both peak performance
and program startup / warmup speed.
Many developers have reported that PyPy only
gives them modest performance gains, or none at all.
Dynamically typed mypy code also has the potential
to run more efficiently than PyPy due differences in semantics.
mypy performance estimates are based on early prototypes, so take them with
a grain of salt.
How is mypy different from Cython?
is a variant of Python that
supports compilation to CPython C modules. It can give major speedups
to certain classes of programs compared to CPython. The mypy VM is a bigger
rethink of the entire language and the implementation.
Mypy differs in the following aspects, among others:
- The mypy syntax is arguably simpler and more "Pythonic" (no
cdef/cpdef, etc.) for statically typed code.
- The mypy syntax is compatible with Python. Mypy programs can be run
as normal Python programs using CPython, for example. Cython has many
incompatible extensions to Python syntax, and Cython programs generally
run without first compiling them to CPython extension modules via C.
Cython also has a pure Python mode, but it seems to support only
a subset of Cython functionality, and the syntax is quite verbose.
- Mypy has a different set of type system features. For example, mypy
has genericity (parametric polymorphism), function types and
bidirectional type inference, which are not supported by Cython.
(Cython has fused types that are different but related to
- The mypy type checker knows about the static types of many Python
stdlib modules and can effectively type check code that uses them.
- Cython supports accessing C functions directly and many features are
defined in terms of translating them to C or C++. Mypy is not bound
to any particular target language and can support both C and Java
backends, for example. However, accessing C library functionality in mypy
will not be as easy as in Cython.
- Our aim is build a new mypy virtual machine that allows speeding
up all major
parts of the implementation, including standard libraries and the
garbage collector. Cython uses the normal Python VM.
- The mypy VM aims to support efficient multithreading without the GIL
for a wide range of programs. Cython programs
can explicitly release the GIL, but manipulating Python objects
is not possible when the GIL is released.
Note however that the mypy VM is still very early in development.
How is mypy different from Nuitka?
is a static compiler that can
translate Python programs to C++. Nuitka integrates with the CPython runtime.
Nuitka has additional future goals,
such as using type inference and whole-program analysis to further speed up
Nuitka has many goals similar to mypy, but since there is not yet much
available information on them, a detailed comparison is not possible.
Here are some likely differences:
- Nuitka is primarily focused on speeding up Python code. Mypy
places a heavy emphasis on static type checking and facilitating better
- Whole-program analysis tends to be slow and scale poorly to large
or complex programs. It is still unclear if
Nuitka can solve these issues. Mypy
does not use whole-program analysis and will support modular
- In general, it seems
that the performance of Nuitka will be limited, at least to a degree, by
having to use CPython object representation and reference counting
semantics, similar to Cython.
- It seems unlikely that Nuitka can solve CPython GIL issues.
- Nuitka aims at being compatible with CPython. Mypy native semantics
relax Python compatibility somewhat, by design, in order to facilitate
better performance and more effective runtime type checking, for example.
In this respect mypy is closer to Cython than Nuitka.
How is mypy different from RPython or Shed Skin?
basically statically typed subsets of Python. Mypy does the following
important things differently:
- Mypy will support both static and dynamic typing. When using dynamic
typing, mypy is very similar to Python. Dynamically typed and statically
typed code can be freely mixed and can interact seamlessly.
- Mypy will support fast and modular type checking. Both RPython and Shed
Skin use whole-program type inference which is very slow, does not scale
well to large programs and often produces confusing error messages.
Mypy can support modularity since it only uses local type inference;
static type checking depends on having type annotatations for functions
- Mypy will support introspection, dynamic loading of code and many other
dynamic language features. RPython and Shed Skin only support a
restricted Python subset without several of these features.
- Mypy supports user-defined generic types.
Mypy is a cool project. Can I help?
Any help is much appreciated! Contact
developers if you would like to contribute. Any help related to development,
design, publicity, documentation, testing, web site maintenance, financing,
etc. can be helpful. You can learn a lot by contributing, and
anybody can help, even beginners!