Sub topic 5
Configuring Ruff in pyproject.toml
This chapter assumes you can run ruff check and ruff format, you know basic TOML syntax, and you have a Python project with a pyproject.toml at its root. Rule selection and autofix get their own chapters, so here we cover where Ruff reads its settings and what the main sections do.
Where Ruff looks for configuration
Ruff searches for a config file starting in the directory of each file it checks and moving up through parent directories. It uses the first one it finds. It recognizes three file names:
pyproject.toml, where settings live under the[tool.ruff]table.ruff.toml, where the same settings are written without thetool.prefix..ruff.toml, a hidden variant ofruff.toml.
If a pyproject.toml has no [tool.ruff] table, Ruff does not treat it as a Ruff config and keeps searching upward. If no config is found, Ruff uses its defaults.
Three tables, three jobs
Ruff splits its settings into three tables.
| Table | Purpose | Example keys |
|---|---|---|
| `[tool.ruff]` | Settings shared by linter and formatter | `line-length`, `target-version`, `exclude` |
| `[tool.ruff.lint]` | Linter only | `select`, `ignore`, `per-file-ignores` |
| `[tool.ruff.format]` | Formatter only | `quote-style`, `indent-style`, `docstring-code-format` |
A common mistake is putting select or ignore directly under [tool.ruff]. Older Ruff versions accepted this, but current versions expect lint settings under [tool.ruff.lint], so put them there.
A working configuration
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B", "UP"]
ignore = []
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
docstring-code-format = trueEach setting does the following:
line-length = 88sets the limit used by the formatter and by the line-length lint rule. Because it sits in the shared table, both tools agree.target-version = "py311"tells Ruff which Python version your code must support. Rules such as theUP(pyupgrade) family use it to decide which syntax is safe to suggest.selectlists the rule families to enable. This list sets the rules explicitly, replacing Ruff's default selection.per-file-ignoresmaps file patterns to rule codes. Here, unused imports in__init__.pyfiles are allowed, because those files often re-export names.quote-style,indent-styleanddocstring-code-formatcontrol howruff formatwrites code. The last one also formats code examples inside docstrings.
How the settings flow
Checking what Ruff actually uses
Configuration can come from several places, so verify the result instead of guessing. This command prints the settings Ruff resolved for the project:
ruff check --show-settings path/to/file.pyThe output is long. Look for linter.rules.enabled to confirm which rules are active and for formatter.line_width to confirm the line length. If you want to test a different config without editing your project file, pass --config path/to/other.toml.
Common errors
- Unknown field errors. Ruff rejects keys it does not recognize. A typo such as
line_length(with an underscore) fails with a parse error, so use hyphens. - Settings in the wrong table. Putting
quote-styleunder[tool.ruff.lint]is an error, because it belongs to the formatter. - A nested config that hides the root one. A
pyproject.tomlwith a[tool.ruff]table in a subdirectory takes over for files in that directory. It does not merge with the parent config unless you useextend = "../pyproject.toml".
Exercise
Your project has select = ["F", "I"] under [tool.ruff] and Ruff reports an error about lint settings, or ignores them. Where should the setting go, and which command lets you confirm the rules that are enabled afterwards?
Show answer
Move select into the [tool.ruff.lint] table. Then run ruff check --show-settings on a file and look at the enabled rules to confirm that the F and I families are active.
Exercise
You add a [tool.ruff] table to src/legacy/pyproject.toml with line-length = 120. The root config uses 88. Which line length applies to files in src/legacy, and how can you keep the other root settings?
Show answer
Files in src/legacy use 120, because Ruff uses the closest config and does not merge it with the parent. To keep the root settings, add extend = "../../pyproject.toml" to the nested [tool.ruff] table, then override only line-length.