Sub topic 1
What Ruff is and what it replaces
Prerequisites
This chapter assumes you can run Python scripts, use a terminal and a virtual environment, install packages with pip, and read a simple TOML file such as pyproject.toml. You do not need prior experience with any linter or formatter.
What Ruff is
Ruff is a single command-line tool that does two jobs for Python code:
- Linting:
ruff checkreads your code without running it and reports likely bugs, unused names, risky patterns and style problems. - Formatting:
ruff formatrewrites your code into one consistent layout.
Ruff is written in Rust and ships as a compiled program, so it runs much faster than the Python-based tools it replaces. You install it with pip, but it does not run on the Python interpreter.
What it replaces
Before Ruff, a typical project combined several tools. Each one had its own configuration and its own release schedule. Ruff covers most of that stack.
| Job | Common older tool | Ruff equivalent |
|---|---|---|
| Lint checks | Flake8 and its plugins | `ruff check` |
| Import sorting | isort | `ruff check` with the `I` rules |
| Code formatting | Black | `ruff format` |
| Syntax upgrades | pyupgrade | `ruff check` with the `UP` rules |
| Bug-prone patterns | flake8-bugbear | `ruff check` with the `B` rules |
Ruff reimplements the rules of these tools, so the rule codes you may already know, such as F401 from Flake8, keep their meaning. The formatter aims for output that is almost identical to Black's. You will see small differences in rare cases, so check your diff when you switch.
A first look
Install Ruff and run both commands on a project:
pip install ruff
ruff check .
ruff format .Here is a small file that ruff check flags with its default rules:
import os
import sys
def total(items):
unused = 1
result = 0
for i in items:
result += i
if result == None:
return 0
return result
print(sys.argv)Ruff reports three problems: F401 (the import os is never used), F841 (the local variable unused is assigned but never used) and E711 (comparing to None with == instead of is). Each report gives the file, line, rule code and a short message. Later chapters cover how to read these codes and how to fix them.
What Ruff does not do
Knowing the limits helps you decide when Ruff is enough.
- It is not a type checker. Ruff does not replace mypy or pyright. It does not check that the types in your code agree.
- It does not cover every plugin. Ruff supports many Flake8 plugins, but not all of them. Check the rule list for the plugins you depend on before you drop them.
- It does not run your code. All checks are static, so Ruff cannot find problems that only appear at runtime.
Why teams adopt it
- Speed. A full check of a large codebase usually takes a fraction of a second, which makes it practical to run on every save and every commit.
- One configuration. Settings for linting and formatting live in one place, normally
pyproject.toml. - Fewer dependencies. You install and pin one package instead of several tools and their plugins.
- Automatic fixes. Many rules can fix the code for you, which the autofix chapter covers.
Exercise
A project uses Flake8, isort and Black. Which Ruff commands cover these three tools, and which common tool is not replaced?
Show answer
ruff check covers Flake8 (lint rules) and isort (through the I rules), and ruff format covers Black. Ruff does not replace a type checker such as mypy or pyright.