Sub topic 4
Reading rule codes and prefixes
This chapter assumes you can run ruff check and read its output. Here you learn how to read the codes in that output, because rule selection and suppression both depend on them.
Anatomy of a rule code
Every Ruff lint rule has a code made of a letter prefix and a number. In F401, the F says which family of rules the check belongs to, and 401 identifies the rule within that family.
Ruff reimplements checks from many older tools, and each prefix usually maps to one of them. The table lists the prefixes you will meet most often.
| Prefix | Origin | Typical concern |
|---|---|---|
| `E`, `W` | pycodestyle | Style errors and warnings |
| `F` | Pyflakes | Unused imports, undefined names |
| `I` | isort | Import ordering |
| `B` | flake8-bugbear | Likely bugs and design problems |
| `UP` | pyupgrade | Outdated syntax for your target Python version |
| `N` | pep8-naming | Naming conventions |
Prefixes can be one or more letters, and some rules add digits, as in C90 for McCabe complexity. Do not assume the prefix is a single letter.
Reading a diagnostic
Run Ruff on a file that has an unused import, using the concise output format so each violation takes one line:
ruff check --output-format concise example.pyA typical line looks like this:
example.py:1:8: F401 [*] `os` imported but unusedReading left to right, you get the file, line, column, rule code and message. The [*] marker means Ruff has an automatic fix for this violation. The next chapters cover fixes. For now, treat the marker as information.
Looking up a rule
You do not have to memorize what a code means. Ruff documents every rule in the terminal:
ruff rule F401
ruff rule E711
ruff linterruff rule prints the rule's explanation, an example of bad and good code, and whether it has a fix. ruff linter lists every supported linter along with its prefix.
Prefixes select groups
A prefix matches every rule whose code starts with it. This is the main reason prefixes matter.
Fselects every Pyflakes rule.F4selects the Pyflakes rules numbered 4xx, such asF401.F401selects exactly one rule.E7selects the pycodestyle statement rules fromE701upward, includingE711.
You can test the effect from the command line:
ruff check --select F .
ruff check --select E7,F401 .
ruff check --statistics .The --statistics flag prints a count of violations per rule code. It is a quick way to see which rules dominate a codebase before you decide what to enable.
The default rule set
Without configuration, Ruff runs only E4, E7, E9 and F. This is deliberately conservative. It leaves out rules such as E501 (line too long), because the formatter already controls line wrapping.
If you add a family like I or B later, you extend this list rather than replacing the idea of defaults. The next chapters show how to write that in pyproject.toml.
Common mistakes
- Selecting a whole family with a short prefix such as
Ealso enables rules that conflict with the formatter. Prefer narrower prefixes when you are unsure. - A prefix typed wrongly may match nothing or match more than you intended. Check the result with
ruff check --statistics. - Codes are stable identifiers, but the set of rules in each family can grow with new Ruff releases. Pinning the Ruff version keeps your results reproducible.
Exercise
You run ruff check --select E7 . and Ruff reports a comparison to None using ==. Which code is shown, which tool family does it come from, and how can you read the full explanation?
Show answer
The code is E711, which belongs to the E family from pycodestyle. It is selected because E7 is a prefix that matches every code starting with E7. Run ruff rule E711 to read the explanation.