Pyrefly and ty: A Closer Look at Python’s New Rust-Powered Type Checkers
Earlier this month, PyCon 2025 featured a Typing Summit where two new Rust-based Python type checkers, pyrefly and ty, made their official appearances. These tools aim to revolutionize Python's type checking landscape, traditionally dominated by mypy and pylance. Both type checkers are in early alpha stages and are open-source, but they differ significantly in their approaches and goals. Pyrefly Developer: Meta (formerly known for Pyre, a Python type checker written in OCaml) Key Features: - Speed: Pyrefly claims to be 35 times faster than Pyre and 14 times faster than Mypy and Pyright. It can handle up to 1.8 million lines of code per second, fast enough for real-time type checking on every keystroke. - Portability and Capability: Meta aims for pyrefly to be more portable and capable compared to its predecessor, Pyre. They emphasize being more engaged with the open-source community and addressing its needs more directly. - Implicit Type Inference: Pyrefly excels in inferring types without explicit annotations, making it useful for legacy code or projects that lack comprehensive type hints. Performance Benchmarks: - PyTorch: Pyrefly took an average of 13.029 seconds to type check the entire repository, which is faster than Mypy and Pyright but slower than ty. - Django: Pyrefly was the second-fastest tool, taking about 0.9 seconds to type check 3,200 files. - Mypy: On the mypy repo, Pyrefly took 136.0 milliseconds to type check the mypyc subdirectory, outperforming Mypy and Pyright. ty Developer: Astral (known for uv and ruff) Key Features: - Gradual Guarantee: ty follows the principle that removing a type annotation should not introduce new type errors. This ensures that working code remains valid even if types are removed, aligning with Python's gradual typing philosophy. - Speed: While also focusing on speed, ty claims to be 1-2 orders of magnitude faster than current-generation type checkers. It outperformed pyrefly in most benchmarks. - AST Parsing: ty leverages Ruff for abstract syntax tree (AST) parsing. - Incrementalism: ty uses the Salsa framework for fine-grained incremental parsing, re-parsing only affected areas. In contrast, pyrefly uses module-level incrementalization, which is simpler to maintain and sufficient for its performance goals. - Error Messages: ty prides itself on providing clear and concise error messages, enhancing developer experience. Performance Benchmarks: - PyTorch: ty completed the type check in an average of 4.039 seconds, making it the fastest among the tested tools. - Django: ty was the fastest here as well, completing in about 0.6 seconds for 2,900 files. - Mypy: On the mypy repo, ty finished in 74.2 milliseconds, significantly faster than Pyrefly, Mypy, and Pyright. Comparisons and Contrasts Speed: Both type checkers deliver impressive speed improvements over existing tools, but ty generally outpaces pyrefly in benchmarks. However, the performance difference is smaller in certain scenarios. Goals: Pyrefly aims to be highly aggressive in type inference, ensuring that even code with minimal type annotations can benefit from strong typing guarantees. This can sometimes lead to false positives, where code that technically works fails type checks. Ty, conversely, focuses on the gradual guarantee, maintaining compatibility with working code as type annotations are added or removed. Incrementalism: - Pyrefly: Module-level incremental parsing, which is fast and easier to maintain but might re-parse entire modules. - Ty: Fine-grained incremental parsing using Salsa, which minimizes re-parsing to only affected areas, offering optimal performance but at the cost of increased complexity. Capabilities: - Implicit Type Inference: Pyrefly is more advanced in implicit type inference, catching potential errors in unannotated code. - Generics: Both tools handle generics, but pyrefly has a slight edge in resolving complex generic relationships. - Intersection and Negation Types: Ty is the only tool to support intersection and negation types, introducing new and powerful paradigms to Python type checking. Industry Insights and Company Profiles Meta (Pyrefly): Meta's transition from Pyre to pyrefly reflects their commitment to optimizing and enhancing their development tools. By being more open and community-focused, they hope to foster broader adoption and contribute to the Python ecosystem's growth. Pyrefly's focus on aggressive type inference aligns with Meta's emphasis on strict type checking, which can be beneficial for large, complex codebases. Astral (ty): Astral, known for their work on the uv web server and the ruff Python linter, brings a fresh perspective to Python type checking with ty. Their emphasis on the gradual guarantee and user-friendly error messages suggests a tool designed for seamless integration into existing workflows. The use of Salsa for fine-grained incremental parsing underscores their commitment to performance and maintainability, although this approach adds complexity to the codebase. Conclusion The introduction of pyrefly and ty marks a significant leap forward in Python type checking. While both tools are still in early alpha stages, they demonstrate promising performance and unique features. pyrefly's aggressive type inference and module-level incremental parsing make it a strong candidate for rigorous type checking, ideal for large-scale projects with a high level of type annotation. ty, with its gradual guarantee and support for intersection and negation types, offers a more flexible and compatible solution, suitable for existing codebases looking to adopt type checking gradually. As these tools evolve, they are likely to converge in features, but their distinct philosophies will continue to cater to different development needs. Go try them out at pyrefly.org/sandbox and play.ty.dev to see how they can enhance your projects.