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] section of pyproject.toml. If any of these is new, review the chapters on rule codes and rule selection first.
Why ignore rules per file
A global ignore list turns a rule off everywhere. Some rules are only a problem in certain files:
__init__.pyfiles often import names only to re-export them, soF401(unused import) fires on code that is correct.- Test files use bare
assertstatements, which theS101rule from the security set flags. - Scripts and notebooks may need a
printcall, whichT201flags.
If you ignore these rules globally, you lose the checks in the rest of the project. per-file-ignores keeps the rule active everywhere except the files you name.
The per-file-ignores table
Add the table [tool.ruff.lint.per-file-ignores]. Each key is a file pattern. Each value is a list of rule codes or prefixes to skip for files that match.
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B", "UP", "S", "T20"]
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"tests/**/*.py" = ["S101"]
"scripts/*.py" = ["T201"]
"migrations/*" = ["E501", "F"]Patterns are glob patterns, matched against the file path. A pattern without a directory part, such as "__init__.py", matches files with that name in any directory. A pattern with a directory, such as "tests/**/*.py", is matched against the path relative to the project root. The ** part matches any number of nested directories.
Values can be full codes (F401) or prefixes (F, which covers every rule starting with F). Use the narrowest value that solves the problem.
How the decision is made
For each file, Ruff first builds the set of selected rules. It then looks at each pattern in per-file-ignores. For every pattern that matches the file, it removes the listed rules from the set.
Checking that a pattern works
Create a package with an unused import in __init__.py:
import osWith the configuration above, ruff check . reports nothing for this file. Remove the __init__.py entry and run it again. F401 appears. This quick test shows whether a pattern really matches the file you meant.
Common errors
- Wrong path style. Patterns are matched relative to the project root, not to the configuration file's folder if they differ. If a pattern does nothing, test a more general one such as
"**/tests/*.py". - Ignoring too much. Writing
"tests/*" = ["ALL"]hides every problem in tests, including real bugs. List specific codes. - Using it as a replacement for fixing code. A per-file ignore suits files with a different purpose. For one bad line, use a
# noqa: CODEcomment, as covered in the previous chapter. - Forgetting the rule must be selected. Ignoring
S101has no effect ifSwas never selected. It is harmless, but it can mislead readers.
Choosing between the options
| Situation | Best tool |
|---|---|
| One line breaks a rule on purpose | `# noqa: CODE` |
| A whole category of files follows different conventions | `per-file-ignores` |
| The rule is wrong for the whole project | `ignore` |
Exercise
Your project has a tests/ folder where assert is normal, but S101 should still run in src/. Which configuration achieves this, and why is it better than adding S101 to ignore?
Show answer
Add "tests/**/*.py" = ["S101"] under [tool.ruff.lint.per-file-ignores]. The rule stays active in src/ and is skipped only for matching test files. Adding S101 to ignore would disable it for the whole project.
Exercise
A teammate wrote "__init__.py" = ["F"] to silence unused imports. What is the problem, and what is the better value?
Show answer
The prefix F disables every Pyflakes rule in those files, including undefined names (F821), which are real bugs. The better value is ["F401"], which targets only the unused-import rule.