Sub topic 4
Reading rule codes and prefixes
This chapter assumes you can run ruff check on a project and read its output, and that you know the basic layout of pyproject.toml. You will learn what a rule code is made of, how prefixes group rules, and how to look up any rule you meet.
Anatomy of a rule code
Every Ruff lint rule has a code, such as F401 or E711. A code has two parts:
- Letters (the prefix) name the linter or plugin the rule comes from.
Fis Pyflakes,Eis pycodestyle errors,Bis flake8-bugbear. - Digits identify one rule inside that linter.
F401is "unused import" andF841is "unused variable".
Some prefixes have more than one letter, such as UP (pyupgrade) and PL (Pylint). Some have digits in the prefix itself: C90 is the mccabe complexity check, and E4 is a subgroup of the E rules.
Ruff reimplements many separate tools in one binary. The prefix tells you which of those tools a rule came from, and that tells you where to find the original documentation and rationale.
Reading the output
Run Ruff on the sample file from the overview and you get lines like these (the exact layout depends on your Ruff version and output format):
example.py:1:8: F401 [*] `os` imported but unused
example.py:6:5: F841 Local variable `unused` is assigned to but never used
example.py:10:15: E711 Comparison to `None` should be `cond is None`Each line gives the file, the line and column, the code, a message, and sometimes a [*] marker. The marker means Ruff has an automatic fix for that violation. Chapter "Fixing violations automatically" covers fixes. For now, treat [*] as "this can be fixed by ruff check --fix".
The code is what you copy into configuration, noqa comments and bug reports. Learn to find it quickly in the output.
Looking up a rule
Do not guess what a code means. Ask Ruff.
ruff rule F401
ruff rule E711
ruff linter
ruff check --statistics .ruff rule CODEprints the rule's name, the linter it belongs to, what it checks, why it matters, an example, and whether it is fixable.ruff linterlists every linter Ruff supports with its prefix, so you can see which letters map to which tool.ruff check --statistics .prints a count of violations per code. This is the quickest way to see which rules dominate in a codebase you have just opened.
Each rule also has a kebab-case name, for example unused-import for F401. The name appears in the statistics output and the documentation. The code is the stable identifier you use in configuration.
Common prefixes
You will see these often. The table is a reading aid, not a recommendation to enable them all.
| Prefix | Linter | What it covers |
|---|---|---|
| `E` | pycodestyle (errors) | Style problems such as comparison to `None`, bare `except`, line length |
| `W` | pycodestyle (warnings) | Whitespace problems such as trailing whitespace |
| `F` | Pyflakes | Unused imports and variables, undefined names, syntax-level mistakes |
| `I` | isort | Import ordering and grouping |
| `B` | flake8-bugbear | Likely bugs and risky patterns, such as mutable default arguments |
| `UP` | pyupgrade | Syntax that newer Python versions replace with something better |
| `N` | pep8-naming | Naming conventions for classes, functions and variables |
| `C90` | mccabe | Function complexity |
| `S` | flake8-bandit | Common security problems |
| `SIM` | flake8-simplify | Code that can be written more simply |
| `PL` | Pylint | A subset of Pylint checks, split into `PLC`, `PLE`, `PLR`, `PLW` |
Prefixes can nest. PL contains PLE (errors), and E contains E4, E7 and E9. A longer prefix is always a smaller set of rules than a shorter prefix that begins with the same characters.
Prefixes as selectors
Anywhere Ruff accepts a rule code, it also accepts a prefix. A prefix selects every rule whose code starts with it.
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B"]
ignore = ["B008"]Here is what each entry means:
"F"enables every Pyflakes rule, including ones added in future Ruff releases."E4"enables only the pycodestyle rules whose code starts withE4, which are import-related checks."B008"is a full code, so it removes that single rule.
You can also use a prefix on the command line:
ruff check --select F,I .
ruff check --select F401 .
ruff check --extend-select B .--select replaces the configured selection for that run. --extend-select adds to it. The special selector ALL enables every stable rule, which is rarely what you want as a starting point.
Resolving conflicts: the most specific selector wins
A rule can be matched by several selectors at once. If you select E and ignore E501, which wins? Ruff compares how specific each matching selector is, meaning the length of the prefix, and the more specific one decides.
[tool.ruff.lint]
select = ["E", "F"]
ignore = ["E501"]E501 (line too long) matches both "E" in select and "E501" in ignore. The ignore entry is longer, so the rule is off. The same logic works in reverse:
[tool.ruff.lint]
select = ["E501", "F"]
ignore = ["E"]Now "E501" in select is more specific than "E" in ignore, so E501 stays on and the rest of E is off. When two selectors have the same length, as when a code appears in both lists, ignore takes effect.
This rule is why order inside the lists does not matter. Specificity decides, not position.
Pitfalls
Several mistakes come up often when people work with codes and prefixes.
- Selecting a whole family blindly.
select = ["B"]is safe.select = ["ALL"]or["PL"]can turn on hundreds of rules at once and produce thousands of violations in an existing codebase. Start small and add prefixes you can explain. - Prefixes that grow. A broad prefix picks up new rules when you upgrade Ruff. A project that selects
"F"may see new findings after an upgrade. Pin the Ruff version, or select full codes if you need a stable result. - Typos in codes. Ruff warns about unknown rule selectors and may refuse to run with an invalid one. Read those warnings. A misspelled code in
ignoredoes nothing useful. - Assuming a letter means one tool.
Eis pycodestyle, butEMis flake8-errmsg andERAis eradicate. The prefix is the whole letter sequence before the digits, not its first character. - Preview rules. Some rules are marked preview. They are not enabled by selecting their prefix alone. You must also enable preview mode, and their behavior can change between releases.
ruff rule CODEstates whether a rule is in preview.
A worked example
Suppose you inherit a project and want to adopt Ruff step by step. First, see what the defaults report:
ruff check --statistics .Say the result shows many F401 and a few E711. Add the import-sorting and bugbear families and check the effect without editing any config:
ruff check --extend-select I,B --statistics .If B008 produces false alarms for a web framework that uses function calls in default arguments, ignore that one code and keep the rest of the family:
[tool.ruff.lint]
extend-select = ["I", "B"]
ignore = ["B008"]You have enabled two families, disabled one rule in them, and documented the reason in your commit message. The next chapter covers the pyproject.toml settings in detail, and the one after covers rule selection as a workflow.
Exercise
Your config contains select = ["E", "F"] and ignore = ["E501", "F"]. Which of these rules are active: E711, E501, F401? Explain your answer.
Show answer
Only E711 is active. E711 matches "E" in select and nothing in ignore, so it runs. E501 matches "E" in select but "E501" in ignore, which is longer, so it is off. F401 matches "F" in select and "F" in ignore. The two selectors have the same length, and ignore takes effect, so it is off.
Exercise
A colleague's config says select = ["I"] and they now see no unused-import warnings, although the project used to report them. What happened, and how do you fix it?
Show answer
Setting select replaces Ruff's default rule set (E4, E7, E9, F), so the Pyflakes rules, including F401, are no longer selected. Change select to extend-select = ["I"], or list the defaults explicitly in select along with "I".