]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-105866: fix dataclass with slots=True, weakref_slot=True (GH-105870) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 19 Mar 2024 10:57:31 +0000 (11:57 +0100)
committerGitHub <noreply@github.com>
Tue, 19 Mar 2024 10:57:31 +0000 (11:57 +0100)
(cherry picked from commit a22d05f04c074dbb4f71e7837f54c0bb693db75d)

Co-authored-by: Aviel Boag <avboag@gmail.com>
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Co-authored-by: Carl Meyer <carl@oddbird.net>
Lib/dataclasses.py
Lib/test/test_dataclasses/__init__.py
Misc/NEWS.d/next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst [new file with mode: 0644]

index 3eacba840db426c5d68027adf883d230c1228393..12b2dfd145b26793ff62d9bd96b73cf4bd84d15d 100644 (file)
@@ -1168,8 +1168,10 @@ def _dataclass_setstate(self, state):
 
 def _get_slots(cls):
     match cls.__dict__.get('__slots__'):
+        # A class which does not define __slots__ at all is equivalent
+        # to a class defining __slots__ = ('__dict__', '__weakref__')
         case None:
-            return
+            yield from ('__dict__', '__weakref__')
         case str(slot):
             yield slot
         # Slots may be any iterable, but we cannot handle an iterator
index 639631cfb39e5fd062eee035af3766bafb4716a9..4e05506cf101f4a433fcf545821dd2c56694a29f 100644 (file)
@@ -3403,6 +3403,17 @@ class TestSlots(unittest.TestCase):
         self.assertIs(a.__weakref__, a_ref)
 
 
+    def test_dataclass_derived_weakref_slot(self):
+        class A:
+            pass
+
+        @dataclass(slots=True, weakref_slot=True)
+        class B(A):
+            pass
+
+        B()
+
+
 class TestDescriptors(unittest.TestCase):
     def test_set_name(self):
         # See bpo-33141.
diff --git a/Misc/NEWS.d/next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst b/Misc/NEWS.d/next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst
new file mode 100644 (file)
index 0000000..28eae12
--- /dev/null
@@ -0,0 +1 @@
+Fixed ``_get_slots`` bug which caused error when defining dataclasses with slots and a weakref_slot.