Meta Open-Sources Pyrefly, a High-Performance Python Type Checker in Rust

MMS Founder
MMS Sergio De Simone

Article originally posted on InfoQ. Visit InfoQ

Currently in alpha, Pyrefly is a new open-source Python type checker developed by Meta in Rust for maximum performance. Inspired by tools like Pyre, Pyright, and MyPy, Pyrefly is intended to replace the OCaml-based Pyre type checker previously used for Instagram’s codebase.

We built a custom engine for incremental computation and designed our type-checking algorithm based on years of experience in gradual typing theory and Rust expertise. By open-sourcing this technology we hope it can serve projects of any size well.

According to Meta engineers, the main motivation behind Pyrefly was the need to support responsive IDE typechecking, which became increasingly difficult to achieve with Pyre as the complexity of Instagram’s type system grew.

We explored alternate solutions and leveraged community tools like Pyright for code navigation. But the need for an extensible type checker that can bring code navigation, checking at scale, and exporting types to other services drove us to start over, creating Pyrefly.

Pyrefly is designed for high performance and implemented in Rust to maximize efficiency. According to Meta, it can check 1.8 million lines of code per second on large codebases, making it possible to perform typechecking on every single keystroke.

Meta’s benchmarks show that Pyrefly can typecheck the entire Instagram codebase in 13.4 seconds, compared to 100+ seconds with Pyre. Similarly, Pyrefly takes just 2.4 seconds to typecheck PyTorch, while Pyright takes 35.2 seconds and MyPy 48.1 seconds.

Beyond performance, Meta chose Rust for building Pyrefly because of its safety, cross-platform support, and ability to compile to WebAssembly, which enables a browser-based Playground experience.

Besides typechecking annotated Python codebases, Pyrefly also aims to be useful for unannotated code. To this end, it can automatically infer types for return values and local variables, allowing developers to explicitly insert the inferred types into their code if they want so.

Meta has released Pyrefly as an alpha, due to open bugs and features in the making, but plans to move to beta tis Summer. A Visual Studio Code extension is available on the Visual Studio Marketplace.

Pyrefly is not the only Python type checker written in Rust. Astral, a company specializing in developer tools, recently launched Ty in preview. Ty uses an hand-written parser for Python, written in Rust and based on Python’s official specification. It also offers a playground if you’d like to try it out. Other tools made by Astral include the Ruff linter and the uv package manager, both written in Rust.

About the Author

Subscribe for MMS Newsletter

By signing up, you will receive updates about our latest information.

  • This field is for validation purposes and should be left unchanged.