# 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)
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)
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:
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)
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:
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):