File tree 4 files changed +42
-0
lines changed
4 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -893,6 +893,11 @@ def _add_attrs_magic_attribute(
893
893
894
894
895
895
def _add_slots (ctx : mypy .plugin .ClassDefContext , attributes : list [Attribute ]) -> None :
896
+ if any (p .slots is None for p in ctx .cls .info .mro [1 :- 1 ]):
897
+ # At least one type in mro (excluding `self` and `object`)
898
+ # does not have concrete `__slots__` defined. Ignoring.
899
+ return
900
+
896
901
# Unlike `@dataclasses.dataclass`, `__slots__` is rewritten here.
897
902
ctx .cls .info .slots = {attr .name for attr in attributes }
898
903
Original file line number Diff line number Diff line change @@ -443,6 +443,12 @@ def add_slots(
443
443
self ._cls ,
444
444
)
445
445
return
446
+
447
+ if any (p .slots is None for p in info .mro [1 :- 1 ]):
448
+ # At least one type in mro (excluding `self` and `object`)
449
+ # does not have concrete `__slots__` defined. Ignoring.
450
+ return
451
+
446
452
info .slots = generated_slots
447
453
448
454
# Now, insert `.__slots__` attribute to class namespace:
Original file line number Diff line number Diff line change @@ -1519,6 +1519,22 @@ class Some:
1519
1519
self.y = 1 # E: Trying to assign name "y" that is not in "__slots__" of type "__main__.Some"
1520
1520
[builtins fixtures/dataclasses.pyi]
1521
1521
1522
+ [case testDataclassWithSlotsDerivedFromNonSlot]
1523
+ # flags: --python-version 3.10
1524
+ from dataclasses import dataclass
1525
+
1526
+ class A:
1527
+ pass
1528
+
1529
+ @dataclass(slots=True)
1530
+ class B(A):
1531
+ x: int
1532
+
1533
+ def __post_init__(self) -> None:
1534
+ self.y = 42
1535
+
1536
+ [builtins fixtures/dataclasses.pyi]
1537
+
1522
1538
[case testDataclassWithSlotsConflict]
1523
1539
# flags: --python-version 3.10
1524
1540
from dataclasses import dataclass
Original file line number Diff line number Diff line change @@ -1677,6 +1677,21 @@ class C:
1677
1677
self.c = 2 # E: Trying to assign name "c" that is not in "__slots__" of type "__main__.C"
1678
1678
[builtins fixtures/plugin_attrs.pyi]
1679
1679
1680
+ [case testAttrsClassWithSlotsDerivedFromNonSlots]
1681
+ import attrs
1682
+
1683
+ class A:
1684
+ pass
1685
+
1686
+ @attrs.define(slots=True)
1687
+ class B(A):
1688
+ x: int
1689
+
1690
+ def __attrs_post_init__(self) -> None:
1691
+ self.y = 42
1692
+
1693
+ [builtins fixtures/plugin_attrs.pyi]
1694
+
1680
1695
[case testRuntimeSlotsAttr]
1681
1696
from attr import dataclass
1682
1697
You can’t perform that action at this time.
0 commit comments