1

I have the following repo in pre-commit file .pre-commit-config.yaml

-   repo: local
    hooks:
    -   id: check_pip
        name: Check pip file
        description: This hook checks if requirements-dev.txt is up to date.
        language: system
        entry: python -m scripts.check_pip_requirements
        args: ["--compare"]

But it keeps giving me the error:

error: unrecognized arguments: .pre-commit-config.yaml

As it passes the filename as an argument to my python script. How can I prevent this?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Erfan
  • 40,971
  • 8
  • 66
  • 78

2 Answers2

3

to clean up your example a little bit -- and use files to only run when the necessary files change:

-   repo: local
    hooks:
    -   id: check_pip
        name: Check pip file
        description: This hook checks if requirements-dev.txt is up to date.
        language: system
        entry: python -m scripts.check_pip_requirements --compare
        files: ^requirements-dev.txt$
        pass_filenames: false

note that I did a couple things:

  • args doesn't really make sense for local hooks, you can just put that in entry
  • pass_filenames (as you did) -- pre-commit is a framework based on passing filenames to executables, but you can turn that off
  • files: this will make it so the hook only gets triggered if requirements-dev.txt changes

alternatively (if you expect changes outside requirements-dev.txt to need to run this hook) you can drop files and use always_run: true


disclaimer: I'm the author of pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • The original script creates `requirements-dev.txt` from a conda `environment.yml`. So would it make sense to put: `^environment.yml$` in `files`? Because we want to check if the users requirements is updated when `environment.yml` changes, which is leading. – Erfan Sep 24 '20 at 10:29
2

I spent quite some time figuring out what caused this and how to solve this. It's not documented well, eventually I fixed it by trial and error. We have to use pass_filenames: false in our hook:

-   repo: local
    hooks:
    -   id: check_pip
        name: Check pip file
        description: This hook checks if requirements-dev.txt is up to date.
        language: system
        entry: python -m scripts.check_pip_requirements
        pass_filenames: false
        args: ["--compare"]
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Erfan
  • 40,971
  • 8
  • 66
  • 78
  • 1
    if you could improve the docs that'd be great -- [`pass_filenames`](https://pre-commit.com/#hooks-pass_filenames), [arguments patterns in hooks](https://pre-commit.com/#arguments-pattern-in-hooks) – anthony sottile Sep 24 '20 at 00:17
  • 1
    Will definitely do, thanks for the updated answer and explanation. – Erfan Sep 24 '20 at 10:28
  • Submitted the [PR](https://github.com/pre-commit/pre-commit.com/pull/401) @AnthonySottile – Erfan Oct 07 '20 at 21:59