Sub topic 12
Avoiding linter and formatter conflicts
This chapter assumes you can run ruff check and ruff format, and that you can edit the [tool.ruff] tables in pyproject.toml. It also assumes you know how rule codes and prefixes select rules.
Why conflicts happen
ruff check reports rule violations. ruff format rewrites layout. Some lint rules check layout too, such as quote style, trailing commas and indentation. If a rule demands one layout and the formatter produces another, you get a loop. You run the formatter, the linter complains, you fix by hand, and the formatter undoes your fix.
The default rule set (E4, E7, E9, F) avoids this. Conflicts usually appear after you widen select, for example with select = ["ALL"].
Rules that overlap with the formatter
| Rules | What they check | Problem |
|---|---|---|
| `COM812` | Missing trailing comma | The formatter decides trailing commas itself, and the two can disagree. |
| `ISC001` | Implicit string concatenation on one line | The formatter may join or split strings in ways that change the result. |
| `Q000`–`Q003` | Quote style | The formatter already enforces `quote-style`. |
| `W191`, `E111`, `E114`, `E117`, `D206`, `D300` | Indentation and docstring quotes | The formatter owns indentation. |
Ruff warns when you enable some of these together with the formatter. The warning names the rules and recommends removing them from select or adding them to ignore.
Ignore the conflicting rules
Keep the broad selection and ignore the overlapping rules explicitly.
[tool.ruff]
line-length = 88
[tool.ruff.lint]
select = ["E", "F", "I", "B", "UP", "COM", "ISC", "Q"]
ignore = ["COM812", "ISC001", "Q000", "Q001", "Q002", "Q003"]
[tool.ruff.format]
quote-style = "double"The formatter still produces double quotes. The Q rules would only report violations the formatter has already fixed or will fix.
Line length is a special case
E501 (line too long) and the formatter both read line-length, so keep them in sync by setting it once under [tool.ruff]. The formatter cannot always shorten a line. It will not split a long string literal or a long comment, so E501 can still fire after formatting. Treat that as a real finding, not a conflict. Many projects leave E501 off and let the formatter do the work.
Import sorting needs an order
The formatter does not sort imports. The I rules do, through I001. If you run only ruff format, imports stay unsorted. If you run the fixer after the formatter, the sort can leave blank lines the formatter would have changed.
Run the lint fix first and the formatter last:
ruff check --fix .
ruff format .The last two steps are what CI should run. They change nothing and fail if any file is not in its final state.
Trailing commas and imports
The formatter honors the "magic trailing comma". If a collection already ends in a comma, it stays one item per line. The isort rules have a matching setting, split-on-trailing-comma, which defaults to true. Leave it at the default so the import sorter and the formatter agree about multi-line imports.
[tool.ruff.lint.isort]
split-on-trailing-comma = trueExercise
You enable select = ["ALL"] and Ruff warns that COM812 and ISC001 may conflict with the formatter. What are two ways to fix this, and in what order should you run the fixer and the formatter afterwards?
Show answer
Remove COM and ISC from select, or keep the selection and add COM812 and ISC001 to ignore under [tool.ruff.lint]. Then run ruff check --fix . first and ruff format . last, so the formatter's output is final.