File: check-classvar.test

package info (click to toggle)
mypy 1.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,464 kB
  • sloc: python: 114,757; ansic: 13,343; cpp: 11,380; makefile: 254; sh: 31
file content (382 lines) | stat: -rw-r--r-- 9,740 bytes parent folder | download | duplicates (4)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
[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: note: 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: note: 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: note: 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: note: 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: note: 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 testOverrideWithIncompatibleType]
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 testOverrideClassVarWithImplicitClassVar]
from typing import ClassVar
class A:
    x = 1  # type: ClassVar[int]
class B(A):
    x = 2

[case testOverrideClassVarWithImplicitThenExplicit]
from typing import ClassVar
class A:
    x = 1  # type: ClassVar[int]
class B(A):
    x = 2
class C(B):
    x = 3  # 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: note: Revealed type is "builtins.int"
main:3: error: Cannot assign to class variable "x" via instance

[case testClassVarWithGeneric]
from typing import ClassVar, Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]):
    x: ClassVar[T]  # Error reported at access site
    @classmethod
    def foo(cls) -> T:
        return cls.x  # OK

A.x  # E: Access to generic class variables is ambiguous
A.x = 1  # E: Access to generic class variables is ambiguous
A[int].x  # E: Access to generic class variables is ambiguous

class Bad(A[int]):
    pass
Bad.x  # E: Access to generic class variables is ambiguous

class Good(A[int]):
    x = 42
reveal_type(Good.x)  # N: Revealed type is "builtins.int"
[builtins fixtures/classmethod.pyi]

[case testClassVarWithNestedGeneric]
from typing import ClassVar, Generic, Tuple, TypeVar, Union, Type
T = TypeVar('T')
U = TypeVar('U')
class A(Generic[T, U]):
    x: ClassVar[Union[T, Tuple[U, Type[U]]]]  # Error reported at access site
    @classmethod
    def foo(cls) -> Union[T, Tuple[U, Type[U]]]:
        return cls.x  # OK

A.x  # E: Access to generic class variables is ambiguous
A.x = 1  # E: Access to generic class variables is ambiguous
A[int, str].x  # E: Access to generic class variables is ambiguous

class Bad(A[int, str]):
    pass
reveal_type(Bad.x)  # E: Access to generic class variables is ambiguous \
                    # N: Revealed type is "Union[builtins.int, tuple[builtins.str, type[builtins.str]]]"
reveal_type(Bad().x)  # N: Revealed type is "Union[builtins.int, tuple[builtins.str, type[builtins.str]]]"

class Good(A[int, str]):
    x = 42
reveal_type(Good.x)  # N: Revealed type is "builtins.int"
[builtins fixtures/classmethod.pyi]

[case testSuggestClassVarOnTooFewArgumentsMethod]
from typing import Callable

class C:
    foo: Callable[[C], int]
c:C
c.foo()  # E: Too few arguments \
         # N: "foo" is considered instance variable, to make it class variable use ClassVar[...]

[case testClassVarUnionBoundOnInstance]
from typing import Union, Callable, ClassVar

class C:
    def f(self) -> int: ...
    g: ClassVar[Union[Callable[[C], int], int]] = f

reveal_type(C().g)  # N: Revealed type is "Union[def () -> builtins.int, builtins.int]"

[case testGenericSubclassAccessNoLeak]
from typing import ClassVar, Generic, TypeVar

T = TypeVar("T")
class B(Generic[T]):
    x: T
    y: ClassVar[T]

class C(B[T]): ...

reveal_type(C.x)  # E: Access to generic instance variables via class is ambiguous \
                  # N: Revealed type is "Any"
reveal_type(C.y)  # E: Access to generic class variables is ambiguous \
                  # N: Revealed type is "Any"

[case testClassVarBareAnnotation]
from typing import ClassVar

class C:
    x: ClassVar = 1
    y: ClassVar

reveal_type(C.x)  # N: Revealed type is "builtins.int"
reveal_type(C().x)  # N: Revealed type is "builtins.int"
reveal_type(C.y)  # N: Revealed type is "Any"
reveal_type(C().y)  # N: Revealed type is "Any"

[case testClassVarBareAnnotationDisabled]
# flags: --disallow-any-generics
from typing import ClassVar

class C:
    x: ClassVar = 1
    y: ClassVar  # E: ClassVar without type argument becomes Any