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 time-saver.
  • 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 may make it practical to compile (some) mypy programs to efficient native code in the future. Only performance-critical code would need to be statically typed to get most of the benefits.
  • Static typing may make it possible to run efficiently on the JVM, .NET and the Android Java VM when using the native semantics (in the future; current development efforts target static type checking only).

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 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.
Can I run my existing Python programs with mypy?
It depends. Compatibility is pretty good when using Python semantics, but several Python features are not yet implemented. 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.
Will static typing make my programs run faster?
Currently mypy only supports static type checking and it does not improve performance. In the future, it may be possible to compile statically typed mypy code to CPython modules or to JVM bytecode, for example (using the native semantics), but this is not the current development focus. It may even be possible to modify existing Python VMs to take advantage of static type information, but whether this is feasible is still unknown.
All of my code is still in Python 2. What are my options?
Mypy currently supports Python 3 syntax. Python 2 support is still in early stages of development. However, Python 2 support will be improving.
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 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:
  1. It is easy to generate short and informative error messages when using a nominal type system. This is especially important when using type inference.
  2. 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.
  3. 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.
  4. Nominal subtyping is used in the JVM and .NET. Nominal subtyping should make it easier and more efficient to interact with the JVM and Java libraries (and .NET) when using the native semantics.

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 nominal subtyping.

I like Python as it is. I don't need static typing.
That wasn't really a question, was it? Mypy is not aimed at replacing Python. The goal is to give more options for Python programmers, to make Python a more competitive alternative to other statically typed languages in large projects, to improve programmer productivity and to improve software quality.
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 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 aims to address many concerns, including scalability to large and complex projects, maintainability, programmer productivity -- and potentially (in the future) efficiency.

PyPy is an implementation of Python that conforms closely to the main Python implementation. Even though mypy aims to support most Python features at some point, mypy may make restrictions to features that are in conflict with other goals of mypy, 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).

How is mypy different from Cython?
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. Mypy differs in the following aspects, among others:
  • Cython is more focused on performance than mypy. Mypy is primarily about static type checking, and increased performance may (or may not) come later.
  • 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 cannot be 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 mypy generics.)
  • 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 could 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 Nuitka?
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 code. Nuitka has many goals similar to mypy, but since there is not yet much available information on them at the time of writing this, 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 tools.
  • 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 type checking.
  • 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 the mypy native semantics is closer to Cython than Nuitka.
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 aims to support (in the future) 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 supports 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! However, some knowledge of compilers and/or type systems is essential if you want to work on mypy internals.