Sub topic 6
Choosing which rules run
This chapter assumes you can run ruff check, read the rule codes it prints (such as F401), and edit a [tool.ruff] section in pyproject.toml. Here you control which rules Ruff applies to your project.
The default rule set
With no configuration, Ruff runs a small set of rules: E4, E7, E9 and F. These cover pyflakes checks (unused imports, undefined names) and a subset of pycodestyle errors (syntax errors, comparisons such as == None). The set is small on purpose, so a new project starts with few warnings.
You change it with three settings under [tool.ruff.lint]:
selectreplaces the default set with the rules you list.extend-selectadds rules to whatever is already selected.ignoreremoves rules from the selected set.
Each entry is a full code (E711) or a prefix (E7, E, F). A prefix selects every rule that begins with it.
Replacing or extending
Use select when you want to state the complete list yourself:
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B"]
ignore = ["B008"]This keeps the four default families and adds import sorting (I) and bugbear (B) rules. It then drops B008, a rule about function calls in argument defaults.
Use extend-select when you only want to add to the defaults:
[tool.ruff.lint]
extend-select = ["I", "B", "UP"]extend-select follows future changes to Ruff's defaults. A full select list does not, so you decide when a new default family applies.
What happens when rules conflict
If a code is both selected and ignored, the more specific entry wins. With select = ["E", "F"] and ignore = ["E501"], every E rule runs except E501. If you select E501 and ignore E, then E501 still runs, because its entry is more specific.
Trying selections from the command line
Command-line flags override the config file for one run. This lets you test a selection before you commit it.
ruff check --select F,I .
ruff check --extend-select B .
ruff check --ignore E711 .
ruff rule E711
ruff linterruff rule E711 prints the explanation for one rule. ruff linter lists every rule family and its prefix.
Common errors
- Using
selectand losing defaults. Settingselect = ["I"]turns offFandE. Unused imports stop being reported. If you only want to add rules, useextend-select. - Selecting
ALL.select = ["ALL"]enables every rule, including ones that conflict with each other and with the formatter. New Ruff releases also add rules, so a version bump can produce new violations. Prefer an explicit list. - Mistyped codes. An unknown code such as
"F4010"in the config stops Ruff with an error. This is useful, because a typo can't silently disable a check. - Ignoring too broadly.
ignore = ["E"]hides everyErule. Ignore the single code you need, and add a comment in the config saying why.
Checking your result
Run ruff check . after each change to the selection. If the number of violations rises or falls in a way you did not expect, compare your select, extend-select and ignore lists against the flow above.
Exercise
Your config has select = ["F", "B"] and ignore = ["F401"]. Ruff reports no unused imports, and you also notice that E711 (comparison to None) is no longer reported. Name two changes: one that restores E711 while keeping everything else, and one that restores unused import reports.
Show answer
Restore E711 by switching to extend-select = ["B"] (so the default E4, E7, E9 and F families stay on) or by adding "E7" to select. Restore unused import reports by removing F401 from ignore. The first change works because E711 belongs to the E7 family, which select = ["F", "B"] had replaced.