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
[ENH] PEP 634 - Structural Pattern Matching #4029
Comments
Python 3.10 was released on 2021-10-04 so this is now required for upstream Python compatibility.
|
PRs welcome, as usual. I would expect that at least larger parts of the C implementation in CPython can be adopted – if that's what we want to use, as opposed to a (probably simpler) Cython implementation. The parser implementation is independent, though, and I would guess that there will be Cython specific issues along the way, possibly even cases where Cython's own syntax (such as C types) interferes with pattern syntax. But we'll see about that when we run into it. In any case, this is a fairly large feature that will take time to implement, probably in several steps. So, again, PRs welcome. |
I don't know that there's much C implementation to take. If you look at the byte-code for a function that uses pattern matching, it's mostly broken down into a set of comparisons. E.g.
So I think it's mostly a parsing then AST manipulation issue. It can probably be split into a parsing PR, and then successive PRs to make individual types of patterns work successfully. (I wasn't planning to do any of this myself any time soon though). |
Ok, I was kinda expecting that this might be the case. Thanks for checking. Then I guess it's really just about generating Cython code (or syntax nodes directly) to implement the comparisons. That merits its own transform class, best also in its own module. |
I have an implementation that passes the CPython test_patma.py tests (which I think is the extent of the testing in CPython). I'll submit it in chunks (with the parser changes already submitted being the first chunk) in the hope of making it a little more tractable to review. If anyone wants to test/use it in advance then the "Or pattern" branch is the final one and covers basically everything. |
As I said above, I'm going to submit this in bits (with #4813) effectively being the first. I'd like to make it easy for people to try it and give some early feedback (we did get some useful reports from people testing dataclasses and assignment expressions for example). What I was proposing to do was to make a Does that sound reasonable? |
In the absence of comments (and because it's very easy to delete if it is a problem...) I've created a branch in the cython repository https://github.com/cython/cython/tree/patma-preview If anyone wants to try out pattern matching before it's officially added to Cython then use that. It's obviously "at your own risk" but I'd be happy to hear of any issues there. It'll hopefully be merged gradually in little bits... |
This comment was marked as off-topic.
This comment was marked as off-topic.
Are there any updates on this or some possible fixes that can be applied? Seems like, the patma-preview patch is a bit to old to be used. |
I've bumped the patma-preview branch to be up to date with the latest master. I should point out:
|
PEP 634 defines a new "switch-statement-like syntax": https://www.python.org/dev/peps/pep-0634/ that will appear in Python 3.10. Being a Python language feature, Cython should aim to support this.
Additional context
__match_args__
(this can be implemented separately from the language support though)if ... elif ...
statement: Implementation of basic pattern matching cases #6000The text was updated successfully, but these errors were encountered: