Are you even a python dev if you are not using ruff?
Ruff is a powerful tool which replaces various linters and formatters. It is so fast that a second will feel like an hour.
I guess that’s enough of a clickbait title except the exaggeration of ruff is for real. I heard about ruff when someone in the company teams channel posted about it. I checked the tool one fine afternoon and was amazed by the blazing fast 🚀 speed. I soon found myself to be voting to get ruff in to ci-cd pipeline. Ruff replaces linter and formatting tools like pylint, flake8, isort and black. One tool fits all, well the most I would say. Ruff does not yet replace static checker tools like mypy.
A little background about ruff. It is developed by Charlie Marsh to meet his needs for fast python linters. Anyone who works on a python project know how the speed creep can impact as the project grows with the existing linters like pylint and flake8. His motto is simple:
I had a hunch that better tools were possible — so I built the thing I wanted for my own work.
Charlie also created uv, a light weight alternative to venv written in rust. Both ruff and uv are written in rust, hence they’re fast and not memory hungry. He is the founder of a startup called Astral, a company dedicated to create and support high performance python tools. It’s funded by coveted VCs like Accel, Caffeinated Capital, Guillermo Rauch
I’ve experienced ruff’s speed over a large project at my job. I would say within a blink of an eye the tool is done linting and formatting. Well that won’t suffice the nerd in us, right? So here’s the benchmarking from ruff’s documentation.
Ok now enough of bragging about ruff. If you are looking to get started quickly in ruff, here’s the commands for linter and the formatter.
Ruff linter:
Ruff check is going to be your primary entry point to the linter
ruff check # Lint all files in the current directory. ruff check --fix # Lint all files in the current directory, and fix any fixable errors. ruff check --watch # Lint all files in the current directory, and re-lint on change. ruff check path/to/code/ # Lint all files in `path/to/code` (and any subdirectories).
Ruff format:
ruff format is going to be your primary entry point to the formatter
ruff format # Format all files in the current directory. ruff format path/to/code/ # Format all files in `path/to/code` (and any subdirectories). ruff format path/to/file.py # Format a single file.
Sorting Imports
ruff does not support sorting imports and formatting in one command yet, but here’s how you can accomplish sorting of import and formatting in ruff
ruff check --select I --fix ruff format
Ruff also supports pyproject.toml configuration. To close off the post I’ll drop in some testimonials taken directly from ruff’s website:
Sebastián Ramírez, creator of FastAPI:
Ruff is so fast that sometimes I add an intentional bug in the code just to confirm it's actually running and checking the code.
Nick Schrock, founder of Elementl, co-creator of GraphQL:
Why is Ruff a gamechanger? Primarily because it is nearly 1000x faster. Literally. Not a typo. On our largest module (dagster itself, 250k LOC) pylint takes about 2.5 minutes, parallelized across 4 cores on my M1. Running ruff against our entire codebase takes .4 seconds.
Bryan Van de Ven, co-creator of Bokeh, original author of Conda:
Ruff is ~150-200x faster than flake8 on my machine, scanning the whole repo takes ~0.2s instead of ~20s. This is an enormous quality of life improvement for local dev. It's fast enough that I added it as an actual commit hook, which is terrific.
Timothy Crosley, creator of isort:
Just switched my first project to Ruff. Only one downside so far: it's so fast I couldn't believe it was working till I intentionally introduced some errors.
Tim Abbott, lead developer of Zulip:
This is just ridiculously fast...
ruff
is amazing.
I hope by now I have convinced you to atleast google ruff and peek into the getting started link. Like me I hope you’ll be amazed at the speed of the tool on one fine afternoon after finishing your work or ticket at hand 😜. Anyways happy coding!