Sub topic 1
What Ruff is and what it replaces
Ruff is a Python linter and code formatter written in Rust and distributed as a single executable. It is developed by Astral. One tool, one configuration file, and one install replace a stack of separate tools that most Python projects used to assemble by hand. This chapter explains what Ruff does, which tools it replaces, which it does not, and how the rest of this tutorial is organized around its two commands.
Prerequisites
This chapter assumes you already have the following:
- Basic Python syntax, and experience running scripts.
- Comfort with a terminal and a virtual environment.
- Experience installing packages with
pipor a similar tool. - Enough TOML to read a
pyproject.tomlfile.
You do not need experience with Flake8, Black, or isort. If you have used them, the comparisons below will be more concrete. Later chapters cover installing Ruff and configuring it in detail. Here you only need the big picture.
Two jobs: linting and formatting
Ruff does two separate jobs, and each has its own subcommand.
- A linter reads your code without running it and reports problems. Examples are an imported module that is never used, a variable assigned but never read, or a comparison written as
x == None. The linter isruff check. - A formatter rewrites your code into a consistent layout. It changes quotes, spacing, line breaks and blank lines, and it never changes what the program does. The formatter is
ruff format.
The two jobs differ in an important way. Lint rules report a violation and sometimes offer a fix. The formatter has no violations to report. It produces one canonical layout for any given program.
pip install ruff
ruff check .
ruff check --fix .
ruff format .
ruff format --check .
ruff rule F401The commands do the following:
ruff check .lints every Python file under the current directory.ruff check --fix .also applies the safe automatic fixes.ruff format .rewrites files in place.ruff format --check .reports which files would change, without changing them. This is the form you use in CI.ruff rule F401prints the documentation for one rule.
A first look at what Ruff reports
Save this file as example.py. It is valid Python that runs, but it contains three problems.
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)Running ruff check example.py with the default settings reports these violations:
| Code | Meaning | Line content |
|---|---|---|
| F401 | `os` is imported but never used | `import os` |
| F841 | Local variable is assigned but never used | `unused = 1` |
| E711 | Comparison to `None` should use `is` | `if result == None:` |
Each code is a letter prefix followed by digits. The prefix names the rule family and the digits name the rule. The letters come from the tool whose rule Ruff reimplemented. Chapter 4 covers codes in detail. For now, remember that F rules come from Pyflakes and E rules come from pycodestyle.
What Ruff replaces
Before Ruff, a typical project combined several tools. Each had its own install, its own configuration, and its own run time. Ruff reimplements the behavior of most of them.
| Previous tool | Job | Ruff equivalent |
|---|---|---|
| Flake8 | Runs lint checks | `ruff check` |
| Pyflakes, pycodestyle | Rule sets inside Flake8 | Rule families `F` and `E`/`W` |
| Flake8 plugins (bugbear, comprehensions, and others) | Extra lint rules | Rule families such as `B` and `C4` |
| isort | Sorts imports | Rule family `I` |
| pyupgrade | Rewrites old syntax to newer syntax | Rule family `UP` |
| autoflake | Removes unused imports and variables | Autofix for `F401` and `F841` |
| pydocstyle | Checks docstring style | Rule family `D` |
| Black | Formats code | `ruff format` |
Three consequences follow from this consolidation:
- One configuration. Settings for all of these jobs live in one
[tool.ruff]table inpyproject.toml, or inruff.toml. Flake8 historically could not readpyproject.tomlwithout a plugin. - One parse. Each file is parsed once, and the linter and formatter share that work. Separate tools each parse every file again.
- Speed. Ruff is written in Rust and caches results in a
.ruff_cachedirectory. On large codebases it runs far faster than the Python-based tools it replaces. The difference is often large enough that you can run it on every save instead of only in CI.
How the pieces fit together
The diagram shows how one Ruff install covers the old toolchain. The linter and formatter share the same configuration source.
The formatter in one example
ruff format follows the Black style and is designed as a drop-in replacement for it. The Ruff documentation describes the output as highly compatible with Black on typical code, but not guaranteed identical in every case. A small before and after shows the kind of change it makes.
# Before ruff format
x = { 'a':1,
'b':2 }
def f(a,b):
return a+b
# After ruff format
x = {"a": 1, "b": 2}
def f(a, b):
return a + bThe formatter normalized the quotes to double quotes. It added spaces after colons and commas and around the operator. It collapsed the dictionary onto one line because it fits. It also put two blank lines before the function.
What Ruff does not replace
Ruff covers style, correctness heuristics and formatting. It does not cover everything a Python project may run.
- Type checking. Ruff does not check types. Keep mypy, pyright, or another type checker. Some Ruff rules inspect type annotations syntactically, such as the
UPrules that modernize them, but that is not type inference. - Complete Flake8 plugin coverage. Ruff has reimplemented the rules from many popular plugins, but not all of them. It has no plugin API, so third-party plugins cannot be loaded. If your project depends on a niche plugin, check whether Ruff has an equivalent rule before you remove it.
- Full Pylint behavior. Ruff has a
PLrule family inspired by Pylint. It covers only a subset, and it cannot do the cross-file inference that some Pylint checks rely on. - Runtime testing. Linting finds patterns that often indicate bugs. It does not replace tests.
A practical rule is to treat Ruff as the replacement for your style and static-pattern tools, and to keep your type checker and test runner.
Common errors when adopting Ruff
These mistakes appear often when teams first switch.
- Expecting every old warning to appear. Ruff's default rule set is small. It selects
E4,E7,E9andF, which is a subset of what a typical Flake8 setup reports. You get fewer findings until you choose more rule families. Chapter 6 covers this. - Running only one command.
ruff checkdoes not format andruff formatdoes not lint. Projects that adopt only one of them keep half of the old toolchain's gaps. - Assuming
ruff check --fixfixes everything. Ruff classifies fixes as safe or unsafe. Only safe fixes apply by default. Chapter 7 explains the difference. - Keeping the old tools running. Leaving Black or isort in your pre-commit configuration alongside Ruff can create two tools that disagree about the same file. Remove the replaced tool when you add the Ruff equivalent.
Where the rest of the tutorial goes
The remaining chapters follow the order in which you will use Ruff. You install it and run the linter. You read rule codes, configure pyproject.toml, and choose which rules run. You then apply fixes, format code, suppress violations, and sort imports. After that you handle linter and formatter conflicts, automate Ruff with pre-commit and CI, set up your editor, and migrate from Black, Flake8 and isort.
Exercise
A teammate says, "Ruff replaces our whole quality toolchain: Flake8, isort, Black and mypy." Which of those four does Ruff not replace, which Ruff commands cover the other three, and why should you be careful about removing Flake8 plugins?
Show answer
Ruff does not replace mypy, because it is not a type checker. ruff check covers Flake8 (with the F and E rule families) and isort (with the I rules), and ruff format covers Black. Be careful with Flake8 plugins because Ruff has no plugin API and reimplements only some popular plugins, so a plugin you depend on may have no Ruff equivalent. Check for an equivalent rule before removing it.
Exercise
You run ruff check example.py on the sample file in this chapter and see three violations. Name the three codes, and say which command you would run in CI to confirm that all files are already formatted without modifying them.
Show answer
The codes are F401 (unused import os), F841 (unused local variable unused) and E711 (comparison to None with ==). In CI you would run ruff format --check ., which reports files that would change and exits with a non-zero status, without rewriting them.