← Back to index

dataclasses_slots.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 2
FP: 0
FN: 4
Optional: 0 / 0
1"""
2Tests the slots functionality of dataclass added in Python 3.10.
3"""
4
5# Specification: https://docs.python.org/3/library/dataclasses.html#module-contents
6
7from dataclasses import dataclass
8
9# This should generate an error because __slots__ is already defined.
10@dataclass(slots=True) # E[DC1]
Expected a ty diagnostic for this line (tag 'DC1')
11class DC1: # E[DC1]
Expected a ty diagnostic for this line (tag 'DC1')
12 x: int
14 __slots__ = ()
17@dataclass(slots=True)
18class DC2:
19 x: int
21 def __init__(self):
22 self.x = 3
24 # This should generate an error because "y" is not in slots.
25 self.y = 3 # E
Expected a ty diagnostic for this line
28@dataclass(slots=False)
29class DC3:
30 x: int
32 __slots__ = ("x",)
34 def __init__(self):
35 self.x = 3
37 # This should generate an error because "y" is not in slots.
38 self.y = 3 # E
Expected a ty diagnostic for this line
41@dataclass
42class DC4:
43 __slots__ = ("y", "x")
44 x: int
45 y: str
48DC4(1, "bar")
51@dataclass(slots=True)
52class DC5:
53 a: int
56DC5.__slots__
57DC5(1).__slots__
60@dataclass
61class DC6:
62 a: int
65# This should generate an error because __slots__ is not defined.
66DC6.__slots__ # E
[unresolved-attribute] Class `DC6` has no attribute `__slots__`
68# This should generate an error because __slots__ is not defined.
69DC6(1).__slots__ # E
[unresolved-attribute] Object of type `DC6` has no attribute `__slots__`