Skip to content

gh-96142: add missing params to dataclass._DataclassParams #96382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,25 @@ class _DataclassParams:
'order',
'unsafe_hash',
'frozen',
'match_args',
'kw_only',
'slots',
'weakref_slot',
)

def __init__(self, init, repr, eq, order, unsafe_hash, frozen):
def __init__(self,
init, repr, eq, order, unsafe_hash, frozen,
match_args, kw_only, slots, weakref_slot):
self.init = init
self.repr = repr
self.eq = eq
self.order = order
self.unsafe_hash = unsafe_hash
self.frozen = frozen
self.match_args = match_args
self.kw_only = kw_only
self.slots = slots
self.weakref_slot = weakref_slot

def __repr__(self):
return ('_DataclassParams('
Expand All @@ -337,7 +347,11 @@ def __repr__(self):
f'eq={self.eq!r},'
f'order={self.order!r},'
f'unsafe_hash={self.unsafe_hash!r},'
f'frozen={self.frozen!r}'
f'frozen={self.frozen!r},'
f'match_args={self.match_args!r},'
f'kw_only={self.kw_only!r},'
f'slots={self.slots!r},'
f'weakref_slot={self.weakref_slot!r}'
')')


Expand Down Expand Up @@ -901,7 +915,9 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
globals = {}

setattr(cls, _PARAMS, _DataclassParams(init, repr, eq, order,
unsafe_hash, frozen))
unsafe_hash, frozen,
match_args, kw_only,
slots, weakref_slot))

# Find our base classes in reverse MRO order, and exclude
# ourselves. In reversed order so that more derived classes
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ def test_field_repr(self):

self.assertEqual(repr_output, expected_output)

def test_dataclass_params_repr(self):
# Even though this is testing an internal implementation detail,
# it's testing a feature we want to make sure is correctly implemented
# for the sake of dataclasses itself
@dataclass(slots=True, frozen=True)
class Some: pass

repr_output = repr(Some.__dataclass_params__)
expected_output = "_DataclassParams(init=True,repr=True," \
"eq=True,order=False,unsafe_hash=False,frozen=True," \
"match_args=True,kw_only=False," \
"slots=True,weakref_slot=False)"
self.assertEqual(repr_output, expected_output)

def test_dataclass_params_signature(self):
# Even though this is testing an internal implementation detail,
# it's testing a feature we want to make sure is correctly implemented
# for the sake of dataclasses itself
@dataclass
class Some: pass

for param in inspect.signature(dataclass).parameters:
if param == 'cls':
continue
self.assertTrue(hasattr(Some.__dataclass_params__, param), msg=param)

def test_named_init_params(self):
@dataclass
class C:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ``match_args``, ``kw_only``, ``slots``, and ``weakref_slot`` to
``_DataclassParams``.