Sub topic 7
Fixing violations automatically
This chapter assumes you can run ruff check, read rule codes such as F401, and edit the [tool.ruff.lint] section of pyproject.toml. Many violations come with a fix that Ruff can apply for you. You will learn how to apply fixes, what separates a safe fix from an unsafe one, and how to keep control over what changes.
Applying fixes with --fix
Pass --fix to ruff check and Ruff rewrites the files in place. It then reports whatever it could not fix.
ruff check --fix .Take this file, which triggers F401 (unused import) and F841 (unused local variable):
import os
import sys
def total(items):
unused = 1
result = 0
for i in items:
result += i
return result
print(sys.argv)After ruff check --fix, the import os line is gone. Ruff fixes F401 by deleting the import. The F841 violation is different. Removing unused = 1 is not always safe, so Ruff treats that fix as unsafe and leaves the line alone by default.
Not every rule has a fix. In the output, a violation with an automatic fix is marked with [*]. Run ruff rule F401 to see whether a rule is fixable and how.
Safe and unsafe fixes
Ruff splits fixes into two groups.
- A safe fix preserves the meaning of your code. Removing an unused import is the usual example.
- An unsafe fix may change runtime behavior or delete comments. Ruff does not apply it unless you ask.
Unsafe fixes exist because the change is usually right but cannot be proven right. For example, the F841 fix removes an assignment, and the right-hand side might have had a side effect.
| Option | Effect |
|---|---|
| `--fix` | Applies safe fixes only |
| `--fix --unsafe-fixes` | Applies safe and unsafe fixes |
| `--unsafe-fixes` (without `--fix`) | Shows unsafe fixes as available, without applying anything |
ruff check --fix --unsafe-fixes .Review the result with git diff before you commit.
Previewing changes
Use --diff to see what a fix would do without writing any file.
ruff check --fix --diff .The output is a unified diff. This is useful in review scripts and when you are trying a new rule for the first time.
Controlling which rules may fix
Fixing is set per rule. You can allow or forbid it in pyproject.toml:
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B", "UP"]
fixable = ["ALL"]
unfixable = ["F841"]fixablelists the rules Ruff may fix. The default is all of them.unfixablelists rules Ruff must never fix, even with--fix.
You can also change how a single fix is classified. The extend-safe-fixes and extend-unsafe-fixes options promote or demote the fixes for given rules. A team that trusts the fix for UP rules could add them to extend-safe-fixes, so plain --fix applies them.
Typical workflow
- Commit your work so the tree is clean.
- Run
ruff check --fix .. - Read the remaining violations. Fix them by hand, or preview unsafe fixes.
- Run the tests, then commit.
Common errors
- Expecting
--fixto fix everything. Many rules have no fix. Others are unsafe and need--unsafe-fixes. - Applying unsafe fixes blindly. They can remove code or comments. Always read the diff.
- Fixing in CI. A CI job should only check, not modify files. Use
ruff check .there, without--fix. - Forgetting the formatter.
--fixdoes not format code. Runruff format .afterwards.
Exercise
You run ruff check --fix . and a file still reports an F841 violation marked as having an unsafe fix. What command applies that fix, and what should you do before and after running it?
Show answer
Run ruff check --fix --unsafe-fixes .. Before, make sure the working tree is committed or clean, and optionally preview with --diff. After, read git diff and run the tests, since an unsafe fix can change behavior.
Exercise
How do you stop Ruff from ever auto-fixing F841, even when --fix is used?
Show answer
Add "F841" to the unfixable list under [tool.ruff.lint] in pyproject.toml.