Sub topic 5
Configuring Ruff in pyproject.toml
This chapter assumes you can run ruff check and ruff format, read a TOML file, and use a virtual environment. It covers where Ruff looks for settings, how the file is organized, which options matter first, and how to debug a configuration that doesn't behave as expected.
Why configure at all
Ruff runs without any configuration. Out of the box it uses a line length of 88 and a small default rule set (E4, E7, E9 and F). That is enough to catch syntax errors and unused names. Real projects usually need more: extra rule families, a declared Python version, excluded directories, and agreement on formatter style. Keeping these settings in the repository means every developer, editor and CI job applies the same rules.
Where Ruff finds its settings
Ruff reads three file names:
| File | Section prefix | Notes |
|---|---|---|
| `pyproject.toml` | `[tool.ruff]` | Ignored for discovery if it has no `[tool.ruff]` table |
| `ruff.toml` | none, such as `[lint]` | Takes precedence over `pyproject.toml` in the same directory |
| `.ruff.toml` | none, such as `[lint]` | Takes precedence over both |
The prefix is the main difference between the files. In pyproject.toml you write [tool.ruff.lint]. In ruff.toml the same table is [lint], and top-level options like line-length have no table at all.
Ruff uses hierarchical discovery. For each file it checks, it walks up from that file's directory and uses the closest configuration file it finds. It does not merge that file with configuration files further up the tree. A ruff.toml in a subdirectory replaces the root configuration for files below it, unless it opts in to inheritance with extend.
Two consequences follow. First, a pyproject.toml that only holds packaging metadata does not stop discovery, because Ruff skips it and keeps walking upward. Second, a monorepo package with its own config does not inherit the root rules by default. To inherit, use extend:
# packages/api/pyproject.toml
[tool.ruff]
extend = "../../pyproject.toml"
line-length = 100The local line-length overrides the base file. Other base settings still apply.
The three tables
Ruff splits its options into three places:
[tool.ruff]holds settings shared by the linter and formatter:line-length,target-version,exclude,src.[tool.ruff.lint]holds rule selection and rule-specific options.[tool.ruff.format]holds formatter style options.
Older tutorials put select and ignore directly under [tool.ruff]. Current Ruff versions still accept that form but warn that it is deprecated. Put linter options under [tool.ruff.lint].
Here is a complete configuration that you can paste into a project:
[project]
name = "demo"
version = "0.1.0"
requires-python = ">=3.11"
[tool.ruff]
line-length = 88
extend-exclude = ["migrations", "build"]
src = ["src", "tests"]
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B", "UP"]
ignore = ["B008"]
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"tests/**/*.py" = ["B011"]
[tool.ruff.lint.isort]
known-first-party = ["demo"]
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
docstring-code-format = trueShared settings in detail
line-length
line-length defaults to 88. It controls where the formatter wraps code. It also sets the limit for rule E501 (line too long), but only when E501 is selected, and it is not in the default set. The formatter wraps code but cannot split long strings or comments, so a line may still exceed the limit. If you select E501, those lines are reported. Many projects leave E501 off and let the formatter handle length.
target-version
target-version tells Ruff which Python version your code must run on. Rules and fixes depend on it. For example, the UP rules rewrite Optional[int] to int | None only when the target is 3.10 or newer. A wrong target gives wrong fixes.
If you do not set it, Ruff reads requires-python from the [project] table of the same pyproject.toml. If that is missing too, it falls back to a default (currently Python 3.9 in recent releases). An explicit setting looks like this:
[tool.ruff]
target-version = "py311"Prefer requires-python as the single source of truth when you have a [project] table. Set target-version only when you need it to differ.
exclude and extend-exclude
Ruff skips some directories by default, including .git, .venv, venv, node_modules, build, dist, and several cache folders. Setting exclude replaces that default list, so you would then have to repeat the entries you still want. Use extend-exclude to add paths while keeping the defaults. This is almost always the safer choice.
Excluded files are skipped when Ruff discovers them by walking a directory. If you pass a file path explicitly, for example from a pre-commit hook, Ruff still skips it unless you pass --force-exclude. Add that flag in hooks so your exclusions are respected.
src
src lists the directories where first-party code lives. Import sorting uses it to decide whether an import is first-party or third-party. The default is the project root and src. Set it when your layout differs, for example when code sits in a lib/ folder.
Lint settings
select, extend-select and ignore
select sets the full list of enabled rules and replaces the defaults. If you write select = ["I"], you lose F and the rest. extend-select adds to the defaults instead:
[tool.ruff.lint]
extend-select = ["I", "B", "UP"]This is equivalent to the long select in the full example above, and it keeps working if Ruff changes its defaults. The tradeoff is that your enabled set can shift when you upgrade Ruff. An explicit select is more predictable. Choose based on whether you prefer stability or tracking the defaults.
ignore removes rules after selection. If a code matches both lists, the more specific entry wins. For example, selecting E and ignoring E501 turns off only E501. Chapter-level detail on choosing rules is covered separately; here the point is where the options live.
Rule options
Some rules take their own settings in subtables named after the plugin, such as [tool.ruff.lint.isort], [tool.ruff.lint.pycodestyle] or [tool.ruff.lint.flake8-bugbear]. For instance, lint.pycodestyle.max-line-length lets E501 use a different limit from the formatter. Run ruff config to list options, or name one to read its documentation:
ruff config lint.isort.known-first-partyFormat settings
The [tool.ruff.format] table controls style choices that Black leaves fixed or optional:
quote-style:"double"(default),"single", or"preserve".indent-style:"space"(default) or"tab".line-ending:"auto"(default),"lf","crlf"or"native".docstring-code-format: whentrue, Ruff formats code examples inside docstrings. The default isfalse.skip-magic-trailing-comma: whentrue, ignore trailing commas that would otherwise force one item per line.
Checking what Ruff actually uses
When behavior surprises you, don't guess. Ask Ruff to print the resolved settings for the current directory:
ruff check --show-settings path/to/file.pyThe output names the configuration file in use and every resolved value, including defaults. Two flags help with isolation:
--config path/to/file.tomluses one file and ignores discovery.--isolatedignores all configuration files and uses defaults, which helps you tell whether a problem comes from your config.
Common mistakes
- Putting a rule family in
selectand losing the defaultFrules. - Using
[tool.ruff]in aruff.tomlfile. That file needs bare tables such as[lint]. - Expecting a nested config to inherit from the root without
extend. - Leaving
target-versionunset and then getting fixes that need a newer Python than you support. - Replacing
excludeinstead of extending it, which causes Ruff to lint.venv.
Exercise
Your repository has a root pyproject.toml with select = ["E4", "E7", "E9", "F", "B"] under [tool.ruff.lint]. A subdirectory scripts/ contains a ruff.toml with only line-length = 100. Which rules apply to files in scripts/, and how do you make them use the root rule selection too?
Show answer
Files in scripts/ use the closest config, which is scripts/ruff.toml. Ruff does not merge it with the root file, so the root select is not applied and the default rules (E4, E7, E9, F) run instead of the root list, with no B rules. To inherit the root settings, add extend = "../pyproject.toml" to scripts/ruff.toml. The local line-length = 100 then overrides the root value.
Exercise
A project declares requires-python = ">=3.9" and has no target-version. You add the UP rules. Will Ruff rewrite Optional[int] as int | None? Where do you check the version Ruff resolved?
Show answer
No. Ruff takes the target version from requires-python, which is 3.9, and int | None annotations need 3.10 or newer at runtime, so the rewrite is not applied there. Run ruff check --show-settings on a file and read the resolved target version in the output.