Search code examples
pythonruff

Enable ruff rules only on specific files


I work on a large project and I'd slowly like to enfore pydocstyle using ruff. However, many files will fail on e.g. D103 "undocumented public function". I'd like to start with enforcing it on a few specific files, so I'd like to write something like

select = ["D"]

[tool.ruff.ignore-except-per-file] # this config does not exist
# ignore D103 but on all files, except the ones that pass
"properly_formatted_module1.py" = ["D103"]
"properly_formatted_module2.py" = ["D103"]

I don't think this is possible; the only way I see is to explicitly write down ALL of the file names in a [tool.ruff.extend-per-file-ignores]. There are a few hunderd of them so that's not really nice to do.


Solution

  • You can invert the file selection in the (extend-)per-file-ignores section to ignore for example D103 on all files except the ones that do match the pattern.

    from the Ruff documentation for the per-file-ignores:

    A list of mappings from file pattern to rule codes or prefixes to exclude, when considering any matching files. An initial '!' negates the file pattern.

    And the corresponding example in a pyproject.toml:

    # Ignore `D` rules everywhere except for the `src/` directory.
    "!src/**.py" = ["D"]