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

pre-commit hook fails to format code with the Structural Pattern Matching #2732

Closed
CelestialGuru opened this issue Dec 28, 2021 · 3 comments
Closed
Labels
C: integrations Editor plugins and other integrations R: not a bug This is deliberate behavior of Black. S: awaiting response Waiting for futher information from OP T: user support OP looking for assistance or answers to a question

Comments

@CelestialGuru
Copy link

Describe the bug

pre-commit hook fails to format code with the Structural Pattern Matching PEP 0622.

To Reproduce

Files

.pre-commit-config.yaml:

repos:
  - repo: https://github.com/psf/black
    rev: 21.12b0
    hooks:
      - id: black

pyproject.toml:

[tool.black]
target-version = ["py310"]

file_with_310_features.py

match "this":
    case "that":
        print(1  +  2)

file_without_310_features.py

print(1  +  2)

Setup

pip install black==21.12b0 pre-commit
git init
git add .
pre-commit install

And run it with these arguments:

pre-commit run black --all-files

The resulting error is:

black....................................................................Failed

  • hook id: black
  • exit code: 123
  • files were modified by this hook

reformatted file_without_310_features.py
error: cannot format file_with_310_features.py: cannot use --safe with this file; failed to parse source file.
Oh no! \U0001f4a5 \U0001f494 \U0001f4a5
1 file reformatted, 1 file failed to reformat.

Expected behavior

Pre-commit hooks should format both file_with_310_features.py and file_without_310_features.py; they are valid python 3.10.

Environment

  • Black's version: 21.12b0
  • OS and Python version: Windows 10, Python 3.10.0

FYI
I can format the file with Python 3.10 features just fine:

black file_with_310_features.py

reformatted file_with_310_features.py
All done! ✨ 🍰 ✨
1 file reformatted,

@CelestialGuru CelestialGuru added the T: bug Something isn't working label Dec 28, 2021
@isidentical
Copy link
Collaborator

The error is due to the version of the python environment, not black. You can configure default_language_version to 3.10 though, which should make this work.

@ichard26 ichard26 added C: integrations Editor plugins and other integrations R: not a bug This is deliberate behavior of Black. S: awaiting response Waiting for futher information from OP T: user support OP looking for assistance or answers to a question and removed T: bug Something isn't working labels Dec 29, 2021
@ichard26
Copy link
Collaborator

I'll also note that this situation should be less confusing with the recent merge of GH-2786 which hints at checking your runtime Python version to make sure it supports the syntax you're throwing Black at.

@JelleZijlstra
Copy link
Collaborator

I don't think there's anything for us to do here. Let us know if you have further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: integrations Editor plugins and other integrations R: not a bug This is deliberate behavior of Black. S: awaiting response Waiting for futher information from OP T: user support OP looking for assistance or answers to a question
Projects
None yet
Development

No branches or pull requests

4 participants