-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
check-classvar.test
266 lines (239 loc) · 6.21 KB
/
check-classvar.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
[case testAssignmentOnClass]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
A.x = 2
[case testAssignmentOnInstance]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
A().x = 2
[out]
main:4: error: Cannot assign to class variable "x" via instance
[case testAssignmentOnSubclassInstance]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
class B(A):
pass
B().x = 2
[out]
main:6: error: Cannot assign to class variable "x" via instance
[case testOverrideOnSelf]
from typing import ClassVar
class A:
x = None # type: ClassVar[int]
def __init__(self) -> None:
self.x = 0
[out]
main:5: error: Cannot assign to class variable "x" via instance
[case testOverrideOnSelfInSubclass]
from typing import ClassVar
class A:
x = None # type: ClassVar[int]
class B(A):
def __init__(self) -> None:
self.x = 0
[out]
main:6: error: Cannot assign to class variable "x" via instance
[case testReadingFromInstance]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
A().x
reveal_type(A().x)
[out]
main:5: error: Revealed type is 'builtins.int'
[case testReadingFromSelf]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
def __init__(self) -> None:
reveal_type(self.x)
[out]
main:5: error: Revealed type is 'builtins.int'
[case testTypecheckSimple]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
y = A.x # type: int
[case testTypecheckWithUserType]
from typing import ClassVar
class A:
pass
class B:
x = A() # type: ClassVar[A]
[case testTypeCheckOnAssignment]
from typing import ClassVar
class A:
pass
class B:
pass
class C:
x = None # type: ClassVar[A]
C.x = B()
[out]
main:8: error: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testTypeCheckWithOverridden]
from typing import ClassVar
class A:
pass
class B(A):
pass
class C:
x = A() # type: ClassVar[A]
C.x = B()
[case testRevealType]
from typing import ClassVar
class A:
x = None # type: ClassVar[int]
reveal_type(A.x)
[out]
main:4: error: Revealed type is 'builtins.int'
[case testInfer]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
y = A.x
reveal_type(y)
[out]
main:5: error: Revealed type is 'builtins.int'
[case testAssignmentOnUnion]
from typing import ClassVar, Union
class A:
x = None # type: int
class B:
x = None # type: ClassVar[int]
c = A() # type: Union[A, B]
c.x = 1
[out]
main:7: error: Cannot assign to class variable "x" via instance
[case testAssignmentOnInstanceFromType]
from typing import ClassVar, Type
class A:
x = None # type: ClassVar[int]
def f(a: Type[A]) -> None:
a().x = 0
[out]
main:5: error: Cannot assign to class variable "x" via instance
[case testAssignmentOnInstanceFromSubclassType]
from typing import ClassVar, Type
class A:
x = None # type: ClassVar[int]
class B(A):
pass
def f(b: Type[B]) -> None:
b().x = 0
[out]
main:7: error: Cannot assign to class variable "x" via instance
[case testClassVarWithList]
from typing import ClassVar, List
class A:
x = None # type: ClassVar[List[int]]
A.x = ['a']
A().x.append(1)
A().x.append('')
[builtins fixtures/list.pyi]
[out]
main:4: error: List item 0 has incompatible type "str"; expected "int"
main:6: error: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
[case testClassVarWithUnion]
from typing import ClassVar, Union
class A:
x = None # type: ClassVar[Union[int, str]]
class B:
pass
A.x = 0
A.x = 'a'
A.x = B()
reveal_type(A().x)
[out]
main:8: error: Incompatible types in assignment (expression has type "B", variable has type "Union[int, str]")
main:9: error: Revealed type is 'Union[builtins.int, builtins.str]'
[case testOverrideWithNarrowedUnion]
from typing import ClassVar, Union
class A: pass
class B: pass
class C: pass
class D:
x = None # type: ClassVar[Union[A, B, C]]
class E(D):
x = None # type: ClassVar[Union[A, B]]
[case testOverrideWithExtendedUnion]
from typing import ClassVar, Union
class A: pass
class B: pass
class C: pass
class D:
x = None # type: ClassVar[Union[A, B]]
class E(D):
x = None # type: ClassVar[Union[A, B, C]]
[out]
main:8: error: Incompatible types in assignment (expression has type "Union[A, B, C]", base class "D" defined the type as "Union[A, B]")
[case testAssignmentToCallableRet]
from typing import ClassVar
class A:
x = None # type: ClassVar[int]
def f() -> A:
return A()
f().x = 0
[out]
main:6: error: Cannot assign to class variable "x" via instance
[case testOverrideWithIncomatibleType]
from typing import ClassVar
class A:
x = None # type: ClassVar[int]
class B(A):
x = None # type: ClassVar[str]
[out]
main:5: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "int")
[case testOverrideWithNormalAttribute]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
class B(A):
x = 2 # type: int
[out]
main:5: error: Cannot override class variable (previously declared on base class "A") with instance variable
[case testOverrideWithAttributeWithClassVar]
from typing import ClassVar
class A:
x = 1 # type: int
class B(A):
x = 2 # type: ClassVar[int]
[out]
main:5: error: Cannot override instance variable (previously declared on base class "A") with class variable
[case testOverrideClassVarManyBases]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
class B:
x = 2 # type: int
class C(A, B):
x = 3 # type: ClassVar[int]
[out]
main:7: error: Cannot override instance variable (previously declared on base class "B") with class variable
[case testOverrideClassVarWithClassVar]
from typing import ClassVar
class A:
x = 1 # type: ClassVar[int]
class B(A):
x = 2 # type: ClassVar[int]
[case testOverrideOnABCSubclass]
from abc import ABCMeta
from typing import ClassVar
class A(metaclass=ABCMeta):
x = None # type: ClassVar[int]
class B(A):
x = 0 # type: ClassVar[int]
[case testAcrossModules]
import m
reveal_type(m.A().x)
m.A().x = 0
[file m.py]
from typing import ClassVar
class A:
x = None # type: ClassVar[int]
[out]
main:2: error: Revealed type is 'builtins.int'
main:3: error: Cannot assign to class variable "x" via instance