]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-102069: Fix `__weakref__` descriptor generation for custom dataclasses...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 13 Mar 2023 22:17:46 +0000 (15:17 -0700)
committerGitHub <noreply@github.com>
Mon, 13 Mar 2023 22:17:46 +0000 (16:17 -0600)
gh-102069: Fix `__weakref__` descriptor generation for custom dataclasses (GH-102075)
(cherry picked from commit d97757f793ea53dda3cc6882b4a92d3e921b17c9)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/dataclasses.py
Lib/test/test_dataclasses.py
Misc/NEWS.d/next/Library/2023-02-20-16-47-56.gh-issue-102069.FS7f1j.rst [new file with mode: 0644]

index 2bfeea515b65c46e5974c42e0a606d25e14a03f6..d2af451efed5065960f9e758b164e35d9c6eeae7 100644 (file)
@@ -1175,6 +1175,9 @@ def _add_slots(cls, is_frozen, weakref_slot):
     # Remove __dict__ itself.
     cls_dict.pop('__dict__', None)
 
+    # Clear existing `__weakref__` descriptor, it belongs to a previous type:
+    cls_dict.pop('__weakref__', None)  # gh-102069
+
     # And finally create the class.
     qualname = getattr(cls, '__qualname__', None)
     cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
index 9af43771fac06ef657f83b226d8f96ac7f63a23a..3a247776e5f42c49810abd1a902a0fd13e1a1f28 100644 (file)
@@ -3076,6 +3076,8 @@ class TestSlots(unittest.TestCase):
         with self.assertRaisesRegex(TypeError,
                                     "cannot create weak reference"):
             weakref.ref(a)
+        with self.assertRaises(AttributeError):
+            a.__weakref__
 
     def test_slots_weakref(self):
         @dataclass(slots=True, weakref_slot=True)
@@ -3084,7 +3086,9 @@ class TestSlots(unittest.TestCase):
 
         self.assertIn("__weakref__", A.__slots__)
         a = A(1)
-        weakref.ref(a)
+        a_ref = weakref.ref(a)
+
+        self.assertIs(a.__weakref__, a_ref)
 
     def test_slots_weakref_base_str(self):
         class Base:
@@ -3150,7 +3154,8 @@ class TestSlots(unittest.TestCase):
         self.assertIn("__weakref__", Base.__slots__)
         self.assertNotIn("__weakref__", A.__slots__)
         a = A(1)
-        weakref.ref(a)
+        a_ref = weakref.ref(a)
+        self.assertIs(a.__weakref__, a_ref)
 
     def test_weakref_slot_subclass_no_weakref_slot(self):
         @dataclass(slots=True, weakref_slot=True)
@@ -3166,7 +3171,8 @@ class TestSlots(unittest.TestCase):
         self.assertIn("__weakref__", Base.__slots__)
         self.assertNotIn("__weakref__", A.__slots__)
         a = A(1)
-        weakref.ref(a)
+        a_ref = weakref.ref(a)
+        self.assertIs(a.__weakref__, a_ref)
 
     def test_weakref_slot_normal_base_weakref_slot(self):
         class Base:
@@ -3181,7 +3187,8 @@ class TestSlots(unittest.TestCase):
         self.assertIn("__weakref__", Base.__slots__)
         self.assertNotIn("__weakref__", A.__slots__)
         a = A(1)
-        weakref.ref(a)
+        a_ref = weakref.ref(a)
+        self.assertIs(a.__weakref__, a_ref)
 
 
 class TestDescriptors(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Library/2023-02-20-16-47-56.gh-issue-102069.FS7f1j.rst b/Misc/NEWS.d/next/Library/2023-02-20-16-47-56.gh-issue-102069.FS7f1j.rst
new file mode 100644 (file)
index 0000000..04c87e5
--- /dev/null
@@ -0,0 +1 @@
+Fix ``__weakref__`` descriptor generation for custom dataclasses.