Skip to content

Commit a7296c4

Browse files
authored
Update tests to prepare for variable redefinition with new type (#6095)
After the planned change, test cases no longer can rely on code like this generating an error, since the two assignments create two logically separate variables with independent types: ``` x = 0 f(x) x = '' # Currently an error, but likely not in the future ``` Instead, we use an if statement to prevent redefinition. This works since redefinition will only happen unconditionally: ``` x = 0 f(x) if int(): x = '' # Still an error ``` Even this may stop working in the future, and we may need to another major test case update then. If we usually use the `if int():` idiom, finding most things that need changing will be easy. In new test cases we should generally avoid using assignment statements such as the above to generate errors. Instead, we can use `reveal_type()` and calls to functions with incompatible argument types, for example.
1 parent 92b8f1c commit a7296c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2821
-1873
lines changed

test-data/unit/check-abstract.test

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ a = None # type: A
1515
b = None # type: B
1616
c = None # type: C
1717

18+
def f(): i, j, a, b, c # Prevent redefinition
19+
1820
j = c # E: Incompatible types in assignment (expression has type "C", variable has type "J")
1921
a = i # E: Incompatible types in assignment (expression has type "I", variable has type "A")
2022
a = j # E: Incompatible types in assignment (expression has type "J", variable has type "A")
@@ -46,6 +48,8 @@ j = None # type: J
4648
a = None # type: A
4749
o = None # type: object
4850

51+
def f(): i, j, a, o # Prevent redefinition
52+
4953
j = i # E: Incompatible types in assignment (expression has type "I", variable has type "J")
5054
a = i # E: Incompatible types in assignment (expression has type "I", variable has type "A")
5155
a = j # E: Incompatible types in assignment (expression has type "J", variable has type "A")
@@ -65,18 +69,21 @@ class J(I): pass
6569
class A(J): pass
6670

6771
[case testInheritingAbstractClassInSubclass]
68-
6972
from abc import abstractmethod, ABCMeta
7073

7174
i = None # type: I
7275
a = None # type: A
7376
b = None # type: B
7477

75-
i = a # E: Incompatible types in assignment (expression has type "A", variable has type "I")
76-
b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
78+
if int():
79+
i = a # E: Incompatible types in assignment (expression has type "A", variable has type "I")
80+
if int():
81+
b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
7782

78-
a = b
79-
i = b
83+
if int():
84+
a = b
85+
if int():
86+
i = b
8087

8188
class I(metaclass=ABCMeta):
8289
@abstractmethod
@@ -116,12 +123,17 @@ class B: pass
116123
i, a, b = None, None, None # type: (I, A, B)
117124
o = None # type: object
118125

119-
a = cast(I, o) # E: Incompatible types in assignment (expression has type "I", variable has type "A")
120-
b = cast(B, i) # Ok; a subclass of B might inherit I
121-
i = cast(I, b) # Ok; a subclass of B might inherit I
126+
if int():
127+
a = cast(I, o) # E: Incompatible types in assignment (expression has type "I", variable has type "A")
128+
if int():
129+
b = cast(B, i) # Ok; a subclass of B might inherit I
130+
if int():
131+
i = cast(I, b) # Ok; a subclass of B might inherit I
122132

123-
i = cast(I, o)
124-
i = cast(I, a)
133+
if int():
134+
i = cast(I, o)
135+
if int():
136+
i = cast(I, a)
125137

126138
[case testInstantiatingClassThatImplementsAbstractMethod]
127139
from abc import abstractmethod, ABCMeta
@@ -218,15 +230,21 @@ class C(B):
218230

219231
var: Type[A]
220232
var()
221-
var = A # E: Can only assign concrete classes to a variable of type "Type[A]"
222-
var = B # E: Can only assign concrete classes to a variable of type "Type[A]"
223-
var = C # OK
233+
if int():
234+
var = A # E: Can only assign concrete classes to a variable of type "Type[A]"
235+
if int():
236+
var = B # E: Can only assign concrete classes to a variable of type "Type[A]"
237+
if int():
238+
var = C # OK
224239

225240
var_old = None # type: Type[A] # Old syntax for variable annotations
226241
var_old()
227-
var_old = A # E: Can only assign concrete classes to a variable of type "Type[A]"
228-
var_old = B # E: Can only assign concrete classes to a variable of type "Type[A]"
229-
var_old = C # OK
242+
if int():
243+
var_old = A # E: Can only assign concrete classes to a variable of type "Type[A]"
244+
if int():
245+
var_old = B # E: Can only assign concrete classes to a variable of type "Type[A]"
246+
if int():
247+
var_old = C # OK
230248
[out]
231249

232250
[case testInstantiationAbstractsInTypeForClassMethods]
@@ -362,7 +380,6 @@ main:11: error: Return type of "h" incompatible with supertype "I"
362380

363381

364382
[case testAccessingAbstractMethod]
365-
366383
from abc import abstractmethod, ABCMeta
367384

368385
class I(metaclass=ABCMeta):
@@ -371,14 +388,16 @@ class I(metaclass=ABCMeta):
371388

372389
i, a, b = None, None, None # type: (I, int, str)
373390

374-
a = i.f(a) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
375-
b = i.f(b) # E: Argument 1 to "f" of "I" has incompatible type "str"; expected "int"
391+
if int():
392+
a = i.f(a) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
393+
if int():
394+
b = i.f(b) # E: Argument 1 to "f" of "I" has incompatible type "str"; expected "int"
376395
i.g() # E: "I" has no attribute "g"
377396

378-
b = i.f(a)
397+
if int():
398+
b = i.f(a)
379399

380400
[case testAccessingInheritedAbstractMethod]
381-
382401
from abc import abstractmethod, ABCMeta
383402

384403
class J(metaclass=ABCMeta):
@@ -388,8 +407,10 @@ class I(J): pass
388407

389408
i, a, b = None, None, None # type: (I, int, str)
390409

391-
a = i.f(1) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
392-
b = i.f(1)
410+
if int():
411+
a = i.f(1) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
412+
if int():
413+
b = i.f(1)
393414

394415

395416
-- Any (dynamic) types

test-data/unit/check-basic.test

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,43 @@
22
[out]
33

44
[case testAssignmentAndVarDef]
5-
65
a = None # type: A
76
b = None # type: B
8-
a = a
9-
a = b # Fail
7+
if int():
8+
a = a
9+
if int():
10+
a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
1011
class A: pass
1112
class B: pass
12-
[out]
13-
main:5: error: Incompatible types in assignment (expression has type "B", variable has type "A")
1413

1514
[case testConstructionAndAssignment]
16-
1715
x = None # type: A
1816
x = A()
19-
x = B()
17+
if int():
18+
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
2019
class A:
2120
def __init__(self): pass
2221
class B:
2322
def __init__(self): pass
24-
[out]
25-
main:4: error: Incompatible types in assignment (expression has type "B", variable has type "A")
2623

2724
[case testInheritInitFromObject]
28-
2925
x = None # type: A
30-
x = A()
31-
x = B()
26+
if int():
27+
x = A()
28+
if int():
29+
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
3230
class A(object): pass
3331
class B(object): pass
34-
[out]
35-
main:4: error: Incompatible types in assignment (expression has type "B", variable has type "A")
3632

3733
[case testImplicitInheritInitFromObject]
38-
3934
x = None # type: A
4035
o = None # type: object
41-
x = o # E: Incompatible types in assignment (expression has type "object", variable has type "A")
42-
x = A()
43-
o = x
36+
if int():
37+
x = o # E: Incompatible types in assignment (expression has type "object", variable has type "A")
38+
if int():
39+
x = A()
40+
if int():
41+
o = x
4442
class A: pass
4543
class B: pass
4644
[out]
@@ -71,8 +69,10 @@ main:3: error: Incompatible types in assignment (expression has type "A", variab
7169
[case testDeclaredVariableInParentheses]
7270

7371
(x) = None # type: int
74-
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
75-
x = 1
72+
if int():
73+
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
74+
if int():
75+
x = 1
7676

7777

7878
-- Simple functions and calling
@@ -122,40 +122,34 @@ main:4: error: Too many arguments for "f"
122122

123123

124124
[case testLocalVariables]
125-
126125
def f() -> None:
127126
x = None # type: A
128127
y = None # type: B
129-
x = x
130-
x = y # Fail
128+
if int():
129+
x = x
130+
x = y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
131131
class A: pass
132132
class B: pass
133-
[out]
134-
main:6: error: Incompatible types in assignment (expression has type "B", variable has type "A")
135133

136134
[case testLocalVariableScope]
137-
138135
def f() -> None:
139-
x = None # type: A
136+
x: A
140137
x = A()
141138
def g() -> None:
142-
x = None # type: B
143-
x = A() # Fail
139+
x: B
140+
x = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
144141
class A: pass
145142
class B: pass
146-
[out]
147-
main:7: error: Incompatible types in assignment (expression has type "A", variable has type "B")
148143

149144
[case testFunctionArguments]
150145
import typing
151146
def f(x: 'A', y: 'B') -> None:
152-
x = y # Fail
153-
x = x
154-
y = B()
147+
if int():
148+
x = y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
149+
x = x
150+
y = B()
155151
class A: pass
156152
class B: pass
157-
[out]
158-
main:3: error: Incompatible types in assignment (expression has type "B", variable has type "A")
159153

160154
[case testLocalVariableInitialization]
161155
import typing
@@ -245,29 +239,28 @@ a = __file__ # type: int # E: Incompatible types in assignment (expression has
245239
[case testLocalVariableShadowing]
246240

247241
a = None # type: A
248-
a = B() # Fail
249-
a = A()
242+
if int():
243+
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
244+
a = A()
250245
def f() -> None:
251246
a = None # type: B
252-
a = A() # Fail
253-
a = B()
254-
a = B() # Fail
247+
if int():
248+
a = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
249+
a = B()
250+
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
255251
a = A()
256252

257253
class A: pass
258254
class B: pass
259-
[out]
260-
main:3: error: Incompatible types in assignment (expression has type "B", variable has type "A")
261-
main:7: error: Incompatible types in assignment (expression has type "A", variable has type "B")
262-
main:9: error: Incompatible types in assignment (expression has type "B", variable has type "A")
263255

264256
[case testGlobalDefinedInBlockWithType]
265257

266258
class A: pass
267259
while A:
268260
a = None # type: A
269-
a = A()
270-
a = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
261+
if int():
262+
a = A()
263+
a = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
271264

272265

273266
-- # type: signatures

test-data/unit/check-bound.test

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@ T = TypeVar('T', bound=A)
1414
U = TypeVar('U')
1515
def f(x: T) -> T: pass
1616
def g(x: U) -> U:
17-
return f(x) # Fail
17+
return f(x) # E: Value of type variable "T" of "f" cannot be "U"
1818

1919
f(A())
2020
f(B())
21-
f(D()) # Fail
21+
f(D()) # E: Value of type variable "T" of "f" cannot be "D"
2222

2323
b = B()
24-
b = f(b)
25-
b = f(C()) # Fail
26-
[out]
27-
main:12: error: Value of type variable "T" of "f" cannot be "U"
28-
main:16: error: Value of type variable "T" of "f" cannot be "D"
29-
main:20: error: Incompatible types in assignment (expression has type "C", variable has type "B")
24+
if int():
25+
b = f(b)
26+
if int():
27+
b = f(C()) # E: Incompatible types in assignment (expression has type "C", variable has type "B")
3028

3129

3230
[case testBoundOnGenericClass]
@@ -200,6 +198,7 @@ def foo(x: int) -> int:
200198

201199
a = 1
202200
b = foo(a)
203-
b = 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
201+
if int():
202+
b = 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
204203
twice(a) # E: Value of type variable "T" of "twice" cannot be "int"
205204
[builtins fixtures/args.pyi]

0 commit comments

Comments
 (0)