First steps
Using mypyc
Native operations reference
Advanced topics
Project Links
Mypyc compiles Python modules to C extensions. It uses standard Pythontype hints togenerate fast code.
The compiled language is a strict,gradually typed Python variant. Itrestricts the use of some dynamic Python features to gain performance,but it’s mostly compatible with standard Python.
Mypyc usesmypy to perform typechecking and type inference. Most type system features in the stdlibtyping module aresupported.
Compiled modules can import arbitrary Python modules and third-partylibraries. You can compile anything from a single performance-criticalmodule to your entire codebase. You can run the modules you compilealso as normal, interpreted Python modules.
Existing code with type annotations is often1.5x to 5x fasterwhen compiled. Code tuned for mypyc can be5x to 10x faster.
Mypyc currently aims to speed up non-numeric code, such as serverapplications. Mypyc is also used to compile itself (and mypy).
Easy to get started. Compiled code has the look and feel ofregular Python code. Mypyc supports familiar Python syntax and idioms.
Expressive types. Mypyc fully supports standard Python type hints.Mypyc has local type inference, generics, optional types, tuple types,union types, and more. Type hints act as machine-checkeddocumentation, making code not only faster but also easier tounderstand and modify.
Python ecosystem. Mypyc runs on top of CPython, thestandard Python implementation. You can use any third-party libraries,including C extensions, installed with pip. Mypyc uses only valid Pythonsyntax, so all Python editors and IDEs work perfectly.
Fast program startup. Mypyc uses ahead-of-time compilation, socompilation does not slow down program startup. Slow program startupis a common issue with JIT compilers.
Migration path for existing code. Existing Python code oftenrequires only minor changes to compile using mypyc.
Waiting for compilation is optional. Compiled code also runs asnormal Python code. You can use interpreted Python during development,with familiar and fast workflows.
Runtime type safety. Mypyc protects you from segfaults and memorycorruption. Any unexpected runtime type safety violation is a bug inmypyc. Runtime values are checked against type annotations. (Withoutmypyc, type annotations are ignored at runtime.)
Find errors statically. Mypyc uses mypy for static type checkingthat helps catch many bugs.
Fix only performance bottlenecks. Often most time is spent in a fewPython modules or functions. Add type annotations and compile thesemodules for easy performance gains.
Compile it all. During development you can use interpreted mode,for a quick edit-run cycle. In releases all non-test code is compiled.This is how mypy achieved a 4x performance improvement over interpretedPython.
Take advantage of existing type hints. If you already use typeannotations in your code, adopting mypyc will be easier. You’ve alreadydone most of the work needed to use mypyc.
Alternative to a lower-level language. Instead of writingperformance-critical code in C, C++, Cython or Rust, you may get goodperformance while staying in the comfort of Python.
Migrate C extensions. Maintaining C extensions is not always funfor a Python developer. With mypyc you may get performance similar tothe original C, with the convenience of Python.
Mypyc targets many similar use cases as Cython. Mypyc does many thingsdifferently, however:
No need to use non-standard syntax, such ascpdef
, or extradecorators to get good performance. Clean, normal-lookingtype-annotated Python code can be fast without language extensions.This makes it practical to compile entire codebases without adeveloper productivity hit.
Mypyc has first-class support for features in thetyping
module,such as tuple types, union types and generics.
Mypyc has powerful type inference, provided by mypy. Variable typeannotations are not needed for optimal performance.
Mypyc fully integrates with mypy for robust and seamless static typechecking.
Mypyc performs strict enforcement of type annotations at runtime,resulting in better runtime type safety and easier debugging.
Unlike Cython, mypyc doesn’t directly support interfacing with C librariesor speeding up numeric code.
Mypyc uses several techniques to produce fast code:
Mypyc usesahead-of-time compilation to native code. This removesCPython interpreter overhead.
Mypyc enforces type annotations (and type comments) at runtime,raisingTypeError
if runtime values don’t match annotations.Value types only need to be checked in the boundaries betweendynamic and static typing.
Compiled code uses optimized, type-specific primitives.
Mypyc usesearly binding to resolve called functions and namereferences at compile time. Mypyc avoids many dynamic namespacelookups.
Classes are compiled toC extension classes. They usevtables for fastmethod calls and attribute access.
Mypyc treats compiled functions, classes, and attributes declaredFinal
as immutable.
Mypyc has memory-efficient, unboxed representations for integers andbooleans.
Mypyc is currently alpha software. It’s only recommended forproduction use cases with careful testing, and if you are willing tocontribute fixes or to work around issues you will encounter.