Skip to content

Commit 4f380db

Browse files
authored
gh-96142: add missing params to dataclass._DataclassParams (gh-96382)
1 parent 53503ff commit 4f380db

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

Lib/dataclasses.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,25 @@ class _DataclassParams:
320320
'order',
321321
'unsafe_hash',
322322
'frozen',
323+
'match_args',
324+
'kw_only',
325+
'slots',
326+
'weakref_slot',
323327
)
324328

325-
def __init__(self, init, repr, eq, order, unsafe_hash, frozen):
329+
def __init__(self,
330+
init, repr, eq, order, unsafe_hash, frozen,
331+
match_args, kw_only, slots, weakref_slot):
326332
self.init = init
327333
self.repr = repr
328334
self.eq = eq
329335
self.order = order
330336
self.unsafe_hash = unsafe_hash
331337
self.frozen = frozen
338+
self.match_args = match_args
339+
self.kw_only = kw_only
340+
self.slots = slots
341+
self.weakref_slot = weakref_slot
332342

333343
def __repr__(self):
334344
return ('_DataclassParams('
@@ -337,7 +347,11 @@ def __repr__(self):
337347
f'eq={self.eq!r},'
338348
f'order={self.order!r},'
339349
f'unsafe_hash={self.unsafe_hash!r},'
340-
f'frozen={self.frozen!r}'
350+
f'frozen={self.frozen!r},'
351+
f'match_args={self.match_args!r},'
352+
f'kw_only={self.kw_only!r},'
353+
f'slots={self.slots!r},'
354+
f'weakref_slot={self.weakref_slot!r}'
341355
')')
342356

343357

@@ -905,7 +919,9 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
905919
globals = {}
906920

907921
setattr(cls, _PARAMS, _DataclassParams(init, repr, eq, order,
908-
unsafe_hash, frozen))
922+
unsafe_hash, frozen,
923+
match_args, kw_only,
924+
slots, weakref_slot))
909925

910926
# Find our base classes in reverse MRO order, and exclude
911927
# ourselves. In reversed order so that more derived classes

Lib/test/test_dataclasses.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ def test_field_repr(self):
6868

6969
self.assertEqual(repr_output, expected_output)
7070

71+
def test_dataclass_params_repr(self):
72+
# Even though this is testing an internal implementation detail,
73+
# it's testing a feature we want to make sure is correctly implemented
74+
# for the sake of dataclasses itself
75+
@dataclass(slots=True, frozen=True)
76+
class Some: pass
77+
78+
repr_output = repr(Some.__dataclass_params__)
79+
expected_output = "_DataclassParams(init=True,repr=True," \
80+
"eq=True,order=False,unsafe_hash=False,frozen=True," \
81+
"match_args=True,kw_only=False," \
82+
"slots=True,weakref_slot=False)"
83+
self.assertEqual(repr_output, expected_output)
84+
85+
def test_dataclass_params_signature(self):
86+
# Even though this is testing an internal implementation detail,
87+
# it's testing a feature we want to make sure is correctly implemented
88+
# for the sake of dataclasses itself
89+
@dataclass
90+
class Some: pass
91+
92+
for param in inspect.signature(dataclass).parameters:
93+
if param == 'cls':
94+
continue
95+
self.assertTrue(hasattr(Some.__dataclass_params__, param), msg=param)
96+
7197
def test_named_init_params(self):
7298
@dataclass
7399
class C:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ``match_args``, ``kw_only``, ``slots``, and ``weakref_slot`` to
2+
``_DataclassParams``.

0 commit comments

Comments
 (0)