Skip to content

Commit 750f484

Browse files
authored
bpo-43764: Add match_args=False parameter to dataclass decorator and to make_dataclasses function. (GH-25337)
Add match_args=False parameter to dataclass decorator and to make_dataclass function.
1 parent c3a478b commit 750f484

File tree

4 files changed

+81
-19
lines changed

4 files changed

+81
-19
lines changed

Doc/library/dataclasses.rst

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ directly specified in the ``InventoryItem`` definition shown above.
4646
Module-level decorators, classes, and functions
4747
-----------------------------------------------
4848

49-
.. decorator:: dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)
49+
.. decorator:: dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True)
5050

5151
This function is a :term:`decorator` that is used to add generated
5252
:term:`special method`\s to classes, as described below.
@@ -79,7 +79,7 @@ Module-level decorators, classes, and functions
7979
class C:
8080
...
8181

82-
@dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)
82+
@dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True)
8383
class C:
8484
...
8585

@@ -161,6 +161,14 @@ Module-level decorators, classes, and functions
161161
:meth:`__setattr__` or :meth:`__delattr__` is defined in the class, then
162162
:exc:`TypeError` is raised. See the discussion below.
163163

164+
- ``match_args``: If true (the default is ``True``), the
165+
``__match_args__`` tuple will be created from the list of
166+
parameters to the generated :meth:`__init__` method (even if
167+
:meth:`__init__` is not generated, see above). If false, or if
168+
``__match_args__`` is already defined in the class, then
169+
``__match_args__`` will not be generated.
170+
171+
164172
``field``\s may optionally specify a default value, using normal
165173
Python syntax::
166174

@@ -325,16 +333,17 @@ Module-level decorators, classes, and functions
325333

326334
Raises :exc:`TypeError` if ``instance`` is not a dataclass instance.
327335

328-
.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)
336+
.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True)
329337

330338
Creates a new dataclass with name ``cls_name``, fields as defined
331339
in ``fields``, base classes as given in ``bases``, and initialized
332340
with a namespace as given in ``namespace``. ``fields`` is an
333341
iterable whose elements are each either ``name``, ``(name, type)``,
334342
or ``(name, type, Field)``. If just ``name`` is supplied,
335343
``typing.Any`` is used for ``type``. The values of ``init``,
336-
``repr``, ``eq``, ``order``, ``unsafe_hash``, and ``frozen`` have
337-
the same meaning as they do in :func:`dataclass`.
344+
``repr``, ``eq``, ``order``, ``unsafe_hash``, ``frozen``, and
345+
``match_args`` have the same meaning as they do in
346+
:func:`dataclass`.
338347

339348
This function is not strictly required, because any Python
340349
mechanism for creating a new class with ``__annotations__`` can

Lib/dataclasses.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,17 @@
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

10271034
def 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

11991208
def 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

12651275
def replace(obj, /, **changes):

Lib/test/test_dataclasses.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,6 +3440,47 @@ class X:
34403440
c: int
34413441
self.assertEqual(X.__match_args__, ("a", "b", "c"))
34423442

3443+
def test_match_args_argument(self):
3444+
@dataclass(match_args=False)
3445+
class X:
3446+
a: int
3447+
self.assertNotIn('__match_args__', X.__dict__)
3448+
3449+
@dataclass(match_args=False)
3450+
class Y:
3451+
a: int
3452+
__match_args__ = ('b',)
3453+
self.assertEqual(Y.__match_args__, ('b',))
3454+
3455+
@dataclass(match_args=False)
3456+
class Z(Y):
3457+
z: int
3458+
self.assertEqual(Z.__match_args__, ('b',))
3459+
3460+
# Ensure parent dataclass __match_args__ is seen, if child class
3461+
# specifies match_args=False.
3462+
@dataclass
3463+
class A:
3464+
a: int
3465+
z: int
3466+
@dataclass(match_args=False)
3467+
class B(A):
3468+
b: int
3469+
self.assertEqual(B.__match_args__, ('a', 'z'))
3470+
3471+
def test_make_dataclasses(self):
3472+
C = make_dataclass('C', [('x', int), ('y', int)])
3473+
self.assertEqual(C.__match_args__, ('x', 'y'))
3474+
3475+
C = make_dataclass('C', [('x', int), ('y', int)], match_args=True)
3476+
self.assertEqual(C.__match_args__, ('x', 'y'))
3477+
3478+
C = make_dataclass('C', [('x', int), ('y', int)], match_args=False)
3479+
self.assertNotIn('__match__args__', C.__dict__)
3480+
3481+
C = make_dataclass('C', [('x', int), ('y', int)], namespace={'__match_args__': ('z',)})
3482+
self.assertEqual(C.__match_args__, ('z',))
3483+
34433484

34443485
if __name__ == '__main__':
34453486
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add match_args parameter to @dataclass decorator to allow suppression of
2+
__match_args__ generation.

0 commit comments

Comments
 (0)