-1

I have my python project. I'm trying to setup pre-commit checks using pre-commit. I want to exclude some folders and some files (by pattern) from the analysis. The `exclude tag in config file only supports string not an array.

E.g. in following project structure, I want to exclude poc and tests folders and conftest.py file.

root
├── poetry.lock
├── pyproject.toml
├── resources
├── src
│   ├── mylib
│   │   └── functions.py
│   └── poc
│       └── some_junk.py
├── conftest.py
└── tests
    ├── entry_point.py
    └── test_functions.py

I can exclude a folder or file using exclude tag, e.g. to exclude poc I do this:

exclude: poc

repos:
  - repo: https://github.com/pycqa/isort
    rev: 5.11.4
    hooks:
      - id: isort
      - id: ...

... but how do I exclude multiple files and folders?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Kashyap
  • 15,354
  • 13
  • 64
  • 103

1 Answers1

2

as documented exclude is a regular expression as such you can utilize | to exclude multiple things

the example given from the docs:

    -   id: my-hook
        exclude: |
            (?x)^(
                path/to/file1.py|
                path/to/file2.py|
                path/to/file3.py
            )$

disclaimer: I wrote pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Thank you! As you're the author: 1. A little odd choice to not support a list of exclude patterns and instead just one string. 2. Not sure if the error message produced could be customized or not, but what threw me off was `"At key: exclude =====> Expected string got list"` when I put in a list of patterns/names. Would've been really user-friendly if it said use `^(xx|yy)$`. 3. Docs: Throwing in the word "regex", in description for `exclude` would help, and there was no example that I could find in official documentation that showed how to satisfy my (very common?) need. – Kashyap Jan 20 '23 at 15:33
  • (1) you don't need a list, you have a regex, (2) if you can't recognize `^$` as a regex you probably have bigger problems (3) I literally linked you to the example **that's in the docs** – anthony sottile Jan 20 '23 at 22:14