The Tech Buffet #11: Ruff - A Blazing Fast Linter and Code Formatter For Python
Unleash the power of Rust.
Hello again 👋 it’s Ahmed. I write The Tech Buffet to share practical tips and tutorials helping you build industrial-grade ML applications.
Subscribe for exclusive content 👇
If you use Python, you probably use a linter or a code formatter that checks the quality of your codebase.
This enforces industry standards and normalizes the code quality across the developers of the same team.
I’ve personally always used Black, isort, and Flake8 in large codebases.
In this issue, I want to share a promising alternative that is 10 to 100 times faster than those tools with additional benefits.
It’s called Ruff
What is Ruff?
Ruff is an extremely fast Python linter and code formatter, written in Rust.
If you don’t know what Rust is, it is a multi-paradigm, general-purpose programming language that emphasizes performance, type safety, and concurrency.
The main selling point of Ruff is its speed. It is insanely fast. In large codebases, this makes a huge difference by speeding up the developer’s workflow.
Multiple benchmarks have been conducted (I read this one on Reddit) and all agree that this tool is 10-100x faster than existing linters (like Flake8) and formatters (like Black).
You should consider moving to Ruff
Ruff has taken the Python community by storm and is now becoming the go-to solution that combines linting and formatting in the same place.
Here are its main features:
🐍 It can be installed with
pip
orpoetry
🛠️ It provides support for
pyproject.toml
🤝 It’s compatible with modern versions of Python (3.12 at this time of writing)
⚖️ It’s 99% compatible with Black and 30x faster
📦 It has a caching mechanism and reanalyzes unchanged files only
🔧 It spots linting errors like traditional linters and fixes them too
📏 It has over 700 built-in rules
⌨️ It integrates well with the developer’s tool (Jetbrain, Vscode, etc.)
🌎 It’s mono-repo-friendly
Ruff is under active development and is supported by a large community of developers.
It’s also used in major open-source projects:
Getting started
You can install Ruff with pip (or any other package manager):
pip install ruff
And you can start using it for lining or formatting from the terminal.
To run Ruff as a linter, you can use one of these commands:
If Ruff detects linting errors, you can ask it to fix them with this command:
ruff check . --fix
To run Ruff as a formatter, you can use one of these commands:
You can also use Ruff in a pre-commit hook via ruff-pre-commit.
Not familiar with pre-commit hooks? Check out this issue when I cover them in details.
Configuration
To determine the appropriate settings for each Python file, Ruff looks for the first pyproject.toml
, ruff.toml
, or .ruff.toml
file in the file's directory or any parent directory.
To configure Ruff, let's create a pyproject.toml
file in our project's root directory. In this example, we’ll a specific rule named line-too-long and defined by the E501 code.
[project]
# Support Python 3.10+.
requires-python = ">=3.10"
[tool.ruff]
# Set the maximum line length to 79.
line-length = 79
[tool.ruff.lint]
# Add the `line-too-long` rule to the enforced rule set.
extend-select = ["E501"]
Running Ruff again, we see that it now enforces a maximum line width, with a limit of 79:
❯ ruff check .
numbers/numbers.py:5:80: E501 Line too long (90 > 79)
Found 1 error.
For a full enumeration of the supported settings, see Settings.
If you want a detailed tutorial on setting up Ruff for your project, have a look a this link.
Resources:
This was a quick issue! Thanks for reading. 👋