biqqles / dataclassy

A fast and flexible reimplementation of data classes
https://pypi.org/project/dataclassy
Mozilla Public License 2.0
81 stars 9 forks source link

Python 3.10 pattern matching #47

Open biqqles opened 3 years ago

biqqles commented 3 years ago

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__.

tirkarthi commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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. https://github.com/python-attrs/attrs/pull/815

tirkarthi commented 3 years ago

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

biqqles commented 3 years ago

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 commented 3 years ago

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