Sub topic 12
Avoiding linter and formatter conflicts
This chapter assumes you can run ruff check and ruff format, read a pyproject.toml, and select rules by code or prefix. It covers what happens when the two tools disagree, which rules cause that, and how to configure and order the tools so they don't fight.
Why conflicts happen
ruff check and ruff format are separate programs inside one binary. The linter reports and fixes rule violations. The formatter rewrites layout: quotes, spacing, line breaks, trailing commas, blank lines. Some lint rules police the same layout decisions the formatter makes. If a rule wants one layout and the formatter produces another, you get one of two failures:
- Fix loop.
ruff check --fixrewrites a line,ruff formatrewrites it back, and the nextruff checkflags it again. - Permanent warning. A rule fires on code the formatter just produced, so the file is never clean.
Neither tool is wrong. They are configured with overlapping responsibilities, and the fix is to give each concern to exactly one tool.
The warning Ruff gives you
Ruff detects the most common conflicts itself. Suppose you select rules from the flake8-commas and flake8-implicit-str-concat families:
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "COM", "ISC"]When you run the formatter, Ruff prints a warning similar to this:
warning: The following rules may cause conflicts when used with the formatter: `COM812`, `ISC001`. To avoid unexpected behavior, we recommend disabling these rules, either by removing them from the `select` or `extend-select` configuration, or adding them to the `ignore` configuration.Read this warning as a configuration bug, not as noise. The fix is to ignore the named rules:
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "COM", "ISC"]
ignore = ["COM812", "ISC001"]The default selection (E4, E7, E9, F) was chosen to avoid these problems. The risk appears when you widen select, especially with whole families such as ALL, E, W, Q, COM or ISC.
Rules that overlap with the formatter
The table lists the rules that most often collide with formatter output. Use ruff rule CODE to read the current description of any of them.
| Rule | What it checks | Why it conflicts |
|---|---|---|
| `COM812` | Missing trailing comma | The formatter decides trailing commas itself, based on whether a collection is split across lines. |
| `COM819` | Prohibited trailing comma | Same decision, opposite direction. |
| `ISC001` | Implicit string concatenation on one line | The formatter can join or reflow implicitly concatenated strings. |
| `Q000`, `Q001`, `Q002`, `Q003` | Quote style | The formatter normalizes quotes according to `quote-style`. Q-rules can disagree if their settings differ from it. |
| `W191` | Tab indentation | Conflicts when `indent-style = "space"` is used. |
| `E111`, `E114`, `E117` | Indentation width and over-indentation | These are preview rules, and the formatter owns indentation. |
| `D206`, `D300` | Docstring indentation and triple-double-quote style | The formatter rewrites docstring indentation and quotes. |
| `E501` | Line too long | Related but not a hard conflict. See the next section. |
The COM, ISC and Q families are the usual culprits when someone enables everything and then adds the formatter later.
Line length is shared, not duplicated
line-length is a top-level setting, so both tools read the same value:
[tool.ruff]
line-length = 100The formatter wraps code to fit this width where it can. It cannot always do so. It does not split long string literals, long URLs in comments, or a long identifier with nothing to break on. E501 will still report those lines, and that is a real signal, not a conflict.
You have three reasonable options when you select E501:
- Fix the offending lines by hand, for example by splitting a string into several literals.
- Add
# noqa: E501to the few lines that must stay long, such as a URL. - Drop
E501fromselectand let the formatter's best effort stand. Many projects do this.
Because E501 is not in the default selection, a project that only uses the defaults never has to make this choice.
Quote style: one source of truth
If you select the Q rules and also set a quote style for the formatter, make sure both say the same thing. This configuration is consistent:
[tool.ruff.format]
quote-style = "single"
[tool.ruff.lint.flake8-quotes]
inline-quotes = "single"
docstring-quotes = "double"
multiline-quotes = "double"If the two disagree, the linter flags every line the formatter produces. The simplest approach is to leave the Q rules out and let quote-style decide. Q-rules are only useful if you want an error for code the formatter has not yet touched.
The formatter also avoids changing quotes when doing so would add escapes. A string such as 'He said "hi"' stays single-quoted under the default quote-style = "double", so Q000 is not a good check for it.
Magic trailing commas and isort
The formatter honors a "magic trailing comma." If a collection already ends in a comma, it stays one item per line:
colors = [
"red",
"green",
]Without the final comma, the formatter would collapse this list onto one line if it fits. Setting skip-magic-trailing-comma = true in [tool.ruff.format] turns this behavior off.
The import sorter has a matching option, split-on-trailing-comma, under [tool.ruff.lint.isort]. If you disable the magic comma in the formatter but leave the isort option at its default of true, the two tools can disagree about how a multi-line import should look. Keep the two settings aligned:
[tool.ruff.format]
skip-magic-trailing-comma = true
[tool.ruff.lint.isort]
split-on-trailing-comma = falseOrder of operations
The formatter does not sort imports, and fixes from the linter can leave code in a shape the formatter would change. For that reason the order matters. Run the linter fixes first and the formatter last:
ruff check --fix .
ruff format .To limit the first step to import sorting only, which is useful if you want formatting-related fixes handled in one pass:
ruff check --select I --fix .
ruff format .Running the formatter last guarantees that the files you commit are in formatter-canonical form. If you reversed the order, ruff format --check in CI could fail on code that ruff check --fix rewrote after formatting.
A conflict-free configuration
This configuration widens the rule set beyond the defaults and removes the known overlaps:
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B", "UP", "COM", "ISC"]
ignore = ["COM812", "ISC001"]
[tool.ruff.format]
quote-style = "double"
indent-style = "space"Running both tools on a small file shows how the pieces fit together:
import sys, os
def build(items):
parts = ["a" "b", 'c']
return { 'count':len(items), 'parts':parts }
print(build(sys.argv), os.name)ruff check --select I,E401 --fix .
ruff format .
ruff check .
ruff format --check .The first command splits import sys, os into two sorted import lines (E401 flags multiple imports on one line, and I001 sorts them). The formatter then fixes the dictionary spacing, normalizes the quotes, and adds the two blank lines before the function. The last two commands should report no problems. Note that "a" "b" is an implicit concatenation on one line. With ISC001 ignored, Ruff does not report it, so check that the code does what you intended.
Checking for conflicts in CI
A cheap guard is to run both checks and fail on any difference:
ruff check .
ruff format --check .ruff format --check exits with a non-zero status if any file would change. ruff format --diff prints what would change without writing anything, which helps when you need to see why the check fails. If ruff check fails only on rules from the table above, treat it as a configuration problem and ignore those rules, instead of adding noqa comments throughout the code.
Summary of decisions
- Prefer the default rule selection plus the specific families you want. Avoid
ALLunless you are ready to maintain a longignorelist. - Ignore
COM812andISC001whenever you use the formatter. - Leave the
Q,W191and indentation rules out, or keep their settings identical to the formatter's. - Run
ruff check --fixfirst andruff formatlast. - Treat the formatter conflict warning as a failing check.
Exercise
You enable select = ["E", "W", "F", "COM"] and run ruff format .. Ruff prints a warning naming COM812. What should you change, and why not just add # noqa: COM812 to the lines it flags?
Show answer
Add COM812 to ignore (for example ignore = ["COM812"]), or remove COM from select. The formatter already decides trailing commas, so the two tools will disagree. A noqa comment would hide each report but not the underlying disagreement, and ruff check --fix and ruff format could still undo each other's changes.
Exercise
A CI job runs ruff format . and then ruff check --fix ., and later ruff format --check . fails on code that was just formatted. What is wrong with the order, and what is the correct order?
Show answer
The linter's fixes ran after the formatter, so they left code that the formatter would still change. Run ruff check --fix . first and ruff format . last. In CI, use ruff check . and ruff format --check . so nothing is rewritten.
Exercise
You set skip-magic-trailing-comma = true under [tool.ruff.format]. Which isort setting should you change to match, and to what value?
Show answer
Set split-on-trailing-comma = false under [tool.ruff.lint.isort]. Both settings then ignore a trailing comma as a hint to keep one item per line, so the import sorter and the formatter agree about multi-line imports.