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

Total
1
Shares

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


Solution

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

Leave a Reply

Your email address will not be published. Required fields are marked *