@@ -3411,8 +3411,114 @@ class A:
3411
3411
class B (A ):
3412
3412
pass
3413
3413
3414
+ self .assertEqual (B .__slots__ , ())
3414
3415
B ()
3415
3416
3417
+ def test_dataclass_derived_generic (self ):
3418
+ T = typing .TypeVar ('T' )
3419
+
3420
+ @dataclass (slots = True , weakref_slot = True )
3421
+ class A (typing .Generic [T ]):
3422
+ pass
3423
+ self .assertEqual (A .__slots__ , ('__weakref__' ,))
3424
+ self .assertTrue (A .__weakref__ )
3425
+ A ()
3426
+
3427
+ @dataclass (slots = True , weakref_slot = True )
3428
+ class B [T2 ]:
3429
+ pass
3430
+ self .assertEqual (B .__slots__ , ('__weakref__' ,))
3431
+ self .assertTrue (B .__weakref__ )
3432
+ B ()
3433
+
3434
+ def test_dataclass_derived_generic_from_base (self ):
3435
+ T = typing .TypeVar ('T' )
3436
+
3437
+ class RawBase : ...
3438
+
3439
+ @dataclass (slots = True , weakref_slot = True )
3440
+ class C1 (typing .Generic [T ], RawBase ):
3441
+ pass
3442
+ self .assertEqual (C1 .__slots__ , ())
3443
+ self .assertTrue (C1 .__weakref__ )
3444
+ C1 ()
3445
+ @dataclass (slots = True , weakref_slot = True )
3446
+ class C2 (RawBase , typing .Generic [T ]):
3447
+ pass
3448
+ self .assertEqual (C2 .__slots__ , ())
3449
+ self .assertTrue (C2 .__weakref__ )
3450
+ C2 ()
3451
+
3452
+ @dataclass (slots = True , weakref_slot = True )
3453
+ class D [T2 ](RawBase ):
3454
+ pass
3455
+ self .assertEqual (D .__slots__ , ())
3456
+ self .assertTrue (D .__weakref__ )
3457
+ D ()
3458
+
3459
+ def test_dataclass_derived_generic_from_slotted_base (self ):
3460
+ T = typing .TypeVar ('T' )
3461
+
3462
+ class WithSlots :
3463
+ __slots__ = ('a' , 'b' )
3464
+
3465
+ @dataclass (slots = True , weakref_slot = True )
3466
+ class E1 (WithSlots , Generic [T ]):
3467
+ pass
3468
+ self .assertEqual (E1 .__slots__ , ('__weakref__' ,))
3469
+ self .assertTrue (E1 .__weakref__ )
3470
+ E1 ()
3471
+ @dataclass (slots = True , weakref_slot = True )
3472
+ class E2 (Generic [T ], WithSlots ):
3473
+ pass
3474
+ self .assertEqual (E2 .__slots__ , ('__weakref__' ,))
3475
+ self .assertTrue (E2 .__weakref__ )
3476
+ E2 ()
3477
+
3478
+ @dataclass (slots = True , weakref_slot = True )
3479
+ class F [T2 ](WithSlots ):
3480
+ pass
3481
+ self .assertEqual (F .__slots__ , ('__weakref__' ,))
3482
+ self .assertTrue (F .__weakref__ )
3483
+ F ()
3484
+
3485
+ def test_dataclass_derived_generic_from_slotted_base (self ):
3486
+ T = typing .TypeVar ('T' )
3487
+
3488
+ class WithWeakrefSlot :
3489
+ __slots__ = ('__weakref__' ,)
3490
+
3491
+ @dataclass (slots = True , weakref_slot = True )
3492
+ class G1 (WithWeakrefSlot , Generic [T ]):
3493
+ pass
3494
+ self .assertEqual (G1 .__slots__ , ())
3495
+ self .assertTrue (G1 .__weakref__ )
3496
+ G1 ()
3497
+ @dataclass (slots = True , weakref_slot = True )
3498
+ class G2 (Generic [T ], WithWeakrefSlot ):
3499
+ pass
3500
+ self .assertEqual (G2 .__slots__ , ())
3501
+ self .assertTrue (G2 .__weakref__ )
3502
+ G2 ()
3503
+
3504
+ @dataclass (slots = True , weakref_slot = True )
3505
+ class H [T2 ](WithWeakrefSlot ):
3506
+ pass
3507
+ self .assertEqual (H .__slots__ , ())
3508
+ self .assertTrue (H .__weakref__ )
3509
+ H ()
3510
+
3511
+ def test_dataclass_slot_dict (self ):
3512
+ class WithDictSlot :
3513
+ __slots__ = ('__dict__' ,)
3514
+
3515
+ @dataclass (slots = True )
3516
+ class A (WithDictSlot ): ...
3517
+
3518
+ self .assertEqual (A .__slots__ , ())
3519
+ self .assertEqual (A ().__dict__ , {})
3520
+ A ()
3521
+
3416
3522
3417
3523
class TestDescriptors (unittest .TestCase ):
3418
3524
def test_set_name (self ):
0 commit comments