Mypy Frequently Asked Questions
What kind of performance can I expect?
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
faster
program startup speed and often better-performing libs than in 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
soon.
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
to
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
results.
We don't believe that it is practical to adapt the CPython VM incrementally
for running parallel workloads efficiently. 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
Alore 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 will 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
mypy modules.
We plan to port commonly used, performance-sensitive Python modules to mypy
and the modified C API to minimize performance bottlenecks.
Why is mypy not using the standard Python syntax for type annotations?
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 makes it easy to compile mypy programs to efficient
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
native code).
- 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.
- Static typing can make programs easier to understand and maintain.
Type declarations serve as machine-checked documentation and automatic
runtime assertions. This is important as code is typically read much
more often than modified, and this is especially important for code that
does complex things.
- 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
time-saver.
- 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 and to improve efficiency.
- Static typing makes it practical to build useful development
tools such as IDEs with precise and reliable code completion, static
analysis tools, etc.
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.
- Efficiency is important.
- Your codebase must be maintained for a long time.
- Many 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
iterations).
- 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.
- You want to future-proof your project even if currently none of the
above really apply.
Will mypy be free?
Yes. Mypy will be available under the MIT license.
Can I run my existing Python programs with mypy?
It depends. Small programs may run with no modifications, but large programs
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 future.
Why a new implementation? Why not add static typing to CPython or
PyPy?
Adding static typing to a language 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,
which already supports optional static typing, 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.
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 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 have
limited processing power. Wasted cycles also consume the battery
quickly.
- Users expect to be able to start mobile apps quickly and competitive
user interfaces
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
native APIs.
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
appreciated, though.
Why not use structural subtyping?
Mypy primarily uses
nominal
subtyping instead of
structural
subtyping. 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.
- 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).
- 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.
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 mypy will primarily use nominal subtyping.
How is mypy different from Python?
The most obvious difference is the availability of static typing.
The
mypy overview mentions a few more such as the
need to make attributes explicit and more explicit protocol representation.
Static typing and the goal of high performance
also result in other, more subtle changes.
Mypy is unlikely to support arbitrary runtime redefinition of methods and
functions, as these features are expensive to support on the JVM and/or with
ahead-of-time compilation.
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
tagged as dynamic).
Also some language features that are evaluated at runtime in Python may
happen during compilation in mypy. For example, mypy base classes may be
bound during compilation (or program loading, before evaluation), unlike
Python.
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 addresses many concerns,
including efficiency, scalability to large and complex projects,
maintainability and programmer productivity.
PyPy is an implementation of Python that conforms
closely to the main Python implementation, more closely than mypy will.
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 also 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 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.
However, current
mypy performance estimates are based on early prototypes, so take them with
a grain of salt.
How is mypy different from Cython?
Cython is a variant of Python that
supports compilation to efficient C modules. It is an
extension to CPython that can give major speedups to certain classes of
programs; mypy is a major rethink of the entire language and
the implementation.
Mypy differs in the following aspects, among others:
- Mypy will have new 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.
- Mypy will 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.
- Mypy has a different set of type system features. (Feature-by-feature
analysis will be added soon.)
- The mypy syntax is arguably simpler and more compact
(no cdef, etc.) for statically typed code.
- Cython supports accessing C functions directly and many features are
defined in terms of translating them to 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.
How is mypy different from RPython or Shed Skin?
RPython and
Shed Skin are
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
signatures.
- 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 will support user-defined generic types.
Mypy is a cool project. Can I help?
Any help is much appreciated!
Contact the
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!