← 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
"""
2
Tests
inheritance
rules
for
dataclasses
.
3
"""
4
5
# Specification: https://peps.python.org/pep-0557/#inheritance
6
7
from
dataclasses
import
dataclass
8
from
typing
import
Any
,
ClassVar
9
10
11
@dataclass
12
class
DC1
:
13
a
:
int
14
b
:
str
=
""
15
16
17
@dataclass
18
class
DC2
(
DC1
):
19
b
:
str
=
""
20
a
:
int
=
1
21
22
23
dc2_1
=
DC2
(
1
,
""
)
24
25
dc2_2
=
DC2
()
26
27
28
@dataclass
29
class
DC3
:
30
x
:
float
=
15.0
31
y
:
str
=
""
32
33
34
@dataclass
35
class
DC4
(
DC3
):
36
z
:
tuple
[
int
]
=
(
10
,)
37
x
:
float
=
15
38
39
40
dc4_1
=
DC4
(
0.0
,
""
,
(
1
,))
41
42
43
@dataclass
44
class
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?
50
51
52
@dataclass
53
class
DC6
:
54
x
:
int
55
y
:
ClassVar
[
int
]
=
1
56
57
58
@dataclass
59
class
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
63
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