mirror of
https://github.com/python/cpython.git
synced 2025-09-20 15:40:32 +00:00
gh-102069: Fix __weakref__
descriptor generation for custom dataclasses (#102075)
This commit is contained in:
parent
71e37d9079
commit
d97757f793
3 changed files with 15 additions and 4 deletions
|
@ -1189,6 +1189,9 @@ def _add_slots(cls, is_frozen, weakref_slot):
|
||||||
# Remove __dict__ itself.
|
# Remove __dict__ itself.
|
||||||
cls_dict.pop('__dict__', None)
|
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.
|
# And finally create the class.
|
||||||
qualname = getattr(cls, '__qualname__', None)
|
qualname = getattr(cls, '__qualname__', None)
|
||||||
cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
|
cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
|
||||||
|
|
|
@ -3175,6 +3175,8 @@ class TestSlots(unittest.TestCase):
|
||||||
with self.assertRaisesRegex(TypeError,
|
with self.assertRaisesRegex(TypeError,
|
||||||
"cannot create weak reference"):
|
"cannot create weak reference"):
|
||||||
weakref.ref(a)
|
weakref.ref(a)
|
||||||
|
with self.assertRaises(AttributeError):
|
||||||
|
a.__weakref__
|
||||||
|
|
||||||
def test_slots_weakref(self):
|
def test_slots_weakref(self):
|
||||||
@dataclass(slots=True, weakref_slot=True)
|
@dataclass(slots=True, weakref_slot=True)
|
||||||
|
@ -3183,7 +3185,9 @@ class TestSlots(unittest.TestCase):
|
||||||
|
|
||||||
self.assertIn("__weakref__", A.__slots__)
|
self.assertIn("__weakref__", A.__slots__)
|
||||||
a = A(1)
|
a = A(1)
|
||||||
weakref.ref(a)
|
a_ref = weakref.ref(a)
|
||||||
|
|
||||||
|
self.assertIs(a.__weakref__, a_ref)
|
||||||
|
|
||||||
def test_slots_weakref_base_str(self):
|
def test_slots_weakref_base_str(self):
|
||||||
class Base:
|
class Base:
|
||||||
|
@ -3249,7 +3253,8 @@ class TestSlots(unittest.TestCase):
|
||||||
self.assertIn("__weakref__", Base.__slots__)
|
self.assertIn("__weakref__", Base.__slots__)
|
||||||
self.assertNotIn("__weakref__", A.__slots__)
|
self.assertNotIn("__weakref__", A.__slots__)
|
||||||
a = A(1)
|
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):
|
def test_weakref_slot_subclass_no_weakref_slot(self):
|
||||||
@dataclass(slots=True, weakref_slot=True)
|
@dataclass(slots=True, weakref_slot=True)
|
||||||
|
@ -3265,7 +3270,8 @@ class TestSlots(unittest.TestCase):
|
||||||
self.assertIn("__weakref__", Base.__slots__)
|
self.assertIn("__weakref__", Base.__slots__)
|
||||||
self.assertNotIn("__weakref__", A.__slots__)
|
self.assertNotIn("__weakref__", A.__slots__)
|
||||||
a = A(1)
|
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):
|
def test_weakref_slot_normal_base_weakref_slot(self):
|
||||||
class Base:
|
class Base:
|
||||||
|
@ -3280,7 +3286,8 @@ class TestSlots(unittest.TestCase):
|
||||||
self.assertIn("__weakref__", Base.__slots__)
|
self.assertIn("__weakref__", Base.__slots__)
|
||||||
self.assertNotIn("__weakref__", A.__slots__)
|
self.assertNotIn("__weakref__", A.__slots__)
|
||||||
a = A(1)
|
a = A(1)
|
||||||
weakref.ref(a)
|
a_ref = weakref.ref(a)
|
||||||
|
self.assertIs(a.__weakref__, a_ref)
|
||||||
|
|
||||||
|
|
||||||
class TestDescriptors(unittest.TestCase):
|
class TestDescriptors(unittest.TestCase):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix ``__weakref__`` descriptor generation for custom dataclasses.
|
Loading…
Add table
Add a link
Reference in a new issue