Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd97757f

Browse files
authored
gh-102069: Fix__weakref__ descriptor generation for custom dataclasses (#102075)
1 parent71e37d9 commitd97757f

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
@@ -1189,6 +1189,9 @@ def _add_slots(cls, is_frozen, weakref_slot):
11891189
# Remove __dict__ itself.
11901190
cls_dict.pop('__dict__',None)
11911191

1192+
# Clear existing `__weakref__` descriptor, it belongs to a previous type:
1193+
cls_dict.pop('__weakref__',None)# gh-102069
1194+
11921195
# And finally create the class.
11931196
qualname=getattr(cls,'__qualname__',None)
11941197
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
@@ -3175,6 +3175,8 @@ class A:
31753175
withself.assertRaisesRegex(TypeError,
31763176
"cannot create weak reference"):
31773177
weakref.ref(a)
3178+
withself.assertRaises(AttributeError):
3179+
a.__weakref__
31783180

31793181
deftest_slots_weakref(self):
31803182
@dataclass(slots=True,weakref_slot=True)
@@ -3183,7 +3185,9 @@ class A:
31833185

31843186
self.assertIn("__weakref__",A.__slots__)
31853187
a=A(1)
3186-
weakref.ref(a)
3188+
a_ref=weakref.ref(a)
3189+
3190+
self.assertIs(a.__weakref__,a_ref)
31873191

31883192
deftest_slots_weakref_base_str(self):
31893193
classBase:
@@ -3249,7 +3253,8 @@ class A(Base):
32493253
self.assertIn("__weakref__",Base.__slots__)
32503254
self.assertNotIn("__weakref__",A.__slots__)
32513255
a=A(1)
3252-
weakref.ref(a)
3256+
a_ref=weakref.ref(a)
3257+
self.assertIs(a.__weakref__,a_ref)
32533258

32543259
deftest_weakref_slot_subclass_no_weakref_slot(self):
32553260
@dataclass(slots=True,weakref_slot=True)
@@ -3265,7 +3270,8 @@ class A(Base):
32653270
self.assertIn("__weakref__",Base.__slots__)
32663271
self.assertNotIn("__weakref__",A.__slots__)
32673272
a=A(1)
3268-
weakref.ref(a)
3273+
a_ref=weakref.ref(a)
3274+
self.assertIs(a.__weakref__,a_ref)
32693275

32703276
deftest_weakref_slot_normal_base_weakref_slot(self):
32713277
classBase:
@@ -3280,7 +3286,8 @@ class A(Base):
32803286
self.assertIn("__weakref__",Base.__slots__)
32813287
self.assertNotIn("__weakref__",A.__slots__)
32823288
a=A(1)
3283-
weakref.ref(a)
3289+
a_ref=weakref.ref(a)
3290+
self.assertIs(a.__weakref__,a_ref)
32843291

32853292

32863293
classTestDescriptors(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)

[8]ページ先頭

©2009-2025 Movatter.jp