mypyc: Mypy to Python C Extension Compiler
Mypyc is (mostly) not yet useful for general Python development.
Mypyc is a compiler that compiles mypy-annotated, statically typed Python modules into CPython C extensions. Currently our primary focus is on making mypy faster through compilation -- the default mypy wheels are compiled with mypyc. Compiled mypy is about 4x faster than without compilation.
Mypyc compiles what is essentially a Python language variant using "strict" semantics. This means (among some other things):
Most type annotations are enforced at runtime (raising
Classes are compiled into extension classes without
__dict__(much, but not quite, like if they used
Monkey patching doesn't work
Instance attributes won't fall back to class attributes if undefined
Also there are still a bunch of bad bugs and unsupported features :)
Compiled modules can import arbitrary Python modules, and compiled modules can be used from other Python modules. Typically mypyc is used to only compile modules that contain performance bottlenecks.
You can run compiled modules also as normal, interpreted Python modules, since mypyc targets valid Python code. This means that all Python developer tools and debuggers can be used.
macOS Sierra or later
Xcode command line tools
Python 3.5+ from python.org (other versions are untested)
A recent enough C/C++ build environment
Windows has been tested with Windows 10 and MSVC 2017.
Quick Start for Contributors
First clone the mypy git repository and git submodules:
$ git clone --recurse-submodules https://github.com/python/mypy.git $ cd mypy
Optionally create a virtualenv (recommended):
$ virtualenv -p python3 <directory> $ source <directory>/bin/activate
Then install the dependencies:
$ python3 -m pip install -r test-requirements.txt
Now you can run the tests:
$ pytest mypyc
Look at the issue tracker for things to work on. Please express your interest in working on an issue by adding a comment before doing any significant work, since development is currently very active and there is real risk of duplicate work.
Note that the issue tracker is still hosted on the mypyc project, not with mypy itself.
We have some developer documentation.
Development Status and Roadmap
These are the current planned major milestones:
[DONE] Support a smallish but useful Python subset. Focus on compiling single modules, while the rest of the program is interpreted and does not need to be type checked.
[DONE] Support compiling multiple modules as a single compilation unit (or dynamic linking of compiled modules). Without this inter-module calls will use slower Python-level objects, wrapper functions and Python namespaces.
[DONE] Mypyc can compile mypy.
[DONE] Optimize some important performance bottlenecks.
[PARTIALLY DONE] Generate useful errors for code that uses unsupported Python features instead of crashing or generating bad code.
[DONE] Release a version of mypy that includes a compiled mypy.
- More feature/compatibility work. (100% compatibility with Python is distinctly an anti-goal, but more than we have now is a good idea.)
- [DONE] Support compiling Black, which is a prominent tool that could benefit and has maintainer buy-in. (Let us know if you maintain another Python tool or library and are interested in working with us on this!)
- More optimization! Code size reductions in particular are likely to be valuable and will speed up mypyc compilation.
We'll see! Adventure is out there!
We have some ideas for future improvements and optimizations.