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

Python 3.10 pattern matching #47

Open
biqqles opened this issue May 28, 2021 · 6 comments
Open

Python 3.10 pattern matching #47

biqqles opened this issue May 28, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@biqqles
Copy link
Owner

biqqles commented May 28, 2021

Classes should support the PEP 622 Structural Pattern Matching feature of Python 3.10. From reading the PEP it seems this should be rather simple, simply adding a __match_args__ tuple which is the same as the arguments to __init__.

@biqqles biqqles added the enhancement New feature or request label May 28, 2021
@tirkarthi
Copy link

This is done in dataclasses at https://github.com/python/cpython/blob/8ab5b7eecaa1a1a249afbd3bf8358ce2b12e4713/Lib/dataclasses.py#L1089-L1092 where __match_args__ is set depending upon match_args parameter value.

@biqqles
Copy link
Owner Author

biqqles commented May 31, 2021

Thanks for the info @tirkarthi, I knew from the PEP what to put in the attribute but didn't realise dataclasses had a decorator option for it. Not sure why, seems like there is no downside to generating it?

@tirkarthi
Copy link

There is no backwards compatibility issue but some users wanted an option to turn it off. Detailed discussion : https://bugs.python.org/issue43764

I added support for attrs too to support pattern matching. It also added match_args parameter.
python-attrs/attrs#815

@tirkarthi
Copy link

Off topic but dataclasses also supports kw_only and slots too . Doc issue https://bugs.python.org/issue43997

@biqqles
Copy link
Owner Author

biqqles commented Jun 1, 2021

I see. kw_only is trivial to add. I shall have to benchmark slots when Python 3.10 is released. btw, 3.10 not being released yet means I see the "implements all the decorator options of dataclasses" claim as still being accurate.

@biqqles
Copy link
Owner Author

biqqles commented Jun 1, 2021

I set up a new branch 3.10-equivalence to track the addition of the new decorator options.

biqqles added a commit that referenced this issue Jun 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants