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

Enable pattern matching syntax by default #2662

Closed
thisisrandy opened this issue Dec 1, 2021 · 8 comments
Closed

Enable pattern matching syntax by default #2662

thisisrandy opened this issue Dec 1, 2021 · 8 comments
Labels
C: target version Related to --target-version, e.g. autodetection S: needs discussion Needs further hashing out before ready for implementation (on desirability, feasibility, etc.) T: enhancement New feature or request

Comments

@thisisrandy
Copy link

thisisrandy commented Dec 1, 2021

Describe the bug
PEP 634 introduces structural pattern matching in python 3.10. Black at 21.11b1 (latest) does not seem to support this new syntax.

To Reproduce

Here is a basic example of the new syntax, which we will save in match_test.py:

foo = "str"
match foo:
    case str():
        print("string!")
    case int():
        print("int!")
    case _:
        print("something else!")
> python --version
Python 3.10.0

> python match_test.py
string!

> python -m black --version
python -m black, 21.11b1 (compiled: no)

> python -m black match_test.py
error: cannot format match_test.py: Cannot parse: 2:6: match foo:
Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior

Structural pattern matching (and all other new python 3.10 syntax) should be fully supported.

Environment

  • Black's version: 21.11b1
  • OS and Python version: Ubuntu 20.04.3 LTS/Python 3.10.0
@thisisrandy thisisrandy added the T: bug Something isn't working label Dec 1, 2021
@ichard26
Copy link
Collaborator

ichard26 commented Dec 1, 2021

Black 21.11b1 does have experimental support for pattern matching syntax, but since it's experimental and is / was quite buggy we decided to lock it under the --target-version py310 option, please try that first, thank you!

@JelleZijlstra
Copy link
Collaborator

It is supported, but you need to explicitly set --target-version py310 for now. We did this because the new parser is potentially unstable.

I think we should consider turning it on by default, so I'll rename this issue to reflect that suggestion. cc @isidentical

@JelleZijlstra JelleZijlstra changed the title Structural pattern matching syntax is not supported Enable pattern matching syntax by default Dec 1, 2021
@ichard26 ichard26 added C: target version Related to --target-version, e.g. autodetection S: awaiting response Waiting for futher information from OP S: needs discussion Needs further hashing out before ready for implementation (on desirability, feasibility, etc.) T: enhancement New feature or request and removed T: bug Something isn't working labels Dec 1, 2021
@thisisrandy
Copy link
Author

Thanks for the quick replies. I'll try out --target-version py310 and look forward to seeing it stable and on by default soon.

@thisisrandy
Copy link
Author

Confirmed that at least this basic example works with --target-version py310. Also for fellow VS Code pylance users, it took me a moment to figure out how to configure things, so I'll detail here. In Settings, search for @ext:ms-python.python black, then add two items to Python>Formatting: Black Args, namely --target-version and py310.

@Kos
Copy link

Kos commented Dec 2, 2021

FWIW would be great experience if error: cannot format match_test.py: Cannot parse: 2:6: match foo: would also include hint: support for match statements is experiemental, enable it using --target-version py310

@lpil
Copy link

lpil commented Dec 7, 2021

Is there a way to specify this in a configuration file? I would like the editors for all the members of my team to automatically have this flag set.

@JelleZijlstra
Copy link
Collaborator

@Kos's suggestion is implemented in 21.12b0. @lpil you can write target_version = ['py310'] in your pyproject.toml.

@ichard26
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: target version Related to --target-version, e.g. autodetection S: needs discussion Needs further hashing out before ready for implementation (on desirability, feasibility, etc.) T: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants