Sub topic 3
Finding problems with ruff check
This chapter assumes you can run Python scripts, use a terminal inside a virtual environment, and have installed Ruff with pip install ruff. It covers ruff check, the linting command. Rule codes, configuration, and fixes get their own chapters.
What the linter does
A linter reads your source code without running it. It reports likely bugs, unused names, and style problems. ruff check is Ruff's linter. It does not change files unless you ask it to.
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)Run Ruff on the current directory:
ruff check .Ruff finds three problems:
F401:osis imported but never used.F841: the local variableunusedis assigned but never used.E711: the code compares toNonewith==instead ofis.
Each report shows the file, line and column, the rule code, a short message, and a snippet of the offending code. A summary line at the end gives the total number of errors.
Reading the output
Each violation has the same shape:
example.py:1:8: F401 [*] `os` imported but unusedThe parts are the path, the line, the column, the rule code and the message. The [*] marker means Ruff can fix this violation automatically. You will use that in the autofix chapter.
Exit codes
The exit code makes ruff check usable in scripts and CI.
| Exit code | Meaning |
|---|---|
| 0 | No violations found, or all violations were fixed |
| 1 | Violations were found |
| 2 | Ruff failed, for example because of an invalid configuration |
A CI job fails when the exit code is not 0. That is how a lint check blocks a merge.
Choosing what to check
You can pass files, directories, or both:
ruff check example.py
ruff check src tests
ruff check .Ruff skips directories such as .git, .venv, and __pycache__ by default. It also respects .gitignore, so ignored files are not checked.
Default rules
With no configuration, Ruff runs a small set of rules. These are a subset of pycodestyle errors (E4, E7, E9) and all of the Pyflakes rules (F). They catch real mistakes, such as syntax errors, undefined names, and unused imports, and they leave out opinionated style rules. You widen the set in pyproject.toml, which a later chapter covers.
Getting more detail
Use ruff rule to read the documentation for any code:
ruff rule F401The output explains what the rule checks, why it matters, and shows an example. Use it whenever a code in the output is unfamiliar.
You can change the output format with --output-format. The concise format prints one line per violation, and json suits tools that parse the result:
ruff check --output-format concise .
ruff check --output-format json .Common mistakes
- Expecting a clean exit after running the formatter.
ruff formatandruff checkare separate commands. Formatting does not fix lint violations, and linting does not reformat code. - Ignoring exit code 2. It signals a broken configuration or an unreadable file. It does not mean your code is clean.
- Checking the wrong directory. Run
ruff check .from the project root so Ruff finds yourpyproject.toml.
How the pieces fit
Exercise
You run ruff check . in CI and it prints three violations. What exit code does it return, and what happens to the CI job?
Show answer
It returns exit code 1 because violations were found. The CI job fails, since any non-zero exit code counts as a failure. Exit code 2 would mean Ruff itself failed, for example because of a bad configuration.
Exercise
Which command shows the documentation for rule E711?
Show answer
ruff rule E711