Sub topic 8
Formatting code with ruff format
This chapter assumes you have installed Ruff, run ruff check at least once, and can read a pyproject.toml file. Here you will learn what ruff format does, how to preview and verify its changes, and which options you can set.
What the formatter does
ruff format rewrites Python files into one consistent layout. It is deterministic: the same input always produces the same output. Its style follows Black, so most Black-formatted code stays unchanged.
The formatter only changes layout. It adjusts quotes, spacing, blank lines, trailing commas and line wrapping. It does not remove unused imports or fix == None. Those are lint violations, and ruff check handles them.
# Before ruff format
x = { 'a':1,
'b':2 }
def f(a,b):
return a+b
# After ruff format
x = {"a": 1, "b": 2}
def f(a, b):
return a + bThe formatter changed single quotes to double quotes, added spaces after colons and commas, collapsed the dictionary onto one line, and put two blank lines before the function.
Running the formatter
ruff format .
ruff format --check .ruff format . rewrites files in place. ruff format --check . changes nothing. It lists the files that would change and exits with a non-zero status if there are any. Use --check in CI so an unformatted commit fails the build. Running ruff format --diff . prints the changes as a diff without writing them, which is useful for a first look at a large codebase.
The command ruff format is separate from ruff check --fix. Neither one does the other's job, so a full cleanup runs both.
Configuring the formatter
Formatter options live in [tool.ruff.format]. Line length and target version are shared with the linter, so they go in the top-level [tool.ruff] table.
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
docstring-code-format = true| Setting | Effect |
|---|---|
| `line-length` | Maximum line width the formatter wraps to. The default is 88. |
| `quote-style` | `"double"` (default), `"single"`, or `"preserve"`. |
| `indent-style` | `"space"` (default) or `"tab"`. |
| `docstring-code-format` | Formats code examples inside docstrings when set to `true`. The default is `false`. |
How wrapping works
The formatter fits as much as it can on one line. If a construct exceeds line-length, it splits it at the brackets and puts one item per line. It then adds a trailing comma. This is the "magic trailing comma" behavior from Black. If you write a trailing comma after the last item, the formatter keeps the construct expanded, even when it would fit on one line.
values = [
1,
2,
3,
]Remove the trailing comma and run the formatter again, and it collapses to values = [1, 2, 3].
Turning formatting off locally
Use # fmt: skip at the end of a line, or wrap a block in # fmt: off and # fmt: on. Use these sparingly, for things like hand-aligned matrices.
matrix = [
1, 0, 0,
0, 1, 0,
0, 0, 1,
] # fmt: skipCommon errors
- Formatting only part of the project. Pass
.or the source directory, and check that no file is excluded by accident. - Expecting
ruff formatto sort imports. It does not. Import sorting is theIlint rules, applied withruff check --fix. - Setting
line-lengthin one tool and not the other. Keep one value inpyproject.toml.
Exercise
Your CI job must fail if any file is not formatted, but it must not change any files. Which command do you run, and what does it do on a failure?
Show answer
Run ruff format --check .. It rewrites nothing, lists the files that would be reformatted, and exits with a non-zero status so the CI job fails.