From a388296d47e8961d328264bd30c665c798ca9468 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 20 Feb 2023 16:48:26 +0300 Subject: [PATCH 1/2] gh-102069: Fix `__weakref__` descriptor generation for custom dataclasses --- Lib/dataclasses.py | 3 +++ Lib/test/test_dataclasses.py | 15 +++++++++++---- ...2023-02-20-16-47-56.gh-issue-102069.FS7f1j.rst | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-02-20-16-47-56.gh-issue-102069.FS7f1j.rst diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 5c0257eba186d1..7db31c4d7adfd3 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -1192,6 +1192,9 @@ def _add_slots(cls, is_frozen, weakref_slot): # Remove __dict__ itself. cls_dict.pop('__dict__', None) + # Clear exiting `__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) diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 81a36aa241acf7..bd1f3423e2c1cf 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -3119,6 +3119,8 @@ class A: 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) @@ -3127,7 +3129,9 @@ class A: 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: @@ -3193,7 +3197,8 @@ class A(Base): 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) @@ -3209,7 +3214,8 @@ class A(Base): 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: @@ -3224,7 +3230,8 @@ class A(Base): 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 index 00000000000000..04c87e515cca93 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-02-20-16-47-56.gh-issue-102069.FS7f1j.rst @@ -0,0 +1 @@ +Fix ``__weakref__`` descriptor generation for custom dataclasses. From 1d512e6f11d77f88c002e72b515712add2a88999 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Mon, 13 Mar 2023 15:01:24 -0600 Subject: [PATCH 2/2] Update Lib/dataclasses.py --- Lib/dataclasses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 7db31c4d7adfd3..66037b46591b8a 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -1192,7 +1192,7 @@ def _add_slots(cls, is_frozen, weakref_slot): # Remove __dict__ itself. cls_dict.pop('__dict__', None) - # Clear exiting `__weakref__` descriptor, it belongs to a previous type: + # Clear existing `__weakref__` descriptor, it belongs to a previous type: cls_dict.pop('__weakref__', None) # gh-102069 # And finally create the class.