Ignore specific rules in specific directory with Ruff

2.5k Views Asked by At

I am using Ruff, a formatter or linter tool for Python code.

I want to ignore some specific rules, and to write that config in pyproject.toml.

My package structure is as follows.

.
├── LICENSE
├── pyproject.toml
├── README.md
├── src
│   └── mypackage/mymodule.py
└── tests
    ├── doc.md
    └── test_mymodule.py

And, I want to ignore rules pydocstyle (D) in the tests/ directory.

1

There are 1 best solutions below

1
On BEST ANSWER

You can use Ruff's per-file-ignores and specify individual files, or select a directory tree with a wildcard (star). You can ignore a "letter class" by specifying only the letter.

Example: Ignore specific rule in a specific file

To ignore line-length violations in your tests, add this to pyproject.toml:

[tool.ruff.lint.per-file-ignores]
"foofile.py" = ["E501"]

Example: Ignore a class of rules in a whole directory tree

To ignore all pydocstyle errors (starting with "D") in your tests, add this to pyproject.toml:

[tool.ruff.lint.per-file-ignores]
"tests/*" = ["D"]

The related Ruff docs are here.