← Back to index

dataclasses_inheritance.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 0
FP: 0
FN: 2
Optional: 0 / 1
1"""
2Tests inheritance rules for dataclasses.
3"""
4
5# Specification: https://peps.python.org/pep-0557/#inheritance
6
7from dataclasses import dataclass
8from typing import Any, ClassVar
9
11@dataclass
12class DC1:
13 a: int
14 b: str = ""
17@dataclass
18class DC2(DC1):
19 b: str = ""
20 a: int = 1
23dc2_1 = DC2(1, "")
25dc2_2 = DC2()
28@dataclass
29class DC3:
30 x: float = 15.0
31 y: str = ""
34@dataclass
35class DC4(DC3):
36 z: tuple[int] = (10,)
37 x: float = 15
40dc4_1 = DC4(0.0, "", (1,))
43@dataclass
44class DC5:
45 # While this generates an error at runtime for the stdlib dataclass,
46 # other libraries that use dataclass_transform don't have similar
47 # restrictions. It is therefore not required that a type checker
48 # report an error here.
49 x: list[int] = [] # E?
52@dataclass
53class DC6:
54 x: int
55 y: ClassVar[int] = 1
58@dataclass
59class DC7(DC6):
60 # This should generate an error because a ClassVar cannot override
61 # an instance variable of the same name.
62 x: ClassVar[int] # E
Expected a ty diagnostic for this line
64 # This should generate an error because an instance variable cannot
65 # override a class variable of the same name.
66 y: int # E
Expected a ty diagnostic for this line