Ignore specific rules in specific directory with Ruff
Asked Answered
H

1

14

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.

Houselights answered 18/12, 2023 at 15:25 Comment(0)
L
19

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.

Lister answered 18/12, 2023 at 16:15 Comment(1)
If anyone else has the issue where Ruff complains lint wasn't an expected property for tool.ruff, ensure Ruff is up-to-date.Dorri

© 2022 - 2024 — McMap. All rights reserved.