-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Support Python 3.10 pattern matching syntax #2242
Comments
Note that the syntax has changed slightly since PEP 622 was superseded. PEP 634 (the accepted PEP) has the updated grammar. I'm happy to help review or answer any questions on this once the work begins. I could probably also find time to help out with the implementation, if needed (probably can't take on the whole parser refactor though). I've contributed a small amount of code to Black in the past. Definitely looking forward to this support! |
This will probably require #2318. |
Is there anything needed from the community to support this following the release of Python 3.10? I apologize if asking this is inappropriate given the status update on Jun 9th from @JelleZijlstra Thank you for the hard work on Black. |
May I ask if there's any news in regards to this? |
I think here is the latest stuff https://docs.google.com/document/d/1QPqm01E7MIRi_l4jrgCGOFVThP_HINnkm4hmVZC_CSg/edit#heading=h.qokppq9m1uzy (from #2318 ) and here Instagram/LibCST#285 (comment) you can follow along the work here Instagram/LibCST@main...zsol:parser |
I do not understand all comments to this issue, but I wonder if there is a branch or something already in which black can handle |
I spent some time this morning looking for a temporary workaround until a permanent solution is found. I was able to add limited support to pattern matching, and I think most of the syntax should be supported with my changes, however some more advanced syntax will still crash I have retained the use of This being a temporary workaround, I can't guarantee that the formatting of some expressions in Anyhow, here is the branch with the limited pattern matching support: https://github.com/argyle-engineering/black/tree/add-minimal-support-for-pattern-matching. Have a look at argyle-engineering#1 for the PR that adds this functionality (also has some context in the body). You can target hash Some examples of syntax that this fork supports (taken from PEP636): match point:
case Point(x, y) if x == y:
print(f"Y=X at {x}")
case Point(x, y):
print(f"Not on the diagonal") match command.split():
case ["go", direction] if direction in current_room.exits:
current_room = current_room.neighbor(direction)
case ["go", _]:
print("Sorry, you can't go that way") match command.split():
case ["drop", *objects]:
for obj in objects:
character.drop(obj, current_room) match command.split():
case ["north"] | ["go", "north"]:
current_room = current_room.neighbor("north")
case ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"]:
... # Code for picking up the given object Example of syntax not supported: match command.split():
case ["go", ("north" | "south" | "east" | "west") as direction]:
current_room = current_room.neighbor(direction) I do not intend to make a PR to this repository here, as this is a quick workaround that won't be able to cover every pattern matching use case. |
Thank you! I heard that @isidentical also came up with a lib2to3 patch that works for most but not all match syntax. I think we should just go with one of your patches to unblock users from using most match syntax, fix bugs as they come up, and hopefully switch to a more powerful parser in the future. |
Great solution @kennipj!
I also had the same problem, so I've simply added
Here is my implementation for people who are interested: isidentical@c22fca6. For testing, I also wrote a couple of scripts which can determine how much of the stuff black can actually parse: https://gist.github.com/isidentical/34f7d90e0b0d59d2cc32cc32d58d4f2e. Currently it parses 315/322 test cases out there (2 fails when |
@isidentical might already be using it, but I've written a few thousand lines of crazy pattern matching code for Python's regression test suite. It can probably help stress your implementation and shake out any edge cases! |
Ah I should have known about this when I started, very useful. Running @isidentical's test, results in 245/324 test cases parsed (79 failures) with my solution. Your solution is clearly more robust 😉 I probably won't address the failing cases, as it will take considerable to cover the remaining cases, and seeing as @isidentical has a more mature solution, it would probably be better to use that instead. EDIT: Took a quick look at some of the failures, and with a couple quick fixes, managed to get it down to just 19 tests failing. All cases with |
So, re the above. Does this mean black will be getting support for the new syntax shortly? Or are we to switch to using one of the forks? |
What I'd like to see is that we land support based on @isidentical's patch, but somebody needs to step up to actually make a PR and get it landed. I might work on that if I have time, but I can't give any guarantees. |
I would love to contribute @JelleZijlstra! If the lack of full support and other issues I have mentioned is considered and agreed upon I can convert my PR to a patch. And iterate over it for better support. |
That would be great! I'd be OK with landing partial support first and iterating on the rest later. |
Cool, will send a patch within this week! |
Partial implementation for #2242. Only works when explicitly stated -t py310. Co-authored-by: Richard Si <[email protected]>
Partial implementation for #2242. Only works when explicitly stated -t py310. Co-authored-by: Richard Si <[email protected]>
I am going to close this because thanks to @isidentical's awesome work we shipped 21.11b1 with basic match support. There may be some cases that don't work, but we should open new issues for those as we run into them. |
Got the last version and it still fails to parse the match keyword on my end, with the exact same example as the OP, any ideas? |
@AlozoR make sure you pass |
Just saw that! Thanks! |
FYI: "python.formatting.blackArgs": [
"--target-version",
"py310"
], |
FYI:
|
With the online formatter match-case doesn't work unless you turn on "skip temporary sanity checks" even when targeting py3.10. |
We do not control the Black Playground. Please report issues with it to https://github.com/jpadilla/black-playground. (Most likely it's running Black under a version <3.10 on the backend.) |
The built-in lib2to3 does not support pattern matching (Python 3.10+): https://docs.python.org/3.11/library/2to3.html#module-lib2to3 The [black][] project managed to get some level of parsing support for `match` out of their modified version `blib2to3`, see: 1. psf/black#2242 2. psf/black#2586 [black]: https://github.com/psf/black This change adds `black` as a dependency and switches to using `blib2to3` to parse. Tests pass, but that's all that's been attempted thus far.
The built-in lib2to3 does not support pattern matching (Python 3.10+): https://docs.python.org/3.11/library/2to3.html#module-lib2to3 The [black][] project managed to get some level of parsing support for `match` out of their modified version `blib2to3`, see: 1. psf/black#2242 2. psf/black#2586 [black]: https://github.com/psf/black This change adds `black` as a dependency and switches to using `blib2to3` to parse. Tests pass, but that's all that's been attempted thus far.
The built-in lib2to3 does not support pattern matching (Python 3.10+): https://docs.python.org/3.11/library/2to3.html#module-lib2to3 The [black][] project managed to get some level of parsing support for `match` out of their modified version `blib2to3`, see: 1. psf/black#2242 2. psf/black#2586 [black]: https://github.com/psf/black This change adds `black` as a dependency and switches to using `blib2to3` to parse. Tests pass, but that's all that's been attempted thus far.
) * Use blib2to3 parser to support match statement The built-in lib2to3 does not support pattern matching (Python 3.10+): https://docs.python.org/3.11/library/2to3.html#module-lib2to3 The [black][] project managed to get some level of parsing support for `match` out of their modified version `blib2to3`, see: 1. psf/black#2242 2. psf/black#2586 [black]: https://github.com/psf/black This change adds `black` as a dependency and switches to using `blib2to3` to parse. Tests pass, but that's all that's been attempted thus far. * Add a _unreleased changelog for blib2to3 integration * fix mypy in docspec/src/docspec/__init__.py * fix mypy * update changelog format * update GitHub workflow * fix workflow * insert PR url * use `--no-venv-check` also for `slap run` in docs job --------- Co-authored-by: Niklas Rosenstein <[email protected]>
Thanks, this also helps in getting the black commit hook to work, e.g. in the pre-commit-config.yaml:
|
Is your feature request related to a problem? Please describe.
Python 3.10 has added support for pattern matching. It will be good to support it. https://www.python.org/dev/peps/pep-0622/
Describe the solution you'd like .
Describe alternatives you've considered
Reproducer
Additional context Add any other context or screenshots about the feature request
here.
The text was updated successfully, but these errors were encountered: