154154
155155# __match_args__
156156#
157- # | no | yes | <--- class has __match_args__ in __dict__?
158- # +=======+=======+
159- # | add | | <- the default
160- # +=======+=======+
161- # __match_args__ is always added unless the class already defines it. It is a
162- # tuple of __init__ parameter names; non-init fields must be matched by keyword.
157+ # +--- match_args= parameter
158+ # |
159+ # v | | |
160+ # | no | yes | <--- class has __match_args__ in __dict__?
161+ # +=======+=======+=======+
162+ # | False | | |
163+ # +-------+-------+-------+
164+ # | True | add | | <- the default
165+ # +=======+=======+=======+
166+ # __match_args__ is a tuple of __init__ parameter names; non-init fields must
167+ # be matched by keyword.
163168
164169
165170# Raised when an attempt is made to modify a frozen class.
@@ -830,7 +835,8 @@ def _hash_exception(cls, fields, globals):
830835# version of this table.
831836
832837
833- def _process_class (cls , init , repr , eq , order , unsafe_hash , frozen ):
838+ def _process_class (cls , init , repr , eq , order , unsafe_hash , frozen ,
839+ match_args ):
834840 # Now that dicts retain insertion order, there's no reason to use
835841 # an ordered dict. I am leveraging that ordering here, because
836842 # derived class fields overwrite base class fields, but the order
@@ -1016,16 +1022,17 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
10161022 cls .__doc__ = (cls .__name__ +
10171023 str (inspect .signature (cls )).replace (' -> NoneType' , '' ))
10181024
1019- if '__match_args__' not in cls .__dict__ :
1020- cls .__match_args__ = tuple (f .name for f in field_list if f .init )
1025+ if match_args :
1026+ _set_new_attribute (cls , '__match_args__' ,
1027+ tuple (f .name for f in field_list if f .init ))
10211028
10221029 abc .update_abstractmethods (cls )
10231030
10241031 return cls
10251032
10261033
10271034def dataclass (cls = None , / , * , init = True , repr = True , eq = True , order = False ,
1028- unsafe_hash = False , frozen = False ):
1035+ unsafe_hash = False , frozen = False , match_args = True ):
10291036 """Returns the same class as was passed in, with dunder methods
10301037 added based on the fields defined in the class.
10311038
@@ -1035,11 +1042,13 @@ def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False,
10351042 repr is true, a __repr__() method is added. If order is true, rich
10361043 comparison dunder methods are added. If unsafe_hash is true, a
10371044 __hash__() method function is added. If frozen is true, fields may
1038- not be assigned to after instance creation.
1045+ not be assigned to after instance creation. If match_args is true,
1046+ the __match_args__ tuple is added.
10391047 """
10401048
10411049 def wrap (cls ):
1042- return _process_class (cls , init , repr , eq , order , unsafe_hash , frozen )
1050+ return _process_class (cls , init , repr , eq , order , unsafe_hash ,
1051+ frozen , match_args )
10431052
10441053 # See if we're being called as @dataclass or @dataclass().
10451054 if cls is None :
@@ -1198,7 +1207,7 @@ def _astuple_inner(obj, tuple_factory):
11981207
11991208def make_dataclass (cls_name , fields , * , bases = (), namespace = None , init = True ,
12001209 repr = True , eq = True , order = False , unsafe_hash = False ,
1201- frozen = False ):
1210+ frozen = False , match_args = True ):
12021211 """Return a new dynamically created dataclass.
12031212
12041213 The dataclass name will be 'cls_name'. 'fields' is an iterable
@@ -1259,7 +1268,8 @@ class C(Base):
12591268 # of generic dataclassses.
12601269 cls = types .new_class (cls_name , bases , {}, lambda ns : ns .update (namespace ))
12611270 return dataclass (cls , init = init , repr = repr , eq = eq , order = order ,
1262- unsafe_hash = unsafe_hash , frozen = frozen )
1271+ unsafe_hash = unsafe_hash , frozen = frozen ,
1272+ match_args = match_args )
12631273
12641274
12651275def replace (obj , / , ** changes ):
0 commit comments