Sub topic 6
Choosing which rules run
This chapter assumes you can run ruff check, read a rule code such as F401, and edit a [tool.ruff] section in pyproject.toml. It covers how Ruff decides which rules are active, how to change that set, and the mistakes that most often cause surprises.
The default rule set
With no configuration, Ruff enables a small set of rules. In current releases that set is E4, E7, E9 and F. These are a subset of the pycodestyle (E) rules plus all Pyflakes (F) rules. They catch real bugs, such as unused imports, undefined names and syntax errors. They skip most style opinions.
Check what is active instead of trusting memory. Defaults can change between releases.
ruff check --show-settings | head -n 60
ruff rule F401
ruff linterruff check --show-settingsprints the resolved configuration for a file, including the enabled rules.ruff rule F401prints documentation for one rule.ruff linterlists every rule family (linter) with its prefix.
How selection works
Ruff builds the active rule set in layers. Each layer adds or removes rules from the one before it.
- Start with the default set, or with whatever the parent configuration selected.
- Apply
select, which replaces the starting set. - Apply
extend-select, which adds to the set. - Apply
ignoreandextend-ignore, which remove rules. - Apply
per-file-ignores, which remove rules for matching paths. - Apply command-line flags such as
--selectand--ignore, which override the file.
A rule is specified by a code or a prefix. F401 names one rule. F4 names every rule whose code begins with F4. F names the whole Pyflakes family.
When a select entry and an ignore entry both match a rule, the more specific one wins. Selecting E and ignoring E501 runs all E rules except E501. Selecting E501 and ignoring E also keeps E501, because the selector is longer than the ignore prefix.
select versus extend-select
select replaces the default set. extend-select adds to it. Mixing them up is the most common mistake in a first configuration.
# Replaces the defaults. Only import sorting runs.
[tool.ruff.lint]
select = ["I"]This configuration stops checking for unused imports and undefined names, because F is no longer selected. If you want the defaults plus import sorting, use extend-select:
[tool.ruff.lint]
extend-select = ["I"]Use select when you want the whole rule set written down in the file, so a reader can see exactly what is enforced. Use extend-select when you want to keep Ruff's defaults and add a few families on top. The first option is more explicit. The second option picks up any change Ruff later makes to its defaults.
Rule families worth knowing
Each family comes from a tool Ruff reimplements. The table lists the ones teams usually enable first.
| Prefix | Origin | What it catches |
|---|---|---|
| `E`, `W` | pycodestyle | Whitespace, line length, syntax-level style |
| `F` | Pyflakes | Unused imports and variables, undefined names |
| `I` | isort | Import ordering and grouping |
| `B` | flake8-bugbear | Likely bugs, such as mutable default arguments |
| `UP` | pyupgrade | Syntax that newer Python versions replace |
| `SIM` | flake8-simplify | Code that can be written more simply |
| `C4` | flake8-comprehensions | Unnecessary `list()` or `dict()` calls |
| `S` | flake8-bandit | Common security problems |
Adding a family in one go can produce hundreds of violations in an existing project. Add one family at a time and fix its violations before adding the next.
A worked example
The following configuration enables the defaults plus import sorting, bugbear and pyupgrade, and ignores one rule project-wide.
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B", "UP"]
ignore = ["B008"]Here is a file to test it against.
from typing import List
def collect(items: List[int], seen=[]) -> List[int]:
for item in items:
seen.append(item)
return seenRun ruff check example.py. Ruff reports UP006 and UP035 for the List annotations, because target-version is py311 and list[int] is available. It reports B006 for the mutable default seen=[]. B006 matters because the same list is shared by every call that omits the argument. It is a real bug, not a style complaint.
B008 is ignored in the configuration above, so Ruff will not flag function calls in argument defaults. Projects that use frameworks such as FastAPI ignore it because those frameworks expect calls like Depends() in defaults.
Using `ALL` and its costs
select = ["ALL"] enables every stable rule. Some teams start there and then ignore what they dislike. That approach has two costs.
- Some rules conflict with each other. For example,
D203andD211give opposite advice about blank lines before a class docstring, and Ruff warns when both are on. - Each Ruff upgrade can add rules, so a passing build can start failing after you update the tool.
If you use ALL, pin the Ruff version in your dependencies and update it on purpose. Otherwise prefer an explicit list.
Preview and stability
New rules first appear in preview mode. Preview rules can change behavior or be renamed before they become stable. Ruff does not run them unless you opt in.
[tool.ruff.lint]
preview = true
explicit-preview-rules = true
extend-select = ["CPY001"]With explicit-preview-rules = true, a preview rule runs only when you name its full code. A broad prefix such as CPY does not turn it on. Without that setting, preview = true makes prefixes pick up preview rules too, which can add new violations after an upgrade.
Deprecated and removed rules
Ruff sometimes deprecates a rule and later removes it. Selecting a removed code raises a configuration error, not a warning. If an upgrade starts failing with an unknown rule code, run ruff rule CODE to see whether it was renamed, then update the entry.
Command-line overrides
Flags are useful for one-off checks without editing the configuration.
ruff check --select B .
ruff check --extend-select SIM .
ruff check --ignore E501 .
ruff check --statistics .--select B runs only bugbear rules for that invocation, which is a quick way to audit one family. --statistics prints a count of violations per rule. Use it before enabling a new family to see how large the cleanup would be.
Common errors
- Using
selectwhen you meantextend-select. The defaults disappear, and checks you relied on stop running. Compareruff check --show-settingsbefore and after a change. - Ignoring a whole family to silence one rule.
ignore = ["E"]disables dozens of rules. Ignore the specific code instead. - Placing settings in the wrong table. Rule settings belong under
[tool.ruff.lint]. Older configurations putselectdirectly under[tool.ruff]. Current Ruff warns about the old location, so move those keys. - Mistyped codes. An unknown code produces an error. A mistyped prefix that happens to be valid may select a different family than you intended, so check with
ruff rule. - Forgetting the command line wins. If CI passes
--select, it ignores theselectinpyproject.toml.
A practical process for a new project
- Run
ruff check .with the defaults and fix everything. - Pick one family, for example
I, and runruff check --select I --statistics .to see the size of the cleanup. - Add the family with
extend-select, fix the violations, and commit. - Repeat for
BandUP. - When the list stabilizes, switch to an explicit
selectso the file documents what is enforced.
Exercise
A project has select = ["I"] under [tool.ruff.lint]. A teammate adds import os to a module and never uses it, yet ruff check reports nothing. Why, and what is the smallest change that makes Ruff report it while keeping import sorting?
Show answer
select replaces the default rule set, so only the I rules run and F401 (unused import) is no longer selected. Change select = ["I"] to extend-select = ["I"] so the defaults, including F, stay active, or change the list to select = ["E4", "E7", "E9", "F", "I"].
Exercise
Your configuration has select = ["E"] and ignore = ["E501"]. You also run ruff check --select E501 . in CI. Is E501 checked in CI, and why?
Show answer
Yes. Command-line flags override the configuration file, so --select E501 replaces the file's select. With that selection, E501 is the only rule chosen and the file's ignore of the same code is a less specific match than the exact selector, so the rule runs.