Sub topic 3
Finding problems with ruff check
This chapter assumes you can run commands in a terminal inside a virtual environment, and that you have installed Ruff with pip install ruff. It also assumes you can read Python and a small pyproject.toml. Here you will learn what ruff check does, how to read its output, how its exit codes behave, and how to narrow or widen what it reports.
What ruff check does
ruff check is Ruff's linter. A linter reads source code without running it and reports patterns that are probably mistakes or style problems. Examples are an import that is never used, a variable that is assigned but never read, or a comparison written in a way that can misbehave.
Ruff parses each file into a syntax tree and runs a set of rules over it. Each rule looks for one kind of problem and has a code such as F401. The next chapter covers the code format in detail. For now, treat the code as the name of a rule.
ruff check never reformats your code. Formatting belongs to ruff format, which is a separate command. The one way ruff check can edit files is when you pass --fix, which a later chapter covers.
A first run
Save this file as example.py:
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)Then run the linter on the current directory:
ruff check .With no configuration, Ruff runs a small default rule set: the E4, E7, E9 and F families. That set is enough to catch this file's three problems. The concise output format prints one line per violation:
ruff check --output-format concise example.pyThe output looks like this. The exact wording can differ between Ruff versions.
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`
Found 3 errors.Each line has the same parts:
- The file path.
- The line number, then the column number.
- The rule code.
- A fix marker, if any.
[*]means Ruff can fix the violation automatically. - A message.
Without --output-format concise, recent Ruff versions print the "full" format. It shows each violation with the offending source lines and a caret underline. The information is the same, but it takes more screen space.
Choosing what to check
You can pass files, directories, or both:
ruff check example.py
ruff check src tests
ruff check .When you give a directory, Ruff walks it and checks every Python file it finds. It skips some directories by default, including .git, .venv, __pycache__, node_modules and build. It also respects .gitignore files. The Ruff documentation lists the full default exclusion list.
You can also pipe code in. A - argument tells Ruff to read from standard input, which editors and scripts use:
cat example.py | ruff check --stdin-filename example.py -The --stdin-filename option tells Ruff which path to use when it applies configuration and per-file rules.
Exit codes
Scripts and CI jobs depend on the exit code, so know the three values:
| Exit code | Meaning |
|---|---|
| 0 | No violations were found, or all violations were fixed when `--fix` ran. |
| 1 | At least one violation remains. |
| 2 | Ruff itself failed, for example because of an invalid configuration file or a bad command-line option. |
A CI job usually treats any nonzero code as a failure. Two flags change this behavior:
--exit-zeroalways returns 0 when Ruff runs successfully, even if violations remain. This is useful when you want a report without blocking a build.--exit-non-zero-on-fixreturns 1 if--fixchanged any file, even when every violation was fixed. This is useful in pre-commit and CI, where a changed file means the committed code was not clean.
How a run flows
The diagram shows what happens when you run the command.
A file with a syntax error cannot be fully analyzed. Ruff reports the syntax error and skips the rule checks for that file. Fix the syntax first, then look at the other messages.
Changing the rule set from the command line
The default rules are a conservative starting point. You can override them for a single run with --select, --extend-select and --ignore:
# Run only the pyflakes (F) rules
ruff check --select F .
# Run the defaults plus import sorting and bugbear
ruff check --extend-select I,B .
# Run the defaults but skip one rule
ruff check --ignore E711 .The difference between --select and --extend-select matters:
--selectreplaces the default set with exactly the rules you name.--extend-selectadds rules to whatever is already selected.
Command-line options take precedence over pyproject.toml. Use them for experiments, and write the settled choices into the configuration file so everyone on the project gets the same results.
Getting a summary
On a project with many violations, a long list is hard to act on. --statistics prints a count per rule instead:
ruff check --statistics .Each line shows the number of violations, the rule code, and the rule name, such as unused-import. Sort by the highest count to see which rules produce the most noise. This is a good first step when you adopt Ruff on an existing codebase, because you can decide rule by rule what to fix and what to ignore.
To understand a rule you do not recognize, ask Ruff for its documentation:
ruff rule F401The output explains what the rule checks, why it matters, and shows an example. It also states whether a fix is available.
Seeing what Ruff would change
Two options show fixes without applying them:
ruff check --diff .
ruff check --show-fixes --fix .--diff prints the changes as a unified diff and leaves files untouched. --show-fixes goes with --fix and lists which fixes were applied. A later chapter explains the difference between safe and unsafe fixes.
Caching and speed
Ruff keeps a cache in a .ruff_cache directory. It skips files that have not changed since the last successful run with the same settings. This is why the second run on a large project is much faster than the first.
If you suspect stale results, bypass the cache for one run with --no-cache, or delete the directory. Add .ruff_cache to your .gitignore. Ruff writes a .gitignore inside the cache directory itself, so it normally ignores itself, but an explicit entry does no harm.
For a fast edit-and-check loop while you work, you can also run Ruff in watch mode:
ruff check --watch .It reruns whenever a file changes.
Common problems
- Different results on different machines. The cause is usually different Ruff versions. Rules change between releases, and new rules can appear. Pin the version in your dependencies, for example
ruff==0.x.y, and upgrade deliberately. - A rule you expect does not run. Check which configuration Ruff found.
ruff check --show-settings example.pyprints the effective settings for a file, including the selected rules. Ruff uses the closestpyproject.toml,ruff.tomlor.ruff.tomlabove the file, so a nested config file can shadow the one at the project root. - Violations in files you meant to exclude. Pass
--force-excludewhen file names come from another tool, as the earlier warning described. - A failing job that only reports. If you want a report without failing the build, use
--exit-zero. Remember to remove it once the codebase is clean, or violations will never block a merge. - Confusing the two commands.
ruff checkfinds problems and does not change layout. Whitespace, quotes and line wrapping are the job ofruff format. A file can passruff checkand still failruff format --check.
Check your understanding
Exercise
You run ruff check . in CI and the job passes with exit code 0. A teammate then runs ruff check --diff . locally and sees nothing, but ruff format --check . fails. Why can both facts be true, and which exit code would ruff check return if a single unused import remained?
Show answer
ruff check and ruff format are separate tools. The linter reports rule violations such as unused imports, while formatting issues like quote style and spacing are only detected by ruff format --check. So a file can be lint-clean and still be unformatted. If one unused import (F401) remained, ruff check would return exit code 1. Exit code 2 is reserved for Ruff failing to run, for example with an invalid configuration.
Exercise
You want to see which rules produce the most violations in a project, and then run only the F rules plus import sorting without editing pyproject.toml. Which two commands do that?
Show answer
Run ruff check --statistics . to print a count of violations per rule. Then run ruff check --select F,I . to check only the pyflakes and import sorting rules. --select replaces the default set, so only F and I run. --extend-select I would instead add import sorting to the defaults.