Sub topic 10
Ignoring rules for specific files
This chapter assumes you can run ruff check, read rule codes such as F401, and edit the [tool.ruff.lint] table in pyproject.toml. If any of those are unfamiliar, review the chapters on rule codes and rule selection first.
Why per-file ignores exist
A single rule set rarely fits every file in a project. Some examples:
__init__.pyfiles often import names only to re-export them, soF401(unused import) is noise there.- Test files use
assertstatements, magic numbers and long fixture names that stricter rules flag. - Scripts and notebooks may legitimately call
print(). - Migration files and generated code follow conventions you do not control.
You have three tools for this, and each fits a different scope.
| Scope | Tool | Typical use |
|---|---|---|
| One line | `# noqa: CODE` comment | A single deliberate exception |
| Whole file or directory | `per-file-ignores` | A category of files that always breaks a rule |
| Files that should not be checked at all | `exclude` or `extend-exclude` | Vendored or generated code |
Per-file ignores sit between the other two. They keep the rule active everywhere else and need no comments in the source.
The per-file-ignores table
Put the table under [tool.ruff.lint]. Each key is a glob pattern. Each value is a list of rule codes or prefixes to ignore for files that match.
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B", "UP"]
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"tests/**/*.py" = ["B011", "E501"]
"scripts/*.py" = ["E402"]
"migrations/**" = ["E501", "UP"]Notes on this configuration:
"__init__.py"has no directory part, so it matches an__init__.pyfile at any depth."tests/**/*.py"matches Python files undertests/at any depth."migrations/**"matches everything undermigrations/."UP"is a prefix, so it ignores every pyupgrade rule in those files.
The older location, [tool.ruff.per-file-ignores] without lint, still works in some versions but is deprecated. Use the lint table.
How patterns are matched
Ruff matches each pattern against both the file name and the path, and the path is checked relative to the project root. The project root is the directory that holds the configuration file. This has two consequences.
- A pattern with no slash, such as
"conftest.py", matches by file name anywhere. - A pattern with a slash, such as
"tests/*.py", is anchored to the project root. Without**, it matches only files directly insidetests/, not intests/unit/.
The second point causes many silent failures. If your ignores seem to have no effect, check the depth of the pattern first.
ruff check tests/unit/test_api.py --show-settings | grep -A 8 "per_file_ignores"--show-settings prints the resolved configuration for the given file, so you can confirm which ignores Ruff applies.
Ignores add to the selected rules
Per-file ignores only remove rules from the set that select has already chosen. They never enable anything. If a rule is not selected, listing it in per-file-ignores has no effect.
[tool.ruff.lint]
select = ["F", "B"]
[tool.ruff.lint.per-file-ignores]
"tests/**" = ["S101"]Here S101 (use of assert) is not selected, so the entry is redundant. It does not cause an error, which means stale entries can accumulate. Review the table occasionally and remove entries for rules you no longer select.
The reverse is also useful. You can select a strict family globally and relax it only where needed:
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B", "S"]
[tool.ruff.lint.per-file-ignores]
"tests/**" = ["S101", "S105", "S106"]The S family comes from flake8-bandit. In tests, asserts and hard-coded passwords are normal, so the table removes those rules there only.
A worked example
Consider a package with this layout:
project/
pyproject.toml
src/
shop/
__init__.py
cart.py
tests/
test_cart.pyThe file src/shop/__init__.py re-exports a name:
from shop.cart import CartRun Ruff with only the default rules plus F:
ruff check srcWithout a per-file ignore, Ruff reports F401 for Cart: it is imported but unused. The fix is the table entry shown earlier, which ignores F401 in __init__.py.
Even so, the more precise fix for a re-export is to declare it in __all__, because that states intent in the code:
from shop.cart import Cart
__all__ = ["Cart"]Ruff treats names listed in __all__ as used, so F401 is not raised. Choose the per-file ignore when the package has many re-exports and you do not want to maintain __all__. Choose __all__ when you want the public interface to be explicit.
Ruff actually skips excluded files before it parses them, but the diagram shows the decision order from the reader's point of view.
Per-file ignores versus exclude
exclude and extend-exclude remove files from checking entirely, for both the linter and the formatter. A per-file ignore only affects the linter and only for the listed rules.
[tool.ruff]
extend-exclude = ["vendor", "src/shop/_generated"]
[tool.ruff.lint.per-file-ignores]
"tests/**" = ["E501"]Use extend-exclude rather than exclude when you want to keep Ruff's default exclusions, such as .git, .venv and __pycache__. Setting exclude replaces that list.
Be careful when you pass a path on the command line. Ruff does not check an excluded file when it finds the file through directory traversal, but it does check a file you name directly. To make Ruff apply exclusions to explicit paths too, which matters for pre-commit hooks that pass file names, use --force-exclude:
ruff check --force-exclude src/shop/_generated/models.pyWithout this flag, a hook passing that path would lint a file you meant to skip.
Combining with extend-per-file-ignores
When one configuration file extends another, the child's per-file-ignores replaces the parent's table. To add entries instead, use extend-per-file-ignores:
# pyproject.toml in a subproject
[tool.ruff]
extend = "../pyproject.toml"
[tool.ruff.lint.extend-per-file-ignores]
"tests/**" = ["D"]This keeps the parent's ignores and adds the docstring rules D for tests. In a monorepo, this keeps shared policy in one place and lets each project add its own exceptions.
Common mistakes
- Pattern depth.
"tests/*.py"does not reachtests/unit/test_x.py. Use"tests/**/*.py". - Ignoring too broadly.
"tests/**" = ["F"]hides real bugs such as undefined names. List the specific codes you need. - Wrong table. Putting
per-file-ignoresunder[tool.ruff]instead of[tool.ruff.lint]may be ignored or warned about, depending on the version. - Relative paths from another directory. Patterns resolve against the configuration file's directory, not the directory you run the command from.
- Hiding a problem. If a rule fires in most files of a directory, the rule may not suit the project. Consider removing it from
selectinstead.
Checking your result
After editing the table, run Ruff on a path from each pattern and confirm the output:
ruff check tests/unit/test_cart.py
ruff check src/shop/__init__.pyIf a rule you expected to suppress still appears, then check these in order: the table location, the pattern depth, whether the code is spelled correctly, and whether the rule comes from a different family than you assumed (ruff rule CODE prints the details).
Exercise
Your project has tests/test_a.py and tests/integration/test_b.py. You write "tests/*.py" = ["E501"] under [tool.ruff.lint.per-file-ignores], yet Ruff still reports E501 in test_b.py. Why does this happen, and what change fixes it?
Show answer
A pattern that contains a slash is anchored to the project root, and a single * does not cross directory boundaries. So tests/*.py matches only files directly inside tests/, which includes test_a.py but not tests/integration/test_b.py. Change the pattern to "tests/**/*.py" (or "tests/**") so it matches files at any depth.
Exercise
A package's __init__.py re-exports three classes and Ruff reports F401 for each. Name two ways to stop the reports and state when you would prefer each.
Show answer
One way is to add "__init__.py" = ["F401"] to per-file-ignores, which suits a package with many re-exports where you do not want to maintain a list. The other way is to list the names in __all__, which suits a package where you want the public interface to be explicit in the code. Ruff treats names in __all__ as used.
Exercise
You set select = ["F", "B"] and add "tests/**" = ["S101"] to per-file-ignores. What effect does the S101 entry have?
Show answer
None. Per-file ignores only remove rules that are already selected, and S101 is not selected because the S family is not in select. The entry is redundant and should be removed, or S should be added to select if you want those checks elsewhere.