Open biqqles opened 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.
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?
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
Off topic but dataclasses also supports kw_only and slots too . Doc issue https://bugs.python.org/issue43997
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.
I set up a new branch 3.10-equivalence
to track the addition of the new decorator options.
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__
.