Skip to content

Commit

Permalink
pythongh-102069: Fix __weakref__ descriptor generation for custom d…
Browse files Browse the repository at this point in the history
…ataclasses (pythonGH-102075)

(cherry picked from commit d97757f)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
  • Loading branch information
sobolevn authored and miss-islington committed Mar 13, 2023
1 parent a4fcd06 commit ad32998
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3076,6 +3076,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)
Expand All @@ -3084,7 +3086,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:
Expand Down Expand Up @@ -3150,7 +3154,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)
Expand All @@ -3166,7 +3171,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:
Expand All @@ -3181,7 +3187,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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``__weakref__`` descriptor generation for custom dataclasses.

0 comments on commit ad32998

Please sign in to comment.