@@ -46,6 +46,9 @@ from typing import ClassVar
46
46
class A:
47
47
x = 1 # type: ClassVar[int]
48
48
A().x
49
+ reveal_type(A().x)
50
+ [out]
51
+ main:5: error: Revealed type is 'builtins.int'
49
52
50
53
[case testTypecheckSimple]
51
54
from typing import ClassVar
@@ -72,6 +75,16 @@ C.x = B()
72
75
[out]
73
76
main:8: error: Incompatible types in assignment (expression has type "B", variable has type "A")
74
77
78
+ [case testTypeCheckWithOverridden]
79
+ from typing import ClassVar
80
+ class A:
81
+ pass
82
+ class B(A):
83
+ pass
84
+ class C:
85
+ x = A() # type: ClassVar[A]
86
+ C.x = B()
87
+
75
88
[case testRevealType]
76
89
from typing import ClassVar
77
90
class A:
@@ -120,21 +133,90 @@ def f(b: Type[B]) -> None:
120
133
[out]
121
134
main:7: error: Illegal assignment to class variable
122
135
123
- [case testAssignmentWithGeneric ]
136
+ [case testClassVarWithList ]
124
137
from typing import ClassVar, List
125
138
class A:
126
139
x = None # type: ClassVar[List[int]]
127
140
A.x = ['a']
141
+ A().x.append(1)
142
+ A().x.append('')
128
143
[builtins fixtures/list.pyi]
129
144
[out]
130
145
main:4: error: List item 0 has incompatible type "str"
146
+ main:6: error: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
147
+
148
+ [case testClassVarWithUnion]
149
+ from typing import ClassVar, Union
150
+ class A:
151
+ x = None # type: ClassVar[Union[int, str]]
152
+ class B:
153
+ pass
154
+ A.x = 0
155
+ A.x = 'a'
156
+ A.x = B()
157
+ reveal_type(A().x)
158
+ [out]
159
+ main:8: error: Incompatible types in assignment (expression has type "B", variable has type "Union[int, str]")
160
+ main:9: error: Revealed type is 'Union[builtins.int, builtins.str]'
161
+
162
+ [case testOverrideWithNarrowedUnion]
163
+ from typing import ClassVar, Union
164
+ class A: pass
165
+ class B: pass
166
+ class C: pass
167
+ class D:
168
+ x = None # type: ClassVar[Union[A, B, C]]
169
+ class E(D):
170
+ x = None # type: ClassVar[Union[A, B]]
171
+
172
+ [case testOverrideWithExtendedUnion]
173
+ from typing import ClassVar, Union
174
+ class A: pass
175
+ class B: pass
176
+ class C: pass
177
+ class D:
178
+ x = None # type: ClassVar[Union[A, B]]
179
+ class E(D):
180
+ x = None # type: ClassVar[Union[A, B, C]]
181
+ [out]
182
+ main:8: error: Incompatible types in assignment (expression has type "Union[A, B, C]", base class "D" defined the type as "Union[A, B]")
183
+
184
+ [case testClassVarWithGeneric]
185
+ from typing import ClassVar, Generic, TypeVar
186
+ T = TypeVar('T')
187
+ class A(Generic[T]):
188
+ x = None # type: ClassVar[T]
189
+ reveal_type(A[int]().x)
190
+ [out]
191
+ main:5: error: Revealed type is 'builtins.int*'
192
+
193
+ [case testClassVarWithGenericList]
194
+ from typing import ClassVar, Generic, List, TypeVar
195
+ T = TypeVar('T')
196
+ class A(Generic[T]):
197
+ x = None # type: ClassVar[List[T]]
198
+ reveal_type(A[int]().x)
199
+ A[int].x.append('a')
200
+ [builtins fixtures/list.pyi]
201
+ [out]
202
+ main:5: error: Revealed type is 'builtins.list[builtins.int*]'
203
+ main:6: error: Argument 1 to "append" of "list" has incompatible type "str"; expected "T"
131
204
132
205
[case testAssignmentToCallableRet]
133
- from typing import ClassVar, Type
206
+ from typing import ClassVar
134
207
class A:
135
208
x = None # type: ClassVar[int]
136
209
def f() -> A:
137
210
return A()
138
211
f().x = 0
139
212
[out]
140
213
main:6: error: Illegal assignment to class variable
214
+
215
+ [case testOverrideWithIncomatibleType]
216
+ from typing import ClassVar
217
+ class A:
218
+ x = None # type: ClassVar[int]
219
+ class B(A):
220
+ x = None # type: ClassVar[str]
221
+ [out]
222
+ main:5: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "int")
0 commit comments