8000 [3.11] gh-102069: Fix `__weakref__` descriptor generation for custom … · python/cpython@b0e221c · GitHub
[go: up one dir, main page]

Skip to content

Commit b0e221c

Browse files
[3.11] gh-102069: Fix __weakref__ descriptor generation for custom dataclasses (GH-102075) (#102662)
gh-102069: Fix `__weakref__` descriptor generation for custom dataclasses (GH-102075) (cherry picked from commit d97757f) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent a4fcd06 commit b0e221c

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

Lib/dataclasses.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,9 @@ def _add_slots(cls, is_frozen, weakref_slot):
11751175
# Remove __dict__ itself.
11761176
cls_dict.pop('__dict__', None)
11771177

1178+
# Clear e 8000 xisting `__weakref__` descriptor, it belongs to a previous type:
1179+
cls_dict.pop('__weakref__', None) # gh-102069
1180+
11781181
# And finally create the class.
11791182
qualname = getattr(cls, '__qualname__', None)
11801183
cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)

Lib/test/test_dataclasses.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,6 +3076,8 @@ class A:
30763076
with self.assertRaisesRegex(TypeError,
30773077
"cannot create weak reference"):
30783078
weakref.ref(a)
3079+
with self.assertRaises(AttributeError):
3080+
a.__weakref__
30793081

30803082
def test_slots_weakref(self):
30813083
@dataclass(slots=True, weakref_slot=True)
@@ -3084,7 +3086,9 @@ class A:
30843086

30853087
self.assertIn("__weakref__", A.__slots__)
30863088
a = A(1)
3087-
weakref.ref(a)
3089+
a_ref = weakref.ref(a)
3090+
3091+
self.assertIs(a.__weakref__, a_ref)
30883092

30893093
def test_slots_weakref_base_str(self):
30903094
class Base:
@@ -3150,7 +3154,8 @@ class A(Base):
31503154
self.assertIn("__weakref__", Base.__slots__)
31513155
self.assertNotIn("__weakref__", A.__slots__)
31523156
a = A(1)
3153-
weakref.ref(a)
3157+
a_ref = weakref.ref(a)
3158+
self.assertIs(a.__weakref__, a_ref)
31543159

31553160
def test_weakref_slot_subclass_no_weakref_slot(self):
31563161
@dataclass(slots=True, weakref_slot=True)
@@ -3166,7 +3171,8 @@ class A(Base):
31663171
self.assertIn("__weakref__", Base.__slots__)
31673172
self.assertNotIn("__weakref__", A.__slots__)
31683173
a = A(1)
3169-
weakref.ref(a)
3174+
a_ref = weakref.ref(a)
3175+
self.assertIs(a.__weakref__, a_ref)
31703176

31713177
def test_weakref_slot_normal_base_weakref_slot(self):
31723178
class Base:
@@ -3181,7 +3187,8 @@ class A(Base):
31813187
self.assertIn("__weakref__", Base.__slots__)
31823188
self.assertNotIn("__weakref__", A.__slots__)
31833189
a = A(1)
3184-
weakref.ref(a)
3190+
a_ref = weakref.ref(a)
3191+
self.assertIs(a.__weakref__, a_ref)
31853192

31863193

31873194
class TestDescriptors(unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``__weakref__`` descriptor generation for custom dataclasses.

0 commit comments

Comments
 (0)
0