Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-commit hook fails when using required-version #2493

Closed
spookylukey opened this issue Sep 13, 2021 · 5 comments
Closed

pre-commit hook fails when using required-version #2493

spookylukey opened this issue Sep 13, 2021 · 5 comments
Labels
C: configuration CLI and configuration C: integrations Editor plugins and other integrations T: documentation Improvements to the docs (e.g. new topic, correction, etc)

Comments

@spookylukey
Copy link

spookylukey commented Sep 13, 2021

This is attempting to use the pre-commit hook as described under version control docs along with setting --required-version in a configuration file. The pre-commit hook fails with:

Oh no! 馃挜 馃挃 馃挜 The required version `21.8b0` does not match the running version `0.1.dev1+ga8b4665`!

To Reproduce

First, a temporary folder/venv to test things in:

mkdir black-pre-commit-test
cd black-pre-commit-test
`which python3.9` -m venv venv
source venv/bin/activate

Then this script:

git init
pip install pre-commit
pre-commit install


cat > .pre-commit-config.yaml << EOF
repos:
  - repo: https://github.com/psf/black
    rev: 21.8b0
    hooks:
      - id: black
        language_version: python3
EOF

cat > pyproject.toml << EOF
[tool.black]
required-version = '21.8b0'
EOF

git add .pre-commit-config.yaml

touch test.py
pre-commit run black --files test.py

Expected output

black....................................................................Passed

Actual output

black....................................................................Failed
- hook id: black
- exit code: 1

Oh no! 馃挜 馃挃 馃挜 The required version `21.8b0` does not match the running version `0.1.dev1+ga8b4665`!

The same happens if doing a normal "git commit" - it is just easier to run the hook via pre-commit run directly. To test with hook:

git commit .pre-commit-config.yaml -m "pre-commit"
git add test.py
git commit test.py -m "test"

Environment:

  • Version: 21.8b0
  • OS: Linux,
  • Python: 3.9.5 (i.e. which python3.9 used in the beginning of the instructions above)
  • However, the version installed by pre-commit in ~/.cache/pre-commit/repo8_9vcuko/py_env-python3/bin/python3 is different - it is whatever is pointed to by which python3, which was Python 3.8.10 first time I ran it. By changing language_version in .pre-commit-config.yaml I can get a different version, e.g. Python 3.9.5, but it made no difference.

Does this bug also happen on main?

Haven't tried, because this is specific to pinning to published versions.

Notes

I can see the incorrect Black version by doing this:

~/.cache/pre-commit/repo8_9vcuko/py_env-python3.9/bin/black --version

However, I haven't been able to further debug because I don't know how this line works:

from _black_version import version as __version__
- it appears to be doing some magical import.

@spookylukey spookylukey added the T: bug Something isn't working label Sep 13, 2021
@MarcoGorelli
Copy link
Contributor

MarcoGorelli commented Sep 16, 2021

a workaround until this is fixed, if you need required-version, could be to make it a local hook - then it'll be installed from PyPI and it'll have the right version

- repo: local
  hooks:
  - id: black
    name: black
    id: black
    entry: black
    language: python
    types_or: [python, pyi]
    additional_dependencies: [black==21.8b0]

@MarcoGorelli
Copy link
Contributor

Possible solutions could be:

  • ask pre-commit to give an option to not to a shallow clone
  • migrate away from setuptools-scm in black, so that the version can be detected even with a shallow clone
  • just recommend the above workaround if someone needs to both set required-version and use pre-commit in the version control integration page in the docs

I think the chances of pre-commit being OK with the first one are 0%. Can't imagine the second one would be too welcome here. Is the third one acceptable? cc @ichard26 if you have any thoughts on this one

@ichard26 ichard26 added C: configuration CLI and configuration C: integrations Editor plugins and other integrations labels Dec 30, 2021
aucampia added a commit to aucampia/rdflib that referenced this issue Jan 30, 2022
This is to work around this issue: psf/black#2493

Without doing this the pre-commit hook for black fails as it thinks it
got the wrong version.
aucampia added a commit to aucampia/rdflib that referenced this issue Jan 30, 2022
This is to work around this issue: psf/black#2493

Without doing this the pre-commit hook for black fails as it thinks it
got the wrong version.
@Jackenmen
Copy link
Contributor

Jackenmen commented Mar 29, 2022

Here's a workaround:

repos:
  - repo: https://github.com/psf/black
    # The `refs/tags/<tag>:refs/tags/<tag>` is needed for black's required-version to work:
    # https://github.com/psf/black/issues/2493#issuecomment-1081987650
    rev: 'refs/tags/22.3.0:refs/tags/22.3.0'
    hooks:
      - id: black

Because pre-commit accepts a git refspec, this way you can tell it to request the named ref as well.
Here's what pre-commit does more or less:

ubuntu@JakubWin10PC:/tmp
$ git init --template= failing-example
Initialized empty Git repository in /tmp/failing-example/.git/
ubuntu@JakubWin10PC:/tmp
$ cd failing-example
ubuntu@JakubWin10PC:/tmp/failing-example
$ git remote add origin https://github.com/psf/black
ubuntu@JakubWin10PC:/tmp/failing-example
$ git -c protocol.version=2 fetch origin 22.3.0 --depth=1
remote: Enumerating objects: 301, done.
remote: Counting objects: 100% (301/301), done.
remote: Compressing objects: 100% (267/267), done.
remote: Total 301 (delta 12), reused 148 (delta 6), pack-reused 0
Receiving objects: 100% (301/301), 1.14 MiB | 554.00 KiB/s, done.
Resolving deltas: 100% (12/12), done.
From https://github.com/psf/black
 * tag               22.3.0     -> FETCH_HEAD
ubuntu@JakubWin10PC:/tmp/failing-example
$ ls .git/refs/tags/



ubuntu@JakubWin10PC:/tmp
$ git init --template= working-example
Initialized empty Git repository in /tmp/working-example/.git/
ubuntu@JakubWin10PC:/tmp
$ cd working-example/
ubuntu@JakubWin10PC:/tmp/working-example
$ git remote add origin https://github.com/psf/black
ubuntu@JakubWin10PC:/tmp/working-example
$ git -c protocol.version=2 fetch origin refs/tags/22.3.0:refs/tags/22.3.0 --depth=1
remote: Enumerating objects: 301, done.
remote: Counting objects: 100% (301/301), done.
remote: Compressing objects: 100% (267/267), done.
remote: Total 301 (delta 12), reused 148 (delta 6), pack-reused 0
Receiving objects: 100% (301/301), 1.14 MiB | 544.00 KiB/s, done.
Resolving deltas: 100% (12/12), done.
From https://github.com/psf/black
 * [new tag]         22.3.0     -> 22.3.0
ubuntu@JakubWin10PC:/tmp/working-example
$ ls .git/refs/tags/
22.3.0

aucampia added a commit to aucampia/rdflib that referenced this issue Mar 29, 2022
See this psf/black#2966

Also simplified pre-commit config of black based on a suggestion from
https://github.com/jack1142 in psf/black#2493 (comment)
aucampia added a commit to aucampia/rdflib that referenced this issue Mar 29, 2022
See this psf/black#2966

Also simplified pre-commit config of black based on a suggestion from
https://github.com/jack1142 in psf/black#2493 (comment)
ashleysommer pushed a commit to RDFLib/rdflib that referenced this issue Mar 30, 2022
See this psf/black#2966

Also simplified pre-commit config of black based on a suggestion from
https://github.com/jack1142 in psf/black#2493 (comment)
@felix-hilden
Copy link
Collaborator

Let's mention that in the docs.

@felix-hilden felix-hilden added T: documentation Improvements to the docs (e.g. new topic, correction, etc) and removed T: bug Something isn't working labels Mar 31, 2022
andthum added a commit to andthum/hpc_submit_scripts that referenced this issue May 13, 2022
The black pre-commit hook fails when using the `required-version` option
(either as command line option or in a config file).  This commit uses
the fix proposed in
psf/black#2493 (comment).
@hauntsaninja
Copy link
Collaborator

Another way to fix this is using the new official mirror at https://github.com/psf/black-pre-commit-mirror , see #3828

andthum added a commit to andthum/mdtools that referenced this issue Oct 13, 2023
* Update https://github.com/pre-commit/pre-commit-hooks from `v4.4.0` to
  `v4.5.0`.

* Downgrade https://github.com/markdownlint/markdownlint from `v0.12.0`
  to `v0.9.0` (see comment in `.pre-commit-config.yaml`).

* Change source of `black` from https://github.com/psf/black to
  https://github.com/psf/black-pre-commit-mirror, because this is now
  the official source of the `black` pre-commit hook (see
  psf/black#2493 (comment)).
andthum added a commit to andthum/hpc_submit_scripts that referenced this issue Oct 13, 2023
* Update https://github.com/pre-commit/pre-commit-hooks from `v4.4.0` to
  `v4.5.0`.

* Downgrade https://github.com/markdownlint/markdownlint from `v0.12.0`
  to `v0.9.0` (see comment in `.pre-commit-config.yaml`).

* Change source of `black` from https://github.com/psf/black to
  https://github.com/psf/black-pre-commit-mirror, because this is now
  the official source of the `black` pre-commit hook (see
  psf/black#2493 (comment)).
andthum added a commit to andthum/lintf2_ether_ana_postproc that referenced this issue Oct 13, 2023
* Update https://github.com/pre-commit/pre-commit-hooks from `v4.4.0` to
  `v4.5.0`.

* Downgrade https://github.com/markdownlint/markdownlint from `v0.12.0`
  to `v0.9.0` (see comment in `.pre-commit-config.yaml`).

* Change source of `black` from https://github.com/psf/black to
  https://github.com/psf/black-pre-commit-mirror, because this is now
  the official source of the `black` pre-commit hook (see
  psf/black#2493 (comment)).
andthum added a commit to andthum/transfer_Li that referenced this issue Oct 13, 2023
* Update https://github.com/pre-commit/pre-commit-hooks from `v4.4.0` to
  `v4.5.0`.

* Downgrade https://github.com/markdownlint/markdownlint from `v0.12.0`
  to `v0.9.0` (see comment in `.pre-commit-config.yaml`).

* Change source of `black` from https://github.com/psf/black to
  https://github.com/psf/black-pre-commit-mirror, because this is now
  the official source of the `black` pre-commit hook (see
  psf/black#2493 (comment)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: configuration CLI and configuration C: integrations Editor plugins and other integrations T: documentation Improvements to the docs (e.g. new topic, correction, etc)
Projects
None yet
Development

No branches or pull requests

6 participants