Running pre commits, excluding a directory for multiples hooks

449 views Asked by At

I'm trying to configure pre-commits to avoid the directory app/test/

I can exclude it directly on the hook, for exemple avec mypy :

In cmd line :mypy . --exclude ^app/test/$

In the pre-commit.yaml file

-   id: mypy
    name: mypy
    entry: .
    args: ["--exclude", "^app/test/$"]
    language: python
    types: [python]

Which is working perfectly fine.

I want to exclude it directly in the pre-commit config (https://pre-commit.com/#top_level-exclude) because I'm using mutiple hooks

But I cannot get it working. So far I have tried combinaison of things like this in my pre-commit file :

exclude : ^app/test/$
repos :
[...]

Do you have any ideas on how should I do this ?


Edit 1

Thanks for your answer. I did made a mistake, my entry is entry: mypy ..

I also forget pass_filenames: false that is on my pre-commit file. I'm not so sure what's it's doing but without it, the exclusion, even as an argument is not working.

So my pre-commit file is looking like this :

-   id: mypy
    name: mypy
    entry: mypy .
    args: ["--exclude", "^app/test/$", "--ignore-missing-imports"]
    language: python
    types: [python]
    pass_filenames: false

Therefore, it might change some of the results you excpected.

To follow your advices, I have changed my hook to : (removing entry)

-   id: mypy
    name: mypy
    args: ["--exclude", "^app/test/$", "--ignore-missing-imports"]
    language: python
    types: [python]

It's not working, the error is : Missing required key : entry.

For this configuration :

-   id: mypy
    name: mypy
    args: ["--exclude", "^app/test/$", "--ignore-missing-imports"]
    language: python
    types: [python]
    pass_filenames: false

It's not working either, the error is the same : Missing required key : entry.

Therefore I added the entry key without . :

-   id: mypy
    name: mypy
    entry: mypy
    args: ["--exclude", "^app/test/$", "--ignore-missing-imports"]
    language: python
    types: [python]

It's not working but not excluding the directory

And for this last configuration :

-   id: mypy
    name: mypy
    entry: mypy
    args: ["--exclude", "^app/test/$", "--ignore-missing-imports"]
    language: python
    types: [python]
    pass_filenames: false

It's not working, I have the following error : Missing target module

Note : I'm not using it yet, but I also want to use the strict flag for mypy.

I'm not using configuration for file mypy, and I don't want to use one.

What can I try to avoid overriding entry or args for mypy but still excluding directory, iedally as a global parameter ?

Thanks


Edit 2

Reading the comments, I have forget a part; I want to run my pre-commit in a CI to avoid the famous --no-verify

1

There are 1 answers

9
anthony sottile On

you've broken the pre-commit <=> mypy integration by setting entry: . (which wouldn't even work? I suspect this is not your actual code and you've instead written args: [.] which also breaks the integration but in a way that double-lints all of your code)

mypy is a tricky one as well in that it will follow your imports by default -- so you'll need to exclude it from files passed and in the mypy configuration

my recommendation:

  • don't override entry or args for mypy -- and if you do don't add . to it as it'll double-lint everything and break file filtering
  • add your particular module to mypy's configuration file to ignore it

disclaimer: I wrote pre-commit