The hook `black` requires pre-commit version 2.9.2 but version 2.6.0 is installed
Asked Answered
P

2

7

I am having trouble with pre-commit and black.

Everything worked fine until I cleared the cache with pre-commit clean. Now I always get the error

The hook black requires pre-commit version 2.9.2 but version 2.6.0 is installed. Perhaps run `pip install --upgrade pre-commit

If I check my version I am running the latest pre-commmit version (v2.12.1). Also, if I run the recommended command, nothing changes and I get the same error. If I deactivate the black hook the error disappears, so I at least know it's a problem with black.

I tried changing version of black from stable to the most recent, but nothing helps.

Any ideas how I can do to troubleshoot this?

My pre-commit config:

  - repo: https://github.com/ambv/black
    rev: stable
    hooks:
      - id: black
        language_version: python3.8
        exclude: |
          (?x)(
              migrations/|
              config/|
              _build/|
              buck-out/|
              build/|
              dist/
          )

Help is greatly appreciated

Pregnable answered 19/5, 2021 at 9:45 Comment(2)
any chance that this hook gets executed with a virtual env different from the one you access from your command line ? for example : is your main python version also 3.8 ?Lindstrom
Yes, I am having an environment activated. But I get the same error if I deactivate my environment. I also get the error when I deinstall pre-commit in my environment which is quite strange. And I get the error when I use no environment at all. My main python version is 3.8Pregnable
J
17

rev: stable is not a supported configuration -- when you run you'll also get a warning telling you exactly that:

$ pre-commit  run black --all-files
[WARNING] The 'rev' field of repo 'https://github.com/ambv/black' appears to be a mutable reference
(moving tag / branch).  Mutable references are never updated after first install and are not
supported.  See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details.
Hint: `pre-commit autoupdate` often fixes this.
...

the warning also tells you the rationale for this and explains the behaviour you saw: mutable references are never updated after first install -- so the version in your cache was from an old old version of black and when you cleared it that got updated to the latest

the latest version of black uses types_or in its definition which requires a newer version of pre-commit

an aside, your exclude isn't doing what you want -- pre-commit only runs on files which are checked into your repository so excluding things like dist and _build isn't necessary

to fix the actual error message you're seeing, make sure to pip install --upgrade pre-commit (and ~potentially pre-commit install to reset the git hooks if you've moved where pre-commit was installed from the first time) -- which -a pre-commit can help you locate all of your installations


disclaimer: I am the creator of pre-commit

Justify answered 19/5, 2021 at 15:42 Comment(1)
Thank you very much Anthony! I wasn't aware stable was not supported as config. Updating the rev to the newest version and doing a pre-commit install did the trick. I thought a pre-commit clean would already reset the hooks, but it's good to know this is done via pre-commit install. Again, thank you for your support, quite amazing to receive help directly from the creator! And thank you for the great product :)Pregnable
S
0

I also had this problem and the other answer did not work for me.

I fixed it by uninstalling the old pre-commit that I had installed with snap and installing a new pre-commit with conda:

conda install -c conda-forge pre-commit
Sicanian answered 10/2, 2023 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.