[Python-checkins] gh-96142: add missing params to `dataclass._DataclassParams` (gh-96382)

ericvsmith webhook-mailer at python.org
Tue Oct 4 12:53:37 EDT 2022


https://github.com/python/cpython/commit/4f380db1a539bf7ae157d1e0791b5ac3fecfcf01
commit: 4f380db1a539bf7ae157d1e0791b5ac3fecfcf01
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: ericvsmith <ericvsmith at users.noreply.github.com>
date: 2022-10-04T09:53:28-07:00
summary:

gh-96142: add missing params to `dataclass._DataclassParams` (gh-96382)

files:
A Misc/NEWS.d/next/Library/2022-08-29-12-49-30.gh-issue-96142.PdCMez.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index efd83467bfa9..65fb8f251861 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -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('
@@ -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}'
                 ')')
 
 
@@ -905,7 +919,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
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index e2eab695789d..328dcdcb0bce 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -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:
diff --git a/Misc/NEWS.d/next/Library/2022-08-29-12-49-30.gh-issue-96142.PdCMez.rst b/Misc/NEWS.d/next/Library/2022-08-29-12-49-30.gh-issue-96142.PdCMez.rst
new file mode 100644
index 000000000000..43d1c3de9922
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-08-29-12-49-30.gh-issue-96142.PdCMez.rst
@@ -0,0 +1,2 @@
+Add ``match_args``, ``kw_only``, ``slots``, and ``weakref_slot`` to
+``_DataclassParams``.



More information about the Python-checkins mailing list