Speedy New Helpers for Your Python Code: Meet Pyrefly and Ty!
Hey everyone, John here! If you’ve ever written a computer program, even a tiny one, you know it’s easy to make little mistakes. Today, we’re looking at some new tools that are like super-fast proofreaders for Python, one of the most popular programming languages out there. And the really interesting part? These new tools are largely built using a different language called Rust, all to make them extra speedy!
Why Not Just Use Python to Check Python?
That’s a great question, and one you might be wondering about. Python is fantastic, super versatile, and relatively easy to learn. But when it comes to tools that need to check your code as you type it and give you instant feedback, sometimes Python itself can be a little on the slower side. Think of it like this: Python is a friendly, all-around helper, but for certain super-fast tasks, you might bring in a specialist.
Lila: “John, what’s so special about this ‘Rust’ language then? Why is it faster for these kinds of tools?”
John: “Great question, Lila! Rust is a programming language known for its speed and safety. Imagine you’re building with LEGOs. Python is like having a big, versatile box of LEGOs that lets you build almost anything, but sometimes finding the exact right piece or making sure everything fits perfectly takes a moment. Rust, in this analogy, is like having a set of precision-engineered, high-speed LEGO-building robots. They can assemble and check structures incredibly quickly and make sure everything is super strong. So, for tools that need to analyze code and give instant feedback – like spell-checkers for code – Rust’s speed is a huge advantage.”
Modern tools for managing projects or formatting code are increasingly using Rust to be quick and efficient. Now, this trend is coming to “type checking” for Python.
Lila: “Hold on, John. What exactly is ‘type checking’?”
John: “Excellent point, Lila! Imagine you’re writing a recipe. ‘Type checking’ is like having an assistant who makes sure you’re using the right kind of ingredients. For example, if the recipe says ‘add 1 cup of sugar,’ the type checker makes sure you’re not trying to add ‘1 cup of salt’ or ‘1 cup of water’ by mistake. In programming, data has ‘types’ – it can be a number, a piece of text (called a ‘string’), a list, and so on. Type checking tools read your Python code and help catch errors where you might be using the wrong type of data in the wrong place. This can save a lot of headaches later on by finding bugs early!”
Two new Rust-powered tools in this area are creating a buzz: Ty (from the makers of other fast Python tools like uv and ruff) and Pyrefly (from Meta, the company behind Facebook). Both aim to do the same thing: check your Python code for these type errors very, very quickly. Let’s see how they stack up!
Getting to Know: Pyrefly
Pyrefly isn’t Meta’s first attempt at a type-checking tool. They previously had one called Pyre (written in yet another language, OCaml), but Pyre has now been retired, and Pyrefly, built fresh in Rust, is its successor.
First Impressions and Handling Errors
If you run Pyrefly on an existing Python project for the first time, especially one that wasn’t written with type checking in mind, you might see a lot of errors pop up! It can be a bit overwhelming.
Lila: “Oh wow, a flood of errors sounds scary! Is there a way to manage that without fixing everything at once?”
John: “Absolutely, Lila. Pyrefly has a neat feature for this. You can tell it to ‘suppress’ all the errors it finds. This means it adds little notes (special comments) in your code to tell itself, ‘Okay, I see this error, but I’m going to ignore it for now.’ Then, you can go back, remove these suppression notes one by one, and fix the actual issues. This allows you to gradually clean up an older ‘untyped’ codebase (a project that didn’t have type information before) without having to tackle hundreds of errors simultaneously. It’s like decluttering a room bit by bit instead of trying to do it all in one go.”
Setting Things Up
Like most modern Python tools, Pyrefly uses a special file called pyproject.toml
to store its settings for your project.
Lila: “What’s a pyproject.toml
file, John? Sounds a bit technical.”
John: “Think of pyproject.toml
(that’s ‘tee-oh-em-el’) as the main instruction manual or settings panel for your Python project, Lila. It’s a standard file where you can tell different tools how they should behave for that specific project. So, for Pyrefly, you’d put its configuration – like which rules to follow or which files to ignore – inside this pyproject.toml
file. It helps keep all your project settings organized in one place.”
Pyrefly can also handle different settings for different parts of your project if needed.
Features and Usability
Pyrefly can already spot a wide range of error types, similar to older, more established tools like mypy and Pyright. It even has features to help you switch over from these tools pretty easily. For a relatively new project, Pyrefly is surprisingly well-developed. It has:
- Detailed documentation to help you learn how to use it.
- An extension for Visual Studio Code (a popular code editor), so it can check your code right as you type.
- An online “sandbox” where you can try it out without installing anything.
You can even try Pyrefly on your code using a tool called uvx
without a full installation.
Lila: “Okay, so many new terms! What are uvx
and venv
? You mentioned venv
when talking about uvx causing errors.”
John: “Good catch, Lila! Let’s break those down.
venv
(short for ‘virtual environment’): Imagine you’re a chef, and for each different dish (Python project) you cook, you want a completely separate, clean kitchen counter with only the specific ingredients and tools for that dish. Avenv
does this for your Python projects. It creates an isolated space so that the tools and libraries for one project don’t interfere with another.uvx
: This is part of the ‘uv’ toolset we mentioned earlier. Think ofuvx
as a way to quickly ‘test drive’ a Python tool. Instead of fully installing Pyrefly into your project’s ‘kitchen’ (yourvenv
),uvx pyrefly
lets you run it almost like you’re trying a sample at a store. The article mentions that if you useuvx
, it sets up its own temporary environment, which might sometimes conflict if your project is already set up with a differentvenv
, potentially leading to some confusing error messages that aren’t really about your code.”
Taking a Look at: Ty
Ty, from Astral (the creators of ruff and uv), is also in its early days, and this is a bit more apparent. Its documentation isn’t as complete as Pyrefly’s, and it doesn’t have quite as many features yet. But, it’s important to remember it was only recently made public.
Installation and Configuration
You can install Ty using ‘pip’ (Python’s standard package installer) or run it using uvx
, similar to Pyrefly. It’s clever enough to find your source code in a project that’s configured with a pyproject.toml
file. However, its configuration options are more basic than Pyrefly’s. For example, if you want to tell Ty to ignore certain files, you might have to list them in a file like .gitignore
instead of in Ty’s own configuration.
Lila: “What’s a .gitignore
file, John?”
John: “Ah, .gitignore
is a file commonly used with ‘Git’, which is a very popular system for tracking changes in code (version control). Think of Git as a meticulous historian for your project, saving snapshots of your work. The .gitignore
file is a list you give to this historian, telling it, ‘Please don’t pay attention to these specific files or folders.’ These might be temporary files, personal notes, or files that get generated automatically. So, Ty can use this existing ‘ignore list’ to know which files it shouldn’t try to type check.”
Rules and Error Reporting
Ty’s set of rules for checking files seems a bit smaller than Pyrefly’s or other existing tools, though it does have some unique checks. For example, it might not check for certain common errors related to ‘async’ code (a way to write Python that can do multiple things at once), but it can detect very specific issues with how classes (blueprints for creating objects in code) are defined.
Despite being new, Ty has a couple of important features ready:
- It’s compatible with something called the Language Server Protocol (LSP).
- It offers a VS Code extension that uses this LSP.
Lila: “Language Server Protocol? LSP? That sounds very technical, John!”
John: “It does, but the idea behind it is to make life easier for developers, Lila! Imagine you have different brands of smart home devices (like your code editor, e.g., VS Code) and you want them all to understand commands from a central smart home hub (the language tool, like Ty or Pyrefly). The Language Server Protocol, or LSP, is like a universal language or a set of standard commands that these ‘hubs’ and ‘devices’ can use to talk to each other. So, if a tool like Ty ‘speaks LSP,’ it means code editors that also ‘speak LSP’ (like VS Code) can easily integrate with it to get features like real-time error checking, auto-completion suggestions, and so on, without needing a custom plugin for every single tool-editor combination.”
A big plus for Ty, even at this early stage, is how detailed its error reports are. While Pyrefly tells you the line number and type of error, Ty often gives more context, similar to how Python itself explains errors, which can be very helpful for figuring out what went wrong.
So, Pyrefly or Ty? The Current Score
When it comes to speed, both these Rust-powered tools are pretty much on par – they’re fast! Right now, though, Pyrefly seems to be the more immediately useful tool. It has more features, better documentation, and helpful tools for switching from other type checkers or for adding type checking to existing projects.
However, Ty is still very young. It’s definitely one to watch, and it will be interesting to see how both tools develop as they move out of their early “alpha” and “beta” testing phases.
Some Final Thoughts…
John: “It’s really fascinating to see this trend of Rust being used to build high-performance tooling for Python. It shows a real commitment to making the Python development experience smoother and faster. For us Python writers, having quicker, more responsive error checking is a huge win. It’s like having a super-efficient editor go over your shoulder!”
Lila: “As someone still learning, the idea of tools that can catch my mistakes quickly sounds amazing! The ‘flood of errors’ part with Pyrefly sounds a bit intimidating, but knowing there’s a way to tackle them gradually is reassuring. And detailed error messages from Ty sound super helpful for understanding what I did wrong. It’s exciting to hear that even the tools for learning are getting better!”
This article is based on the following original source, summarized from the author’s perspective:
Pyrefly and Ty: Two new Rust-powered Python type-checking
tools compared