Debian Codegraph @

test-data/unit/check-classvar.test

raw
1 [case testAssignmentOnClass]
2 from typing import ClassVar
3 class A:
4 x = 1 # type: ClassVar[int]
5 A.x = 2
6
7 [case testAssignmentOnInstance]
8 from typing import ClassVar
9 class A:
10 x = 1 # type: ClassVar[int]
11 A().x = 2
12 [out]
13 main:4: error: Cannot assign to class variable "x" via instance
14
15 [case testAssignmentOnSubclassInstance]
16 from typing import ClassVar
17 class A:
18 x = 1 # type: ClassVar[int]
19 class B(A):
20 pass
21 B().x = 2
22 [out]
23 main:6: error: Cannot assign to class variable "x" via instance
24
25 [case testOverrideOnSelf]
26 from typing import ClassVar
27 class A:
28 x: ClassVar[int]
29 def __init__(self) -> None:
30 self.x = 0
31 [out]
32 main:5: error: Cannot assign to class variable "x" via instance
33
34 [case testOverrideOnSelfInSubclass]
35 from typing import ClassVar
36 class A:
37 x: ClassVar[int]
38 class B(A):
39 def __init__(self) -> None:
40 self.x = 0
41 [out]
42 main:6: error: Cannot assign to class variable "x" via instance
43
44 [case testReadingFromInstance]
45 from typing import ClassVar
46 class A:
47 x = 1 # type: ClassVar[int]
48 A().x
49 reveal_type(A().x)
50 [out]
51 main:5: note: Revealed type is "builtins.int"
52
53 [case testReadingFromSelf]
54 from typing import ClassVar
55 class A:
56 x = 1 # type: ClassVar[int]
57 def __init__(self) -> None:
58 reveal_type(self.x)
59 [out]
60 main:5: note: Revealed type is "builtins.int"
61
62 [case testTypecheckSimple]
63 from typing import ClassVar
64 class A:
65 x = 1 # type: ClassVar[int]
66 y = A.x # type: int
67
68 [case testTypecheckWithUserType]
69 from typing import ClassVar
70 class A:
71 pass
72 class B:
73 x = A() # type: ClassVar[A]
74
75 [case testTypeCheckOnAssignment]
76 from typing import ClassVar
77 class A:
78 pass
79 class B:
80 pass
81 class C:
82 x: ClassVar[A]
83 C.x = B()
84 [out]
85 main:8: error: Incompatible types in assignment (expression has type "B", variable has type "A")
86
87 [case testTypeCheckWithOverridden]
88 from typing import ClassVar
89 class A:
90 pass
91 class B(A):
92 pass
93 class C:
94 x = A() # type: ClassVar[A]
95 C.x = B()
96
97 [case testRevealType]
98 from typing import ClassVar
99 class A:
100 x: ClassVar[int]
101 reveal_type(A.x)
102 [out]
103 main:4: note: Revealed type is "builtins.int"
104
105 [case testInfer]
106 from typing import ClassVar
107 class A:
108 x = 1 # type: ClassVar[int]
109 y = A.x
110 reveal_type(y)
111 [out]
112 main:5: note: Revealed type is "builtins.int"
113
114 [case testAssignmentOnUnion]
115 from typing import ClassVar, Union
116 class A:
117 x: int
118 class B:
119 x: ClassVar[int]
120 c = A() # type: Union[A, B]
121 c.x = 1
122 [out]
123 main:7: error: Cannot assign to class variable "x" via instance
124
125 [case testAssignmentOnInstanceFromType]
126 from typing import ClassVar, Type
127 class A:
128 x: ClassVar[int]
129 def f(a: Type[A]) -> None:
130 a().x = 0
131 [out]
132 main:5: error: Cannot assign to class variable "x" via instance
133
134 [case testAssignmentOnInstanceFromSubclassType]
135 from typing import ClassVar, Type
136 class A:
137 x: ClassVar[int]
138 class B(A):
139 pass
140 def f(b: Type[B]) -> None:
141 b().x = 0
142 [out]
143 main:7: error: Cannot assign to class variable "x" via instance
144
145 [case testClassVarWithList]
146 from typing import ClassVar, List
147 class A:
148 x: ClassVar[List[int]]
149 A.x = ['a']
150 A().x.append(1)
151 A().x.append('')
152 [builtins fixtures/list.pyi]
153 [out]
154 main:4: error: List item 0 has incompatible type "str"; expected "int"
155 main:6: error: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
156
157 [case testClassVarWithUnion]
158 from typing import ClassVar, Union
159 class A:
160 x: ClassVar[Union[int, str]]
161 class B:
162 pass
163 A.x = 0
164 A.x = 'a'
165 A.x = B()
166 reveal_type(A().x)
167 [out]
168 main:8: error: Incompatible types in assignment (expression has type "B", variable has type "int | str")
169 main:9: note: Revealed type is "builtins.int | builtins.str"
170
171 [case testOverrideWithNarrowedUnion]
172 from typing import ClassVar, Union
173 class A: pass
174 class B: pass
175 class C: pass
176 class D:
177 x: ClassVar[Union[A, B, C]]
178 class E(D):
179 x: ClassVar[Union[A, B]]
180
181 [case testOverrideWithExtendedUnion]
182 from typing import ClassVar, Union
183 class A: pass
184 class B: pass
185 class C: pass
186 class D:
187 x: ClassVar[Union[A, B]]
188 class E(D):
189 x: ClassVar[Union[A, B, C]]
190 [out]
191 main:8: error: Incompatible types in assignment (expression has type "A | B | C", base class "D" defined the type as "A | B")
192
193 [case testAssignmentToCallableRet]
194 from typing import ClassVar
195 class A:
196 x: ClassVar[int]
197 def f() -> A:
198 return A()
199 f().x = 0
200 [out]
201 main:6: error: Cannot assign to class variable "x" via instance
202
203 [case testOverrideWithIncompatibleType]
204 from typing import ClassVar
205 class A:
206 x: ClassVar[int]
207 class B(A):
208 x: ClassVar[str]
209 [out]
210 main:5: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "int")
211
212 [case testOverrideWithNormalAttribute]
213 from typing import ClassVar
214 class A:
215 x = 1 # type: ClassVar[int]
216 class B(A):
217 x = 2 # type: int
218 [out]
219 main:5: error: Cannot override class variable (previously declared on base class "A") with instance variable
220
221 [case testOverrideWithAttributeWithClassVar]
222 from typing import ClassVar
223 class A:
224 x = 1 # type: int
225 class B(A):
226 x = 2 # type: ClassVar[int]
227 [out]
228 main:5: error: Cannot override instance variable (previously declared on base class "A") with class variable
229
230 [case testOverrideClassVarManyBases]
231 from typing import ClassVar
232 class A:
233 x = 1 # type: ClassVar[int]
234 class B:
235 x = 2 # type: int
236 class C(A, B):
237 x = 3 # type: ClassVar[int]
238 [out]
239 main:7: error: Cannot override instance variable (previously declared on base class "B") with class variable
240
241 [case testOverrideClassVarWithClassVar]
242 from typing import ClassVar
243 class A:
244 x = 1 # type: ClassVar[int]
245 class B(A):
246 x = 2 # type: ClassVar[int]
247
248 [case testOverrideClassVarWithImplicitClassVar]
249 from typing import ClassVar
250 class A:
251 x = 1 # type: ClassVar[int]
252 class B(A):
253 x = 2
254
255 [case testOverrideClassVarWithImplicitThenExplicit]
256 from typing import ClassVar
257 class A:
258 x = 1 # type: ClassVar[int]
259 class B(A):
260 x = 2
261 class C(B):
262 x = 3 # type: ClassVar[int]
263
264 [case testOverrideOnABCSubclass]
265 from abc import ABCMeta
266 from typing import ClassVar
267 class A(metaclass=ABCMeta):
268 x: ClassVar[int]
269 class B(A):
270 x = 0 # type: ClassVar[int]
271
272 [case testAcrossModules]
273 import m
274 reveal_type(m.A().x)
275 m.A().x = 0
276 [file m.py]
277 from typing import ClassVar
278 class A:
279 x: ClassVar[int]
280 [out]
281 main:2: note: Revealed type is "builtins.int"
282 main:3: error: Cannot assign to class variable "x" via instance
283
284 [case testClassVarWithGeneric]
285 from typing import ClassVar, Generic, TypeVar
286 T = TypeVar('T')
287 class A(Generic[T]):
288 x: ClassVar[T] # Error reported at access site
289 @classmethod
290 def foo(cls) -> T:
291 return cls.x # OK
292
293 A.x # E: Access to generic class variables is ambiguous
294 A.x = 1 # E: Access to generic class variables is ambiguous
295 A[int].x # E: Access to generic class variables is ambiguous
296
297 class Bad(A[int]):
298 pass
299 Bad.x # E: Access to generic class variables is ambiguous
300
301 class Good(A[int]):
302 x = 42
303 reveal_type(Good.x) # N: Revealed type is "builtins.int"
304 [builtins fixtures/classmethod.pyi]
305
306 [case testClassVarWithNestedGeneric]
307 from typing import ClassVar, Generic, Tuple, TypeVar, Union, Type
308 T = TypeVar('T')
309 U = TypeVar('U')
310 class A(Generic[T, U]):
311 x: ClassVar[Union[T, Tuple[U, Type[U]]]] # Error reported at access site
312 @classmethod
313 def foo(cls) -> Union[T, Tuple[U, Type[U]]]:
314 return cls.x # OK
315
316 A.x # E: Access to generic class variables is ambiguous
317 A.x = 1 # E: Access to generic class variables is ambiguous
318 A[int, str].x # E: Access to generic class variables is ambiguous
319
320 class Bad(A[int, str]):
321 pass
322 reveal_type(Bad.x) # E: Access to generic class variables is ambiguous \
323 # N: Revealed type is "builtins.int | tuple[builtins.str, type[builtins.str]]"
324 reveal_type(Bad().x) # N: Revealed type is "builtins.int | tuple[builtins.str, type[builtins.str]]"
325
326 class Good(A[int, str]):
327 x = 42
328 reveal_type(Good.x) # N: Revealed type is "builtins.int"
329 [builtins fixtures/classmethod.pyi]
330
331 [case testSuggestClassVarOnTooFewArgumentsMethod]
332 from typing import Callable
333
334 class C:
335 foo: Callable[[C], int]
336 c:C
337 c.foo() # E: Too few arguments \
338 # N: "foo" is considered instance variable, to make it class variable use ClassVar[...]
339
340 [case testClassVarUnionBoundOnInstance]
341 from typing import Union, Callable, ClassVar
342
343 class C:
344 def f(self) -> int: ...
345 g: ClassVar[Union[Callable[[C], int], int]] = f
346
347 reveal_type(C().g) # N: Revealed type is "(def () -> builtins.int) | builtins.int"
348
349 [case testGenericSubclassAccessNoLeak]
350 from typing import ClassVar, Generic, TypeVar
351
352 T = TypeVar("T")
353 class B(Generic[T]):
354 x: T
355 y: ClassVar[T]
356
357 class C(B[T]): ...
358
359 reveal_type(C.x) # E: Access to generic instance variables via class is ambiguous \
360 # N: Revealed type is "Any"
361 reveal_type(C.y) # E: Access to generic class variables is ambiguous \
362 # N: Revealed type is "Any"
363
364 [case testClassVarBareAnnotation]
365 from typing import ClassVar
366
367 class C:
368 x: ClassVar = 1
369 y: ClassVar
370
371 reveal_type(C.x) # N: Revealed type is "builtins.int"
372 reveal_type(C().x) # N: Revealed type is "builtins.int"
373 reveal_type(C.y) # N: Revealed type is "Any"
374 reveal_type(C().y) # N: Revealed type is "Any"
375
376 [case testClassVarBareAnnotationDisabled]
377 # flags: --disallow-any-generics
378 from typing import ClassVar
379
380 class C:
381 x: ClassVar = 1
382 y: ClassVar # E: ClassVar without type argument becomes Any