Skip to content

Commit 781245f

Browse files
committed
Add more tests
1 parent 15e1e85 commit 781245f

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

test-data/unit/check-classvar.test

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ from typing import ClassVar
4646
class A:
4747
x = 1 # type: ClassVar[int]
4848
A().x
49+
reveal_type(A().x)
50+
[out]
51+
main:5: error: Revealed type is 'builtins.int'
4952

5053
[case testTypecheckSimple]
5154
from typing import ClassVar
@@ -72,6 +75,16 @@ C.x = B()
7275
[out]
7376
main:8: error: Incompatible types in assignment (expression has type "B", variable has type "A")
7477

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+
7588
[case testRevealType]
7689
from typing import ClassVar
7790
class A:
@@ -120,21 +133,90 @@ def f(b: Type[B]) -> None:
120133
[out]
121134
main:7: error: Illegal assignment to class variable
122135

123-
[case testAssignmentWithGeneric]
136+
[case testClassVarWithList]
124137
from typing import ClassVar, List
125138
class A:
126139
x = None # type: ClassVar[List[int]]
127140
A.x = ['a']
141+
A().x.append(1)
142+
A().x.append('')
128143
[builtins fixtures/list.pyi]
129144
[out]
130145
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"
131204

132205
[case testAssignmentToCallableRet]
133-
from typing import ClassVar, Type
206+
from typing import ClassVar
134207
class A:
135208
x = None # type: ClassVar[int]
136209
def f() -> A:
137210
return A()
138211
f().x = 0
139212
[out]
140213
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")

test-data/unit/semanal-classvar.test

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@ def f() -> None:
2525
[out]
2626
main:3: error: Invalid ClassVar definition
2727

28+
[case testClassVarDefInMethod]
29+
from typing import ClassVar
30+
class A:
31+
def f(self) -> None:
32+
x = None # type: ClassVar
33+
[out]
34+
main:4: error: Invalid ClassVar definition
35+
2836
[case testClassVarTooManyArguments]
2937
from typing import ClassVar
3038
class A:
3139
x = 1 # type: ClassVar[int, str]
3240
[out]
33-
main:3: error: ClassVar[...] must have exactly one type argument
41+
main:3: error: ClassVar[...] must have at most one type argument
3442

3543
[case testClassVarWithoutArguments]
3644
from typing import ClassVar
@@ -67,9 +75,9 @@ main:5: error: Invalid class attribute definition (previously declared on base c
6775
[case testOverrideClassVarWithInstanceVariable]
6876
from typing import ClassVar
6977
class A:
70-
x = 1 # type: int
78+
x = 1 # type: ClassVar[int]
7179
class B(A):
72-
x = 2 # type: ClassVar[int]
80+
x = 2 # type: int
7381
[out]
7482
main:5: error: Invalid class attribute definition (previously declared on base class "A")
7583

0 commit comments

Comments
 (0)