@@ -3109,6 +3109,54 @@ def test_weakref_slot_make_dataclass(self):
3109
3109
"weakref_slot is True but slots is False" ):
3110
3110
B = make_dataclass ('B' , [('a' , int ),], weakref_slot = True )
3111
3111
3112
+ def test_weakref_slot_subclass_weakref_slot (self ):
3113
+ @dataclass (slots = True , weakref_slot = True )
3114
+ class Base :
3115
+ field : int
3116
+
3117
+ # A *can* also specify weakref_slot=True if it wants to (gh-93521)
3118
+ @dataclass (slots = True , weakref_slot = True )
3119
+ class A (Base ):
3120
+ ...
3121
+
3122
+ # __weakref__ is in the base class, not A. But an instance of A
3123
+ # is still weakref-able.
3124
+ self .assertIn ("__weakref__" , Base .__slots__ )
3125
+ self .assertNotIn ("__weakref__" , A .__slots__ )
3126
+ a = A (1 )
3127
+ weakref .ref (a )
3128
+
3129
+ def test_weakref_slot_subclass_no_weakref_slot (self ):
3130
+ @dataclass (slots = True , weakref_slot = True )
3131
+ class Base :
3132
+ field : int
3133
+
3134
+ @dataclass (slots = True )
3135
+ class A (Base ):
3136
+ ...
3137
+
3138
+ # __weakref__ is in the base class, not A. Even though A doesn't
3139
+ # specify weakref_slot, it should still be weakref-able.
3140
+ self .assertIn ("__weakref__" , Base .__slots__ )
3141
+ self .assertNotIn ("__weakref__" , A .__slots__ )
3142
+ a = A (1 )
3143
+ weakref .ref (a )
3144
+
3145
+ def test_weakref_slot_normal_base_weakref_slot (self ):
3146
+ class Base :
3147
+ __slots__ = ('__weakref__' ,)
3148
+
3149
+ @dataclass (slots = True , weakref_slot = True )
3150
+ class A (Base ):
3151
+ field : int
3152
+
3153
+ # __weakref__ is in the base class, not A. But an instance of
3154
+ # A is still weakref-able.
3155
+ self .assertIn ("__weakref__" , Base .__slots__ )
3156
+ self .assertNotIn ("__weakref__" , A .__slots__ )
3157
+ a = A (1 )
3158
+ weakref .ref (a )
3159
+
3112
3160
3113
3161
class TestDescriptors (unittest .TestCase ):
3114
3162
def test_set_name (self ):
0 commit comments