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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

black not working with required_version in pre-commit #3788

Closed
shahzebsiddiqui opened this issue Jul 12, 2023 · 3 comments
Closed

black not working with required_version in pre-commit #3788

shahzebsiddiqui opened this issue Jul 12, 2023 · 3 comments

Comments

@shahzebsiddiqui
Copy link

Here is my black configuration in pyproject.toml

[tool.black]
required-version = '23.3.0'
target-version = ['py38', 'py39', 'py310', 'py311']
skip_magic_trailing_comma = true
line-length = 88
verbose = true

I have a .pre-commit-config.yaml file that will install the black hook

(buildtest)  ~/Documents/github/buildtest/ [pyproject.toml+*] cat .pre-commit-config.yaml                          
ci:
    autoupdate_branch: ''
    autoupdate_commit_msg: '[pre-commit.ci] pre-commit weekly autoupdate'
    autoupdate_schedule: weekly

repos:
  - repo: https://github.com/pycqa/isort
    rev: 5.11.5
    hooks:
      - id: isort
        args: ["--profile", "black", "--filter-files", "buildtest", "tests", "docs"]

  - repo: https://github.com/psf/black
    rev: 23.3.0
    hooks:
      - id: black
        language_version: python3

I can confirm the black version is 23.3.0

(buildtest)  ~/Documents/github/buildtest/ [pyproject.toml+*] black --version   
black, 23.3.0 (compiled: yes)
Python (CPython) 3.10.12
(buildtest)  ~/Documents/github/buildtest/ [pyproject.toml+*] pip list | grep black               
black                          23.3.0

The problem is when i install the hook pre-commit install it doesnt work and states the following

(buildtest)  ~/Documents/github/buildtest/ [pyproject.toml+*] git commit
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /Users/siddiq90/.cache/pre-commit/patch1689195589-35757.
isort....................................................................Passed
black....................................................................Failed
- hook id: black
- exit code: 1

Identified `/Users/siddiq90/Documents/github/buildtest` as project root containing a .git directory.
Sources to be formatted: "buildtest/cli/report.py", "buildtest/main.py", "buildtest/system.py", "buildtest/tools/stylecheck.py", "buildtest/tools/unittests.py", "buildtest/utils/command.py", "buildtest/utils/file.py", "buildtest/utils/shell.py", "buildtest/utils/tools.py", "tests/cli/test_history.py", "tests/cli/test_inspect.py", "tests/cli/test_report.py", "tests/schemas/test_utils.py", "tests/test_ascent.py", "tests/test_jlse.py", "tests/test_nersc.py", "tests/test_pbs.py", "tests/tools/test_stylecheck.py", "tests/utils/test_file.py"
Using configuration from project root.
required_version: 23.3.0
target_version: ['py38', 'py39', 'py310', 'py311']
skip_magic_trailing_comma: True
line_length: 88
verbose: True
Oh no! 💥 💔 💥 The required version `23.3.0` does not match the running version `0.1.dev1+gbf7a162`!

[INFO] Restored changes from /Users/siddiq90/.cache/pre-commit/patch1689195589-35757.

It is confusing because i am able to run black manually and --required-version works for me but it doesnt work when i add to configuration file

(buildtest)  ~/Documents/github/buildtest/ [pyproject.toml+*] black --required-version 23.3.0 --check buildtest/main.py
Identified `/Users/siddiq90/Documents/github/buildtest` as project root containing a .git directory.
Sources to be formatted: "buildtest/main.py"
Using configuration from project root.
target_version: ['py38', 'py39', 'py310', 'py311']
skip_magic_trailing_comma: True
line_length: 88
verbose: True
buildtest/main.py wasn't modified on disk since last run.

All done! ✨ 🍰 ✨
1 file would be left unchanged.

Is this key not accepted in pyproject.toml

required-version = '23.3.0'

Note i have this working when i dont have the key and install the precommit hook it only fails when i add this configuration in my pyproject.toml

@JelleZijlstra JelleZijlstra changed the title black not working with required_version in pyproject.toml black not working with required_version in pre-commit Jul 12, 2023
@JelleZijlstra
Copy link
Collaborator

I suspect the issue here is that pre-commit checks out the repo directly and in that case we can't figure out the right version.

@hauntsaninja
Copy link
Collaborator

Duplicate of #2493 , that issue contains a workaround

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2023
@shahzebsiddiqui
Copy link
Author

if i understand the workaround is #2493 (comment) what up with the refs being twice do i just do it once i have the following

  - repo: https://github.com/psf/black
    rev: 'refs/tags/22.3.0'
    hooks:
      - id: black
        language_version: python3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants