-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathcheck-redefine.test
More file actions
616 lines (532 loc) · 16.4 KB
/
check-redefine.test
File metadata and controls
616 lines (532 loc) · 16.4 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
-- Test cases for the redefinition of variable with a different type.
-- Redefine local variable
-- -----------------------
[case testRedefineLocalWithDifferentType]
# flags: --allow-redefinition-old
def f() -> None:
x = 0
reveal_type(x) # N: Revealed type is "builtins.int"
x = ''
reveal_type(x) # N: Revealed type is "builtins.str"
[case testCannotConditionallyRedefineLocalWithDifferentType]
# flags: --allow-redefinition-old
def f() -> None:
y = 0
reveal_type(y) # N: Revealed type is "builtins.int"
if int():
y = '' \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(y) # N: Revealed type is "builtins.int"
[case testRedefineFunctionArg]
# flags: --allow-redefinition-old
def f(x: int) -> None:
g(x)
x = ''
reveal_type(x) # N: Revealed type is "builtins.str"
def g(x: int) -> None:
if int():
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
reveal_type(x) # N: Revealed type is "builtins.int"
[case testRedefineAnnotationOnly]
# flags: --allow-redefinition-old
def f() -> None:
x: int
x = '' \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
reveal_type(x) # N: Revealed type is "builtins.int"
def g() -> None:
x: int
x = 1
reveal_type(x) # N: Revealed type is "builtins.int"
x = ''
reveal_type(x) # N: Revealed type is "builtins.str"
[case testRedefineLocalUsingOldValue]
# flags: --allow-redefinition-old
from typing import TypeVar, Union
T = TypeVar('T')
def f(x: int) -> None:
x = g(x)
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
y = 1
y = g(y)
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str"
def g(x: T) -> Union[T, str]: pass
[case testRedefineLocalForLoopIndexVariable]
# flags: --allow-redefinition-old
from typing import Iterable
def f(a: Iterable[int], b: Iterable[str]) -> None:
for x in a:
x = '' \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
reveal_type(x) # N: Revealed type is "builtins.int"
for x in b:
x = 1 \
# E: Incompatible types in assignment (expression has type "int", variable has type "str")
reveal_type(x) # N: Revealed type is "builtins.str"
def g(a: Iterable[int]) -> None:
for x in a: pass
x = ''
def h(a: Iterable[int]) -> None:
x = ''
reveal_type(x) # N: Revealed type is "builtins.str"
for x in a: pass
[case testCannotRedefineLocalWithinTry]
# flags: --allow-redefinition-old
def g(): pass
def f() -> None:
try:
x = 0
x
g() # Might raise an exception
x = '' \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
except:
pass
reveal_type(x) # N: Revealed type is "builtins.int"
y = 0
y
y = ''
[case testRedefineLocalWithinTryClauses]
# flags: --allow-redefinition-old
def fn_str(_: str) -> int: ...
def fn_int(_: int) -> None: ...
def in_block() -> None:
try:
a = ""
a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int"
except:
b = ""
b = fn_str(b)
fn_int(b)
else:
c = ""
c = fn_str(c)
fn_int(c)
finally:
d = ""
d = fn_str(d)
fn_int(d)
reveal_type(a) # N: Revealed type is "builtins.str"
reveal_type(b) # N: Revealed type is "builtins.int"
reveal_type(c) # N: Revealed type is "builtins.int"
reveal_type(d) # N: Revealed type is "builtins.int"
def across_blocks() -> None:
try:
a = ""
except:
pass
else:
a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
reveal_type(a) # N: Revealed type is "builtins.str"
[case testRedefineLocalExceptVar]
# flags: --allow-redefinition-old
def fn_exc(_: Exception) -> str: ...
def exc_name() -> None:
try:
pass
except RuntimeError as e:
e = fn_exc(e)
[builtins fixtures/exception.pyi]
[case testRedefineNestedInTry]
# flags: --allow-redefinition-old
def fn_int(_: int) -> None: ...
try:
try:
...
finally:
a = ""
a = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int"
except:
pass
[case testRedefineLocalWithinWith]
# flags: --allow-redefinition-old
def f() -> None:
with g():
x = 0
x
g() # Might raise an exception, but we ignore this
x = ''
reveal_type(x) # N: Revealed type is "builtins.str"
y = 0
y
y = ''
def g(): pass
[case testCannotRedefineAcrossNestedFunction]
# flags: --allow-redefinition-old
def f() -> None:
x = 0
x
def g() -> None:
x
g()
x = '' \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
g()
y = 0
y
y = ''
[case testCannotRedefineAcrossNestedDecoratedFunction]
# flags: --allow-redefinition-old
def dec(f): return f
def f() -> None:
x = 0
x
@dec
def g() -> None:
x
g()
x = '' \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
g()
y = 0
y
y = ''
[case testCannotRedefineAcrossNestedOverloadedFunction]
# flags: --allow-redefinition-old
from typing import overload
def f() -> None:
x = 0
x
@overload
def g() -> None: pass
@overload
def g(x: int) -> None: pass
def g(x=0):
pass
g()
x = '' \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
g()
y = 0
y
y = ''
[case testRedefineLocalInMultipleAssignment]
# flags: --allow-redefinition-old
def f() -> None:
x, x = 1, ''
reveal_type(x) # N: Revealed type is "builtins.str"
x = object()
reveal_type(x) # N: Revealed type is "builtins.object"
def g() -> None:
x = 1
if 1:
x, x = '', 1 \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testRedefineUnderscore]
# flags: --allow-redefinition-old
def f() -> None:
_, _ = 1, ''
if 1:
_, _ = '', 1
# This is unintentional but probably fine. No one is going to read _ value.
reveal_type(_) # N: Revealed type is "builtins.int"
[case testRedefineWithBreakAndContinue]
# flags: --allow-redefinition-old
def f() -> None:
y = 0
y
while int():
z = 0
z
z = ''
x = 0
if int():
break
x = '' \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
reveal_type(x) # N: Revealed type is "builtins.int"
y = ''
def g() -> None:
y = 0
y
for a in h():
z = 0
z
z = ''
x = 0
if int():
continue
x = '' \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")
reveal_type(x) # N: Revealed type is "builtins.int"
y = ''
def h(): pass
[case testRedefineLocalAndNestedLoops]
# flags: --allow-redefinition-old
def f() -> None:
z = 0
z
while int():
x = 0
x
while int():
if 1:
y = 1
y
if int():
break
y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x = ''
z = ''
[case testCannotRedefineVarAsFunction]
# flags: --allow-redefinition-old
def f() -> None:
def x(): pass
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
reveal_type(x) # N: Revealed type is "def () -> Any"
y = 1
def y(): pass # E: Name "y" already defined on line 6
[case testCannotRedefineVarAsClass]
# flags: --allow-redefinition-old
def f() -> None:
class x: pass
x = 1 # E: Cannot assign to a type \
# E: Incompatible types in assignment (expression has type "int", variable has type "type[x]")
y = 1
class y: pass # E: Name "y" already defined on line 5
[case testRedefineVarAsTypeVar]
# flags: --allow-redefinition-old
from typing import TypeVar
def f() -> None:
x = TypeVar('x')
x = 1 # E: Invalid assignment target \
# E: Incompatible types in assignment (expression has type "int", variable has type "TypeVar")
reveal_type(x) # N: Revealed type is "typing.TypeVar"
y = 1
y = TypeVar('y') # E: Cannot redefine "y" as a type variable \
# E: Incompatible types in assignment (expression has type "TypeVar", variable has type "int")
def h(a: y) -> y: return a # E: Variable "y" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]
[case testCannotRedefineVarAsModule]
# flags: --allow-redefinition-old
def f() -> None:
import typing as m
m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type Module)
n = 1
import typing as n # E: Incompatible import of "n" (imported name has type Module, local name has type "int")
[builtins fixtures/module.pyi]
[typing fixtures/typing-full.pyi]
[case testRedefineLocalWithTypeAnnotation]
# flags: --allow-redefinition-old
def f() -> None:
x = 1
reveal_type(x) # N: Revealed type is "builtins.int"
x = '' # type: object
reveal_type(x) # N: Revealed type is "builtins.object"
def g() -> None:
x = 1
reveal_type(x) # N: Revealed type is "builtins.int"
x: object = ''
reveal_type(x) # N: Revealed type is "builtins.object"
def h() -> None:
x: int
x = 1
reveal_type(x) # N: Revealed type is "builtins.int"
x: object
x: object = '' # E: Name "x" already defined on line 16
def farg(x: int) -> None:
x: str = '' # E: Name "x" already defined on line 18
def farg2(x: int) -> None:
x: str = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[case testRedefineLocalWithTypeAnnotationSpecialCases]
# flags: --allow-redefinition-old
def f() -> None:
x: object
x = 1
if int():
x = ''
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int"
x = ''
reveal_type(x) # N: Revealed type is "builtins.str"
if int():
x = 2 \
# E: Incompatible types in assignment (expression has type "int", variable has type "str")
[case testCannotRedefineSelf]
# flags: --allow-redefinition-old
class A:
x = 0
def f(self) -> None:
reveal_type(self.x) # N: Revealed type is "builtins.int"
self = f()
self.y: str = ''
reveal_type(self.y) # N: Revealed type is "builtins.str"
def f() -> A: return A()
-- Redefine global variable
-- ------------------------
[case testRedefineGlobalWithDifferentType]
# flags: --allow-redefinition-old
import m
reveal_type(m.x) # N: Revealed type is "builtins.str"
[file m.py]
x = 0
reveal_type(x) # N: Revealed type is "builtins.int"
x = object()
reveal_type(x) # N: Revealed type is "builtins.object"
x = ''
reveal_type(x) # N: Revealed type is "builtins.str"
[case testRedefineGlobalForIndex]
# flags: --allow-redefinition-old
import m
reveal_type(m.x) # N: Revealed type is "builtins.str"
[file m.py]
from typing import Iterable
def f(): pass
it1: Iterable[int] = f()
it2: Iterable[str] = f()
for x in it1:
reveal_type(x) # N: Revealed type is "builtins.int"
for x in it2:
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(x) # N: Revealed type is "builtins.str"
[case testRedefineGlobalBasedOnPreviousValues]
# flags: --allow-redefinition-old
from typing import TypeVar, Iterable
T = TypeVar('T')
def f(x: T) -> Iterable[T]: pass
a = 0
a = f(a)
reveal_type(a) # N: Revealed type is "typing.Iterable[builtins.int]"
[case testRedefineGlobalWithSeparateDeclaration]
# flags: --allow-redefinition-old
x = ''
reveal_type(x) # N: Revealed type is "builtins.str"
x: int
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
reveal_type(x) # N: Revealed type is "builtins.int"
x: object
x = 1
reveal_type(x) # N: Revealed type is "builtins.int"
if int():
x = object()
[case testRedefineGlobalUsingForLoop]
# flags: --allow-redefinition-old
from typing import Iterable, TypeVar, Union
T = TypeVar('T')
def f(x: T) -> Iterable[Union[T, str]]: pass
x = 0
reveal_type(x) # N: Revealed type is "builtins.int"
for x in f(x):
pass
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
[case testNoRedefinitionIfOnlyInitialized]
# flags: --allow-redefinition-old --no-strict-optional
x = None # type: int
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int")
x # Reference to variable
x = ''
y = 0
y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testNoRedefinitionIfNoValueAssigned]
# flags: --allow-redefinition-old
x: int
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
reveal_type(x) # N: Revealed type is "builtins.int"
x: object
[case testNoRedefinitionIfExplicitlyDisallowed]
# flags: --disallow-redefinition-old
x = 0
x = 2
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
def f() -> None:
y = 0
y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
class C:
y = 0
y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
def g() -> None:
# _ is a special case
_ = 0
_ = ''
x, _ = 0, C()
[builtins fixtures/tuple.pyi]
[case testRedefineAsException]
# flags: --allow-redefinition-old
e = 1
reveal_type(e) # N: Revealed type is "builtins.int"
try:
pass
except Exception as e:
reveal_type(e) # N: Revealed type is "builtins.Exception"
e = ''
reveal_type(e) # N: Revealed type is "builtins.str"
[builtins fixtures/exception.pyi]
[case testRedefineUsingWithStatement]
# flags: --allow-redefinition-old
class A:
def __enter__(self) -> int: ...
def __exit__(self, x, y, z) -> None: ...
class B:
def __enter__(self) -> str: ...
def __exit__(self, x, y, z) -> None: ...
with A() as x:
reveal_type(x) # N: Revealed type is "builtins.int"
with B() as x:
x = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[case testRedefineModuleAsException]
import typing
try:
pass
except Exception as typing:
pass
[builtins fixtures/exception.pyi]
[typing fixtures/typing-full.pyi]
[case testRedefiningUnderscoreFunctionIsntAnError]
def _(arg):
pass
def _(arg):
pass
[case testTypeErrorsInUnderscoreFunctionsReported]
def _(arg: str):
x = arg + 1 # E: Unsupported left operand type for + ("str")
def _(arg: int) -> int:
return 'a' # E: Incompatible return value type (got "str", expected "int")
[case testFunctionStillTypeCheckedWhenAliasedAsUnderscoreDuringImport]
from a import f as _
_(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
reveal_type(_('a')) # N: Revealed type is "builtins.str"
[file a.py]
def f(arg: str) -> str:
return arg
[case testCallToFunctionStillTypeCheckedWhenAssignedToUnderscoreVariable]
from a import g
_ = g
_('a') # E: Argument 1 has incompatible type "str"; expected "int"
reveal_type(_(1)) # N: Revealed type is "builtins.int"
[file a.py]
def g(arg: int) -> int:
return arg
[case testRedefiningUnderscoreFunctionWithDecoratorWithUnderscoreFunctionsNextToEachOther]
def dec(f):
return f
@dec
def _(arg):
pass
@dec
def _(arg):
pass
[case testRedefiningUnderscoreFunctionWithDecoratorInDifferentPlaces]
def dec(f):
return f
def dec2(f):
return f
@dec
def _(arg):
pass
def f(arg):
pass
@dec2
def _(arg):
pass
[case testOverwritingImportedFunctionThatWasAliasedAsUnderscore]
from a import f as _
def _(arg: str) -> str: # E: Name "_" already defined (possibly by an import)
return arg
[file a.py]
def f(s: str) -> str:
return s