Debian Codegraph @

test-data/unit/check-redefine2.test

raw
1 -- Test cases for the redefinition of variable with a different type (new version).
2
3 [case testNewRedefineLocalWithDifferentType]
4 # flags: --allow-redefinition
5 def f() -> None:
6 x = 0
7 reveal_type(x) # N: Revealed type is "builtins.int"
8 x = ''
9 reveal_type(x) # N: Revealed type is "builtins.str"
10
11 [case testNewRedefineConditionalLocalWithDifferentTypeAndFlagAlias]
12 # flags: --allow-redefinition-new
13 def f() -> None:
14 if int():
15 x = 0
16 reveal_type(x) # N: Revealed type is "builtins.int"
17 else:
18 x = ''
19 reveal_type(x) # N: Revealed type is "builtins.str"
20
21 [case testNewRedefineMergeConditionalLocal1]
22 # flags: --allow-redefinition
23 def f1() -> None:
24 if int():
25 x = 0
26 else:
27 x = ''
28 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
29
30 def f2() -> None:
31 if int():
32 x = 0
33 else:
34 x = None
35 reveal_type(x) # N: Revealed type is "builtins.int | None"
36
37 [case testNewRedefineMergeConditionalLocal2]
38 # flags: --allow-redefinition
39 def nested_ifs() -> None:
40 if int():
41 if int():
42 x = 0
43 else:
44 x = ''
45 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
46 else:
47 if int():
48 x = None
49 else:
50 x = b""
51 reveal_type(x) # N: Revealed type is "None | builtins.bytes"
52 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None | builtins.bytes"
53
54 [case testNewRedefineUninitializedCodePath1]
55 # flags: --allow-redefinition
56 def f1() -> None:
57 if int():
58 x = 0
59 reveal_type(x) # N: Revealed type is "builtins.int"
60 x = ""
61 reveal_type(x) # N: Revealed type is "builtins.str"
62
63 [case testNewRedefineUninitializedCodePath2]
64 # flags: --allow-redefinition
65 from typing import Union
66
67 def f1() -> None:
68 if int():
69 x: Union[int, str] = 0
70 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
71 x = ""
72 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
73
74 [case testNewRedefineUninitializedCodePath3]
75 # flags: --allow-redefinition
76 from typing import Union
77
78 def f1() -> None:
79 if int():
80 x = 0
81 elif int():
82 x = ""
83 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
84
85 [case testNewRedefineUninitializedCodePath4]
86 # flags: --allow-redefinition
87 from typing import Union
88
89 def f1() -> None:
90 if int():
91 x: Union[int, str] = 0
92 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
93
94 [case testNewRedefineUninitializedCodePath5]
95 # flags: --allow-redefinition
96 from typing import Union
97
98 def f1() -> None:
99 x = 0
100 if int():
101 x = ""
102 reveal_type(x) # N: Revealed type is "builtins.str"
103 x = None
104 reveal_type(x) # N: Revealed type is "builtins.int | None"
105
106 [case testNewRedefineUninitializedCodePath6]
107 # flags: --allow-redefinition
108 from typing import Union
109
110 x: Union[str, None]
111
112 def f1() -> None:
113 if x is not None:
114 reveal_type(x) # N: Revealed type is "builtins.str"
115 reveal_type(x) # N: Revealed type is "builtins.str | None"
116
117 [case testNewRedefineHasAttr]
118 # flags: --allow-redefinition
119 from typing import Union
120
121 def test(lst: list[object]) -> None:
122 for cls in lst:
123 if not hasattr(cls, "module"):
124 break
125 reveal_type(cls.module) # N: Revealed type is "Any"
126
127 other = object()
128 if not hasattr(other, "foo"):
129 return
130 reveal_type(other.foo) # N: Revealed type is "Any"
131
132 x = object()
133 if bool():
134 assert hasattr(x, "bar")
135 x.bar # OK
136 x.bar # E: "object" has no attribute "bar"
137
138 class C:
139 x: int
140
141 u: Union[C, object]
142 if hasattr(u, "x"):
143 # Ideally we should have Any | int here and below, but this is tricky
144 reveal_type(u.x) # N: Revealed type is "Any"
145
146 y = hasattr(u, "x") and u.x
147 reveal_type(y) # N: Revealed type is "Literal[False] | Any"
148 [builtins fixtures/isinstancelist.pyi]
149
150 [case testNewRedefineGlobalVariableSimple]
151 # flags: --allow-redefinition
152 if int():
153 x = 0
154 reveal_type(x) # N: Revealed type is "builtins.int"
155 else:
156 x = ""
157 reveal_type(x) # N: Revealed type is "builtins.str"
158 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
159
160 def f1() -> None:
161 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
162
163 def f2() -> None:
164 global x
165 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
166 x = 0
167 reveal_type(x) # N: Revealed type is "builtins.int"
168
169 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
170
171 [case testNewRedefineGlobalVariableNoneInit1]
172 # flags: --allow-redefinition
173 x = None
174
175 def f() -> None:
176 global x
177 reveal_type(x) # N: Revealed type is "None"
178 x = 1
179 reveal_type(x) # N: Revealed type is "builtins.int"
180
181 reveal_type(x) # N: Revealed type is "None | builtins.int"
182
183 [case testNewRedefineGlobalVariableNoneInit2]
184 # flags: --allow-redefinition
185 x = None
186
187 def deco(f): return f
188
189 @deco
190 def f() -> None:
191 global x
192 if int():
193 x = 1
194 reveal_type(x) # N: Revealed type is "None | builtins.int"
195
196 reveal_type(x) # N: Revealed type is "None | builtins.int"
197
198 [case testNewRedefineGlobalVariableNoneInit3]
199 # flags: --allow-redefinition
200 from typing import overload
201
202 x = None
203
204 class C:
205 @overload
206 def f(self) -> None: ...
207 @overload
208 def f(self, n: int) -> None: ...
209
210 def f(self, n: int = 0) -> None:
211 global x
212 x = n
213 a = [x]
214 x = None
215 reveal_type(x) # N: Revealed type is "None"
216
217 reveal_type(x) # N: Revealed type is "None | builtins.int"
218
219 [case testNewRedefineGlobalVariableNoneInit4]
220 # flags: --allow-redefinition
221 x = None
222
223 def f() -> None:
224 def nested() -> None:
225 global x
226 x = 1
227
228 nested()
229
230 reveal_type(x) # N: Revealed type is "None | builtins.int"
231
232 [case testNewRedefineGlobalVariableWithUnsupportedType]
233 # flags: --allow-redefinition
234 x = 1
235
236 def f() -> None:
237 global x
238 x = "a" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
239
240 reveal_type(x) # N: Revealed type is "builtins.int"
241
242 [case testNewRedefineParameterTypes]
243 # flags: --allow-redefinition
244 from typing import Optional
245
246 def f1(x: Optional[str] = None) -> None:
247 reveal_type(x) # N: Revealed type is "builtins.str | None"
248 if x is None:
249 x = ""
250 reveal_type(x) # N: Revealed type is "builtins.str"
251
252 def f2(*args: str, **kwargs: int) -> None:
253 reveal_type(args) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
254 reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
255
256 class C:
257 def m(self) -> None:
258 reveal_type(self) # N: Revealed type is "__main__.C"
259 [builtins fixtures/dict.pyi]
260
261
262 [case testNewRedefineClassBody]
263 # flags: --allow-redefinition
264 class C:
265 if int():
266 x = 0
267 reveal_type(x) # N: Revealed type is "builtins.int"
268 else:
269 x = ""
270 reveal_type(x) # N: Revealed type is "builtins.str"
271 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
272
273 reveal_type(C.x) # N: Revealed type is "builtins.int | builtins.str"
274
275 [case testNewRedefineNestedFunctionBasics]
276 # flags: --allow-redefinition
277 def f1() -> None:
278 if int():
279 x = 0
280 else:
281 x = ""
282
283 def nested() -> None:
284 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
285
286 def f2() -> None:
287 if int():
288 x = 0
289 else:
290 x = ""
291
292 def nested() -> None:
293 nonlocal x
294 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
295 x = 0
296 reveal_type(x) # N: Revealed type is "builtins.int"
297
298 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
299
300 [case testNewRedefineLambdaBasics]
301 # flags: --allow-redefinition
302 def f1() -> None:
303 x = 0
304 if int():
305 x = None
306 f = lambda: reveal_type(x) # N: Revealed type is "builtins.int | None"
307 reveal_type(f) # N: Revealed type is "def () -> builtins.int | None"
308 if x is None:
309 x = ""
310 f = lambda: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
311 reveal_type(f) # N: Revealed type is "def () -> builtins.int | builtins.str"
312
313 [case testNewRedefineAssignmentExpression]
314 # flags: --allow-redefinition
315 def f1() -> None:
316 if x := int():
317 reveal_type(x) # N: Revealed type is "builtins.int"
318 elif x := str():
319 reveal_type(x) # N: Revealed type is "builtins.str"
320 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
321
322 def f2() -> None:
323 if x := int():
324 reveal_type(x) # N: Revealed type is "builtins.int"
325 elif x := str():
326 reveal_type(x) # N: Revealed type is "builtins.str"
327 else:
328 pass
329 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
330
331 def f3() -> None:
332 if (x := int()) or (x := str()):
333 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
334 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
335
336 [case testNewRedefineOperatorAssignment]
337 # flags: --allow-redefinition
338 class D: pass
339 class C:
340 def __add__(self, x: C) -> D: ...
341
342 c = C()
343 if int():
344 c += C()
345 reveal_type(c) # N: Revealed type is "__main__.D"
346 reveal_type(c) # N: Revealed type is "__main__.C | __main__.D"
347
348 [case testNewRedefineImportFrom-xfail]
349 # flags: --allow-redefinition
350 if int():
351 from m import x
352 else:
353 # TODO: This could be useful to allow
354 from m import y as x # E: Incompatible import of "x" (imported name has type "str", local name has type "int")
355 reveal_type(x) # N: Revealed type is "builtins.int"
356
357 if int():
358 from m import y
359 else:
360 y = 1
361 reveal_type(y) # N: Revealed type is "Union[builtins.str, builtins.int]"
362
363 [file m.py]
364 x = 1
365 y = ""
366
367 [case testNewRedefineImport]
368 # flags: --allow-redefinition
369 if int():
370 import m
371 else:
372 import m2 as m # E: Name "m" already defined (by an import)
373 m.x
374 m.y # E: Module has no attribute "y"
375
376 [file m.py]
377 x = 1
378
379 [file m2.py]
380 y = ""
381 [builtins fixtures/module.pyi]
382
383 [case testNewRedefineOptionalTypesSimple]
384 # flags: --allow-redefinition
385 def f1() -> None:
386 x = None
387 if int():
388 x = ""
389 reveal_type(x) # N: Revealed type is "None | builtins.str"
390
391 def f2() -> None:
392 if int():
393 x = None
394 elif int():
395 x = ""
396 else:
397 x = 1
398 reveal_type(x) # N: Revealed type is "None | builtins.str | builtins.int"
399
400 def f3() -> None:
401 if int():
402 x = None
403 else:
404 x = ""
405 reveal_type(x) # N: Revealed type is "None | builtins.str"
406
407 def f4() -> None:
408 x = None
409 reveal_type(x) # N: Revealed type is "None"
410
411 y = None
412 if int():
413 y = 1
414 reveal_type(y) # N: Revealed type is "None | builtins.int"
415
416 if int():
417 z = None
418 elif int():
419 z = 1
420 else:
421 z = ""
422 reveal_type(z) # N: Revealed type is "None | builtins.int | builtins.str"
423
424 [case testNewRedefinePartialTypeForInstanceVariable]
425 # flags: --allow-redefinition
426 class C1:
427 def __init__(self) -> None:
428 self.x = None
429 if int():
430 self.x = 1
431 reveal_type(self.x) # N: Revealed type is "builtins.int"
432 reveal_type(self.x) # N: Revealed type is "builtins.int | None"
433
434 reveal_type(C1().x) # N: Revealed type is "builtins.int | None"
435
436 class C2:
437 def __init__(self) -> None:
438 self.x = []
439 for i in [1, 2]:
440 self.x.append(i)
441 reveal_type(self.x) # N: Revealed type is "builtins.list[builtins.int]"
442
443 reveal_type(C2().x) # N: Revealed type is "builtins.list[builtins.int]"
444
445 class C3:
446 def __init__(self) -> None:
447 self.x = None
448 if int():
449 self.x = 1
450 else:
451 self.x = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int | None")
452 reveal_type(self.x) # N: Revealed type is "builtins.int | None"
453
454 reveal_type(C3().x) # N: Revealed type is "builtins.int | None"
455
456 class C4:
457 def __init__(self) -> None:
458 self.x = []
459 if int():
460 self.x = [""]
461 reveal_type(self.x) # N: Revealed type is "builtins.list[builtins.str]"
462
463 reveal_type(C4().x) # N: Revealed type is "builtins.list[builtins.str]"
464
465 class C5:
466 def __init__(self) -> None:
467 if int():
468 self.x = None
469 return
470 self.x = [""]
471 reveal_type(self.x) # N: Revealed type is "builtins.list[builtins.str]"
472
473 reveal_type(C5().x) # N: Revealed type is "builtins.list[builtins.str] | None"
474 [builtins fixtures/list.pyi]
475
476 [case testNewRedefinePartialGenericTypes]
477 # flags: --allow-redefinition
478 def f1() -> None:
479 a = []
480 a.append(1)
481 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
482
483 def f2() -> None:
484 a = []
485 a.append(1)
486 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
487 a = [""]
488 reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
489
490 def f3() -> None:
491 a = []
492 a.append(1)
493 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
494 a = []
495 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
496
497 def f4() -> None:
498 a = []
499 a.append(1)
500 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
501 # Partial types are currently not supported on reassignment
502 a = []
503 a.append("x") # E: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
504 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
505
506 def f5() -> None:
507 if int():
508 a = []
509 a.append(1)
510 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
511 else:
512 b = [""]
513 a = b
514 reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
515 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]"
516
517 def f6() -> None:
518 a = []
519 a.append(1)
520 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
521 b = [""]
522 a = b
523 reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
524 [builtins fixtures/list.pyi]
525
526 [case testNewRedefineFinalLiteral]
527 # flags: --allow-redefinition
528 from typing import Final, Literal
529
530 x: Final = "foo"
531 reveal_type(x) # N: Revealed type is "Literal['foo']?"
532 a: Literal["foo"] = x
533
534 class B:
535 x: Final = "bar"
536 a: Literal["bar"] = x
537 reveal_type(B.x) # N: Revealed type is "Literal['bar']?"
538 [builtins fixtures/tuple.pyi]
539
540 [case testNewRedefineAnnotatedVariable]
541 # flags: --allow-redefinition
542 from typing import Optional
543
544 def f1() -> None:
545 x: int = 0
546 if int():
547 x = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
548 reveal_type(x) # N: Revealed type is "builtins.int"
549 reveal_type(x) # N: Revealed type is "builtins.int"
550
551 def f2(x: Optional[str]) -> None:
552 if x is not None:
553 reveal_type(x) # N: Revealed type is "builtins.str"
554 else:
555 x = ""
556 reveal_type(x) # N: Revealed type is "builtins.str"
557
558 def f3() -> None:
559 a: list[Optional[str]] = [""]
560 reveal_type(a) # N: Revealed type is "builtins.list[builtins.str | None]"
561 a = [""]
562 reveal_type(a) # N: Revealed type is "builtins.list[builtins.str | None]"
563
564 class C:
565 x: Optional[str]
566
567 def f(self) -> None:
568 if self.x is not None:
569 reveal_type(self.x) # N: Revealed type is "builtins.str"
570 else:
571 self.x = ""
572 reveal_type(self.x) # N: Revealed type is "builtins.str"
573
574 [case testNewRedefineAnyType1]
575 # flags: --allow-redefinition
576 def a(): pass
577
578 def f1() -> None:
579 if int():
580 x = ""
581 else:
582 x = a()
583 reveal_type(x) # N: Revealed type is "Any"
584 reveal_type(x) # N: Revealed type is "builtins.str | Any"
585 x = 1
586 reveal_type(x) # N: Revealed type is "builtins.int"
587
588 def f2() -> None:
589 if int():
590 x = a()
591 else:
592 x = ""
593 reveal_type(x) # N: Revealed type is "builtins.str"
594 reveal_type(x) # N: Revealed type is "Any | builtins.str"
595 x = 1
596 reveal_type(x) # N: Revealed type is "builtins.int"
597
598 def f3() -> None:
599 x = 1
600 x = a()
601 reveal_type(x) # N: Revealed type is "Any"
602 x = ""
603 reveal_type(x) # N: Revealed type is "builtins.str"
604
605 def f4() -> None:
606 x = a()
607 x = 1
608 reveal_type(x) # N: Revealed type is "builtins.int"
609 x = a()
610 reveal_type(x) # N: Revealed type is "Any"
611
612 def f5() -> None:
613 x = a()
614 if int():
615 x = 1
616 reveal_type(x) # N: Revealed type is "builtins.int"
617 elif int():
618 x = ""
619 reveal_type(x) # N: Revealed type is "builtins.str"
620 reveal_type(x) # N: Revealed type is "Any | builtins.int | builtins.str"
621
622 def f6() -> None:
623 x = a()
624 if int():
625 x = 1
626 else:
627 x = ""
628 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
629
630 def f7() -> None:
631 x: int
632 x = a()
633 reveal_type(x) # N: Revealed type is "builtins.int"
634
635 [case testNewRedefineAnyType2]
636 # flags: --allow-redefinition
637 from typing import Any
638
639 def f1() -> None:
640 x: Any
641 x = int()
642 reveal_type(x) # N: Revealed type is "Any"
643
644 def f2() -> None:
645 x: Any
646 if int():
647 x = 0
648 reveal_type(x) # N: Revealed type is "Any"
649 else:
650 x = ""
651 reveal_type(x) # N: Revealed type is "Any"
652 reveal_type(x) # N: Revealed type is "Any"
653
654 def f3(x) -> None:
655 if int():
656 x = 0
657 reveal_type(x) # N: Revealed type is "builtins.int"
658 reveal_type(x) # N: Revealed type is "Any | builtins.int"
659
660 [case tetNewRedefineDel]
661 # flags: --allow-redefinition
662 def f1() -> None:
663 x = ""
664 reveal_type(x) # N: Revealed type is "builtins.str"
665 del x
666 reveal_type(x) # N: Revealed type is "<Deleted 'x'>"
667 x = 0
668 reveal_type(x) # N: Revealed type is "builtins.int"
669
670 def f2() -> None:
671 if int():
672 x = 0
673 del x
674 else:
675 x = ""
676 reveal_type(x) # N: Revealed type is "builtins.str"
677
678 def f3() -> None:
679 if int():
680 x = 0
681 else:
682 x = ""
683 del x
684 reveal_type(x) # N: Revealed type is "builtins.int"
685
686 def f4() -> None:
687 while int():
688 if int():
689 x: int = 0
690 else:
691 del x
692 reveal_type(x) # N: Revealed type is "builtins.int"
693
694 def f5() -> None:
695 while int():
696 if int():
697 x = 0
698 else:
699 del x
700 continue
701 x = ""
702 reveal_type(x) # N: Revealed type is "builtins.str"
703
704 [case testNewRedefineWhileLoopSimple]
705 # flags: --allow-redefinition
706 def f() -> None:
707 while int():
708 x = ""
709 reveal_type(x) # N: Revealed type is "builtins.str"
710 x = 0
711 reveal_type(x) # N: Revealed type is "builtins.int"
712 reveal_type(x) # N: Revealed type is "builtins.int"
713 while int():
714 x = None
715 reveal_type(x) # N: Revealed type is "None"
716 x = b""
717 reveal_type(x) # N: Revealed type is "builtins.bytes"
718 reveal_type(x) # N: Revealed type is "builtins.int | builtins.bytes"
719 x = [1]
720 reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
721
722 [case testNewRedefineWhileLoopOptional]
723 # flags: --allow-redefinition
724 def f1() -> None:
725 x = None
726 while int():
727 if int():
728 x = ""
729 reveal_type(x) # N: Revealed type is "None | builtins.str"
730
731 def f2() -> None:
732 x = None
733 while int():
734 reveal_type(x) # N: Revealed type is "None | builtins.str"
735 if int():
736 x = ""
737 reveal_type(x) # N: Revealed type is "None | builtins.str"
738
739 [case testNewRedefineWhileLoopPartialType]
740 # flags: --allow-redefinition
741 def f1() -> None:
742 x = []
743 while int():
744 x.append(1)
745 reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
746 [builtins fixtures/list.pyi]
747
748 [case testNewRedefineWhileLoopComplex1]
749 # flags: --allow-redefinition
750
751 def f1() -> None:
752 while True:
753 try:
754 pass
755 except Exception as e:
756 continue
757 [builtins fixtures/exception.pyi]
758
759 [case testNewRedefineWhileLoopComplex2]
760 # flags: --allow-redefinition
761
762 class C:
763 def __enter__(self) -> str: ...
764 def __exit__(self, *args) -> str: ...
765
766 def f1() -> None:
767 while True:
768 with C() as x:
769 continue
770
771 def f2() -> None:
772 while True:
773 from m import y
774 if int():
775 continue
776
777 [file m.py]
778 y = ""
779 [builtins fixtures/tuple.pyi]
780
781 [case testNewRedefineReturn]
782 # flags: --allow-redefinition
783 def f1() -> None:
784 if int():
785 x = 0
786 return
787 else:
788 x = ""
789 reveal_type(x) # N: Revealed type is "builtins.str"
790
791 def f2() -> None:
792 if int():
793 x = ""
794 else:
795 x = 0
796 return
797 reveal_type(x) # N: Revealed type is "builtins.str"
798
799 [case testNewRedefineBreakAndContinue]
800 # flags: --allow-redefinition
801 def b() -> None:
802 while int():
803 x = ""
804 if int():
805 x = 1
806 break
807 reveal_type(x) # N: Revealed type is "builtins.str"
808 x = None
809 reveal_type(x) # N: Revealed type is "builtins.int | None"
810
811 def c() -> None:
812 x = 0
813 while int():
814 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None"
815 if int():
816 x = ""
817 continue
818 else:
819 x = None
820 reveal_type(x) # N: Revealed type is "None"
821 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None"
822
823 [case testNewRedefineUnderscore]
824 # flags: --allow-redefinition
825 def f() -> None:
826 if int():
827 _ = 0
828 reveal_type(_) # N: Revealed type is "builtins.int"
829 else:
830 _ = ""
831 reveal_type(_) # N: Revealed type is "builtins.str"
832 reveal_type(_) # N: Revealed type is "builtins.int | builtins.str"
833
834 [case testNewRedefineWithStatement]
835 # flags: --allow-redefinition
836 class C:
837 def __enter__(self) -> int: ...
838 def __exit__(self, x, y, z): ...
839 class D:
840 def __enter__(self) -> str: ...
841 def __exit__(self, x, y, z): ...
842
843 def f1() -> None:
844 with C() as x:
845 reveal_type(x) # N: Revealed type is "builtins.int"
846 with D() as x:
847 reveal_type(x) # N: Revealed type is "builtins.str"
848
849 def f2() -> None:
850 if int():
851 with C() as x:
852 reveal_type(x) # N: Revealed type is "builtins.int"
853 else:
854 with D() as x:
855 reveal_type(x) # N: Revealed type is "builtins.str"
856 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
857
858 [case testNewRedefineNoWideningDeferred]
859 # flags: --allow-redefinition
860 def deferred() -> None:
861 c: C
862 x = 1
863 if int():
864 x = c.x
865 reveal_type(x) # N: Revealed type is "builtins.int"
866
867 class C:
868 def __init__(self) -> None:
869 self.x = defer()
870
871 def defer() -> int: ...
872
873 [case testNewRedefineTryStatement]
874 # flags: --allow-redefinition
875 class E(Exception): pass
876
877 def g(): ...
878
879 def f1() -> None:
880 try:
881 x = 1
882 g()
883 x = ""
884 reveal_type(x) # N: Revealed type is "builtins.str"
885 except RuntimeError as e:
886 reveal_type(e) # N: Revealed type is "builtins.RuntimeError"
887 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
888 except E as e:
889 reveal_type(e) # N: Revealed type is "__main__.E"
890 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
891 reveal_type(e) # N: Revealed type is "<Deleted 'e'>"
892 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
893
894 def f2() -> None:
895 try:
896 x = 1
897 if int():
898 x = ""
899 return
900 except Exception:
901 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
902 return
903 reveal_type(x) # N: Revealed type is "builtins.int"
904
905 def f3() -> None:
906 try:
907 x = 1
908 if int():
909 x = ""
910 return
911 finally:
912 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
913 reveal_type(x) # N: Revealed type is "builtins.int"
914
915 def f4() -> None:
916 while int():
917 try:
918 x = 1
919 if int():
920 x = ""
921 break
922 if int():
923 while int():
924 if int():
925 x = None
926 break
927 finally:
928 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None"
929 reveal_type(x) # N: Revealed type is "builtins.int | None"
930 [builtins fixtures/exception.pyi]
931
932 [case testNewRedefineRaiseStatement]
933 # flags: --allow-redefinition
934 def f1() -> None:
935 if int():
936 x = ""
937 elif int():
938 x = None
939 raise Exception()
940 else:
941 x = 1
942 reveal_type(x) # N: Revealed type is "builtins.str | builtins.int"
943
944 def f2() -> None:
945 try:
946 x = 1
947 if int():
948 x = ""
949 raise Exception()
950 reveal_type(x) # N: Revealed type is "builtins.int"
951 except Exception:
952 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
953 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
954 [builtins fixtures/exception.pyi]
955
956
957 [case testNewRedefineMultipleAssignment]
958 # flags: --allow-redefinition
959 def f1() -> None:
960 x, y = 1, ""
961 reveal_type(x) # N: Revealed type is "builtins.int"
962 reveal_type(y) # N: Revealed type is "builtins.str"
963 x, y = None, 2
964 reveal_type(x) # N: Revealed type is "None"
965 reveal_type(y) # N: Revealed type is "builtins.int"
966
967 def f2() -> None:
968 if int():
969 x, y = 1, ""
970 reveal_type(x) # N: Revealed type is "builtins.int"
971 reveal_type(y) # N: Revealed type is "builtins.str"
972 else:
973 x, y = None, 2
974 reveal_type(x) # N: Revealed type is "None"
975 reveal_type(y) # N: Revealed type is "builtins.int"
976 reveal_type(x) # N: Revealed type is "builtins.int | None"
977 reveal_type(y) # N: Revealed type is "builtins.str | builtins.int"
978
979 [case testNewRedefineForLoopBasics]
980 # flags: --allow-redefinition
981 def f1() -> None:
982 for x in [1]:
983 reveal_type(x) # N: Revealed type is "builtins.int"
984 for x in [""]:
985 reveal_type(x) # N: Revealed type is "builtins.str"
986
987 def f2() -> None:
988 if int():
989 for x, y in [(1, "x")]:
990 reveal_type(x) # N: Revealed type is "builtins.int"
991 reveal_type(y) # N: Revealed type is "builtins.str"
992 else:
993 for x, y in [(None, 1)]:
994 reveal_type(x) # N: Revealed type is "None"
995 reveal_type(y) # N: Revealed type is "builtins.int"
996
997 reveal_type(x) # N: Revealed type is "builtins.int | None"
998 reveal_type(y) # N: Revealed type is "builtins.str | builtins.int"
999 [builtins fixtures/for.pyi]
1000
1001 [case testNewRedefineForLoop1]
1002 # flags: --allow-redefinition
1003 def l() -> list[int]:
1004 return []
1005
1006 def f1() -> None:
1007 x = ""
1008 for x in l():
1009 reveal_type(x) # N: Revealed type is "builtins.int"
1010 reveal_type(x) # N: Revealed type is "builtins.str | builtins.int"
1011
1012 def f2() -> None:
1013 for x in [1, 2]:
1014 x = [x]
1015 reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
1016
1017 def f3() -> None:
1018 for x in [1, 2]:
1019 if int():
1020 x = "x"
1021 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
1022 [builtins fixtures/for.pyi]
1023
1024 [case testNewRedefineForLoop2]
1025 # flags: --allow-redefinition
1026 from typing import Any
1027
1028 def f(a: Any) -> None:
1029 for d in a:
1030 if isinstance(d["x"], str):
1031 return
1032 [builtins fixtures/isinstance.pyi]
1033
1034 [case testNewRedefineForStatementIndexNarrowing]
1035 # flags: --allow-redefinition
1036 from typing import TypedDict
1037
1038 class X(TypedDict):
1039 hourly: int
1040 daily: int
1041
1042 x: X
1043 for a in ("hourly", "daily"):
1044 reveal_type(a) # N: Revealed type is "Literal['hourly']? | Literal['daily']?"
1045 reveal_type(x[a]) # N: Revealed type is "builtins.int"
1046 reveal_type(a.upper()) # N: Revealed type is "builtins.str"
1047 c = a
1048 reveal_type(c) # N: Revealed type is "builtins.str"
1049 a = "monthly"
1050 reveal_type(a) # N: Revealed type is "builtins.str"
1051 a = "yearly"
1052 reveal_type(a) # N: Revealed type is "builtins.str"
1053 a = 1
1054 reveal_type(a) # N: Revealed type is "builtins.int"
1055 reveal_type(a) # N: Revealed type is "builtins.int"
1056
1057 b: str
1058 for b in ("hourly", "daily"):
1059 reveal_type(b) # N: Revealed type is "builtins.str"
1060 reveal_type(b.upper()) # N: Revealed type is "builtins.str"
1061 [builtins fixtures/for.pyi]
1062 [typing fixtures/typing-full.pyi]
1063
1064 [case testNewRedefineForLoopIndexWidening]
1065 # flags: --allow-redefinition
1066
1067 def f1() -> None:
1068 for x in [1]:
1069 reveal_type(x) # N: Revealed type is "builtins.int"
1070 x = ""
1071 reveal_type(x) # N: Revealed type is "builtins.str"
1072 reveal_type(x) # N: Revealed type is "builtins.str"
1073
1074 def f2() -> None:
1075 for x in [1]:
1076 reveal_type(x) # N: Revealed type is "builtins.int"
1077 if int():
1078 break
1079 x = ""
1080 reveal_type(x) # N: Revealed type is "builtins.str"
1081 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
1082
1083 def f3() -> None:
1084 if int():
1085 for x in [1]:
1086 x = ""
1087 reveal_type(x) # N: Revealed type is "builtins.str"
1088
1089 [case testNewRedefineVariableAnnotatedInLoop]
1090 # flags: --allow-redefinition --enable-error-code=redundant-expr
1091 from typing import Optional
1092
1093 def f1() -> None:
1094 e: Optional[str] = None
1095 for x in ["a"]:
1096 if e is None and int():
1097 e = x
1098 continue
1099 elif e is not None and int():
1100 break
1101 reveal_type(e) # N: Revealed type is "builtins.str | None"
1102 reveal_type(e) # N: Revealed type is "builtins.str | None"
1103
1104 def f2(e: Optional[str]) -> None:
1105 for x in ["a"]:
1106 if e is None and int():
1107 e = x
1108 continue
1109 elif e is not None and int():
1110 break
1111 reveal_type(e) # N: Revealed type is "builtins.str | None"
1112 reveal_type(e) # N: Revealed type is "builtins.str | None"
1113
1114 [case testNewRedefineLoopAndPartialTypesSpecialCase]
1115 # flags: --allow-redefinition
1116 def f() -> list[str]:
1117 a = [] # type: ignore
1118 o = []
1119 for line in ["x"]:
1120 if int():
1121 continue
1122 if int():
1123 a = []
1124 if int():
1125 a.append(line)
1126 else:
1127 o.append(line)
1128 return o
1129 [builtins fixtures/list.pyi]
1130
1131 [case testNewRedefineFinalVariable]
1132 # flags: --allow-redefinition
1133 from typing import Final
1134
1135 x: Final = "foo"
1136 x = 1 # E: Cannot assign to final name "x" \
1137 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
1138
1139 class C:
1140 y: Final = "foo"
1141 y = 1 # E: Cannot assign to final name "y" \
1142 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
1143
1144 [case testNewRedefineEnableUsingComment]
1145 import a
1146 import b
1147
1148 [file a.py]
1149 # mypy: allow-redefinition
1150 if int():
1151 x = 0
1152 else:
1153 x = ""
1154 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
1155
1156 [file b.py]
1157 if int():
1158 x = 0
1159 else:
1160 x = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1161 reveal_type(x) # N: Revealed type is "builtins.int"
1162
1163 [case testNewRedefineWithoutLocalPartialTypes]
1164 import a
1165 import b
1166
1167 [file a.py]
1168 # mypy: local-partial-types, allow-redefinition
1169 x = 0
1170 if int():
1171 x = ""
1172
1173 [file b.py]
1174 # mypy: local-partial-types=false, allow-redefinition
1175 x = 0
1176 if int():
1177 x = ""
1178
1179 [out]
1180 tmp/b.py:1: error: --local-partial-types must be enabled if using --allow-redefinition
1181
1182 [case testNewRedefineNestedLoopInfiniteExpansion]
1183 # flags: --allow-redefinition
1184 def a(): ...
1185
1186 def f() -> None:
1187 while int():
1188 x = a()
1189
1190 while int():
1191 # Use tuple to make revealed type a bit more compact.
1192 x = (x,)
1193
1194 reveal_type(x) # N: Revealed type is "Any | tuple[Any | tuple[Any | tuple[Any | tuple[Any]]]]"
1195 [builtins fixtures/tuple.pyi]
1196
1197 [case testNewRedefinePartialNoneEmptyList]
1198 # flags: --allow-redefinition
1199 def func() -> None:
1200 l = None # E: Need type annotation for "l"
1201
1202 if int():
1203 l = []
1204 l.append(1)
1205 reveal_type(l) # N: Revealed type is "None | builtins.list[Any]"
1206 [builtins fixtures/list.pyi]
1207
1208 [case testNewRedefineLoopProgressiveWidening]
1209 # flags: --allow-redefinition
1210 def foo() -> None:
1211 while int():
1212 if int():
1213 x = 1
1214 elif isinstance(x, list):
1215 x = ""
1216 elif isinstance(x, str):
1217 x = None
1218 else:
1219 x = [1]
1220 reveal_type(x) # N: Revealed type is "builtins.int | builtins.list[builtins.int] | builtins.str | None"
1221 [builtins fixtures/isinstancelist.pyi]
1222
1223 [case testNewRedefineAnyOrEmptyUnpackInLoop]
1224 # flags: --allow-redefinition
1225 from typing import Any
1226
1227 def foo() -> None:
1228 while int():
1229 data: Any
1230 x, y = data or ([], [])
1231 # Requiring an annotation may be better, but we want to preserve old behaviour.
1232 reveal_type(x) # N: Revealed type is "Any | builtins.list[Never]"
1233 [builtins fixtures/isinstancelist.pyi]
1234
1235 [case testNewRedefineNarrowingSpecialCase]
1236 # flags: --allow-redefinition --warn-unreachable
1237 from typing import Any, Union
1238
1239 def get() -> Union[tuple[Any, Any], tuple[None, None]]: ...
1240
1241 def f() -> None:
1242 x, _ = get()
1243 reveal_type(x) # N: Revealed type is "Any | None"
1244 if x and int():
1245 reveal_type(x) # N: Revealed type is "Any"
1246 reveal_type(x) # N: Revealed type is "Any | None"
1247 if x and int():
1248 reveal_type(x) # N: Revealed type is "Any"
1249 [builtins fixtures/tuple.pyi]
1250
1251 [case testNewRedefinitionPartialTypeInLoopDeferred]
1252 # flags: --allow-redefinition
1253
1254 def f(self) -> None:
1255 x = []
1256 for i in [1, 2, 3]:
1257 C().x
1258 x.append(1)
1259 reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
1260
1261 class C:
1262 def __init__(self) -> None:
1263 self.x = defer()
1264
1265 def defer() -> int: ...
1266 [builtins fixtures/list.pyi]
1267
1268 [case testNewRedefineCorrectChangedOnPop]
1269 # flags: --allow-redefinition
1270 from typing import Optional
1271
1272 def foo() -> None:
1273 name: Optional[str] = None
1274 name = None
1275 for i in [1, 2, 3]:
1276 reveal_type(name) # N: Revealed type is "None | builtins.str"
1277 if bool():
1278 name = "a"
1279
1280 [case testNewRedefinePartialTypeForUnderscore]
1281 # flags: --allow-redefinition
1282
1283 def t() -> tuple[int]:
1284 return (42,)
1285
1286 def f1() -> None:
1287 # Underscore is slightly special to preserve backward compatibility
1288 x, *_ = t()
1289 reveal_type(x) # N: Revealed type is "builtins.int"
1290
1291 def f2() -> None:
1292 x, *y = t() # E: Need type annotation for "y" (hint: "y: list[<type>] = ...")
1293
1294 def f3() -> None:
1295 x, _ = 1, []
1296
1297 def f4() -> None:
1298 a, b = 1, [] # E: Need type annotation for "b" (hint: "b: list[<type>] = ...")
1299 [builtins fixtures/tuple.pyi]
1300
1301 [case testNewRedefineUseInferredTypedDictTypeForContext]
1302 # flags: --allow-redefinition
1303 from typing import TypedDict
1304
1305 class TD(TypedDict):
1306 x: int
1307
1308 def f() -> None:
1309 td = TD(x=1)
1310 if int():
1311 td = {"x": 5}
1312 reveal_type(td) # N: Revealed type is "TypedDict('__main__.TD', {'x': builtins.int})"
1313 [typing fixtures/typing-typeddict.pyi]
1314
1315 [case testNewRedefineEmptyGeneratorUsingUnderscore]
1316 # flags: --allow-redefinition
1317 def f() -> None:
1318 gen = (_ for _ in ())
1319 reveal_type(gen) # N: Revealed type is "typing.Generator[Any, None, None]"
1320 [builtins fixtures/tuple.pyi]
1321
1322 [case testNewRedefineCannotWidenImportedVariable]
1323 # flags: --allow-redefinition
1324 import a
1325 import b
1326 reveal_type(a.x) # N: Revealed type is "builtins.str"
1327
1328 [file a.py]
1329 from b import x
1330 if int():
1331 x = None # E: Incompatible types in assignment (expression has type "None", variable has type "str")
1332
1333 [file b.py]
1334 x = "a"
1335
1336 [case testNewRedefineCannotWidenGlobalOrClassVariableWithMemberRef]
1337 # flags: --allow-redefinition
1338 from typing import ClassVar
1339 import a
1340
1341 a.x = None # E: Incompatible types in assignment (expression has type "None", variable has type "str")
1342 reveal_type(a.x) # N: Revealed type is "builtins.str"
1343
1344 class C:
1345 x = ""
1346 y: ClassVar[str] = ""
1347
1348 C.x = None # E: Incompatible types in assignment (expression has type "None", variable has type "str")
1349 reveal_type(C.x) # N: Revealed type is "builtins.str"
1350 C.y = None # E: Incompatible types in assignment (expression has type "None", variable has type "str")
1351 reveal_type(C.y) # N: Revealed type is "builtins.str"
1352
1353 [file a.py]
1354 x = "a"
1355
1356 [case testNewRedefineWidenGlobalInInitModule]
1357 # flags: --allow-redefinition
1358 import pkg
1359
1360 [file pkg/__init__.py]
1361 x = 0
1362 if int():
1363 x = ""
1364 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
1365
1366 [case testNewRedefineNarrowingOnFirstAssignment]
1367 # flags: --allow-redefinition
1368 from typing import Any
1369
1370 li: list[int]
1371
1372 def test() -> None:
1373 x: list[Any] = li
1374 reveal_type(x) # N: Revealed type is "builtins.list[Any]"
1375
1376 if bool():
1377 y: list[Any] = li
1378 else:
1379 y = li
1380 reveal_type(y) # N: Revealed type is "builtins.list[Any]"
1381 [builtins fixtures/primitives.pyi]
1382
1383 [case testNewRedefineAnyNoSpecialCasing]
1384 # flags: --allow-redefinition
1385 from typing import Any
1386
1387 def test() -> None:
1388 a: Any
1389 x = 1
1390 if bool():
1391 x = a
1392 reveal_type(x) # N: Revealed type is "builtins.int | Any"
1393
1394 if bool():
1395 y = a
1396 else:
1397 y = 1
1398 reveal_type(y) # N: Revealed type is "Any | builtins.int"
1399
1400 [case testNewRedefineSequentialRedefinitionToEmpty]
1401 # flags: --allow-redefinition
1402 items = ["a", "b", "c"]
1403 if bool():
1404 items = ["a", "b", "c"]
1405 elif bool():
1406 items = [item.split() for item in items]
1407 else:
1408 items = [] # OK
1409 reveal_type(items) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.list[builtins.str]]"
1410 [builtins fixtures/primitives.pyi]
1411
1412 [case testNewRedefineSequentialRedefinitionToEmpty2]
1413 # flags: --allow-redefinition
1414 items = ["a", "b", "c"]
1415 reveal_type(items) # N: Revealed type is "builtins.list[builtins.str]"
1416 items = [item.split() for item in items]
1417 reveal_type(items) # N: Revealed type is "builtins.list[builtins.list[builtins.str]]"
1418 items = []
1419 # This is consistent with old behavior (see below), but we may want to change this.
1420 reveal_type(items) # N: Revealed type is "builtins.list[Never]"
1421 [builtins fixtures/primitives.pyi]
1422
1423 [case testNarrowFromUnionToEmpyListOld]
1424 from typing import Union
1425 x: Union[list[str], list[int]]
1426 x = []
1427 reveal_type(x) # N: Revealed type is "builtins.list[Never]"
1428
1429 [case testNewRedefineRedefineToSubtypeItem]
1430 # flags: --allow-redefinition
1431 class Super: ...
1432 class Sub(Super): ...
1433
1434 if bool():
1435 x = [Super(), Super()]
1436 else:
1437 x = [Sub(), Sub()]
1438 reveal_type(x[0]) # N: Revealed type is "__main__.Sub"
1439
1440 [case testNewRedefineFunctionArgumentsFullContext]
1441 # flags: --allow-redefinition
1442 from typing import Optional
1443
1444 def process(items: list[Optional[str]]) -> None:
1445 if not items:
1446 items = [None]
1447 reveal_type(items) # N: Revealed type is "builtins.list[builtins.str | None]"
1448 process(items) # OK
1449
1450 [case testNewRedefineUnionArgumentFallbackUnion]
1451 # flags: --allow-redefinition
1452 from typing import Union, Optional, TypeVar, Sequence
1453
1454 T = TypeVar("T")
1455 def gen(x: T) -> Union[str, T]: ...
1456
1457 def test(x: Optional[str]) -> None:
1458 if x is None:
1459 x = gen("foo")
1460 reveal_type(x) # N: Revealed type is "builtins.str"
1461
1462 [case testNewRedefineUnionArgumentFallbackAsync]
1463 # flags: --allow-redefinition
1464 from typing import Optional, TypeVar
1465
1466 T = TypeVar("T")
1467 async def gen(x: T) -> T: ...
1468
1469 async def test(x: Optional[str]) -> None:
1470 if x is None:
1471 x = await gen("foo")
1472 reveal_type(x) # N: Revealed type is "builtins.str"
1473
1474 [case testNewRedefineFunctionArgumentsEmptyContext]
1475 # flags: --allow-redefinition
1476 def process1(items: list[str]) -> None:
1477 items = [item.split() for item in items]
1478 reveal_type(items) # N: Revealed type is "builtins.list[builtins.list[builtins.str]]"
1479
1480 def process2(items: list[str]) -> None:
1481 if bool():
1482 items = [item.split() for item in items]
1483 reveal_type(items) # N: Revealed type is "builtins.list[builtins.list[builtins.str]]"
1484 reveal_type(items) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.list[builtins.str]]"
1485
1486 def process3(items: list[str]) -> None:
1487 if bool():
1488 items = []
1489 reveal_type(items) # N: Revealed type is "builtins.list[builtins.str]"
1490 reveal_type(items) # N: Revealed type is "builtins.list[builtins.str]"
1491 [builtins fixtures/primitives.pyi]
1492
1493 [case testNewRedefineNarrowingForNestedUnionWithAny]
1494 # flags: --allow-redefinition
1495 from typing import Any, Union
1496
1497 a: Any
1498 Int = Union[int, Any]
1499
1500 def test(x: Union[str, Int]) -> None:
1501 x = a
1502 reveal_type(x) # N: Revealed type is "Any"
1503 y: Union[str, Int]
1504 y = a
1505 reveal_type(y) # N: Revealed type is "Any"
1506
1507 [case testNewRedefineNarrowingForNestedUnionWithNone]
1508 # flags: --allow-redefinition
1509 from typing import Any, Union
1510
1511 a: Any
1512 Int = Union[int, None]
1513
1514 def test(x: Union[str, Int]) -> None:
1515 x = a
1516 reveal_type(x) # N: Revealed type is "Any"
1517 y: Union[str, Int]
1518 y = a
1519 reveal_type(y) # N: Revealed type is "builtins.str | Any | builtins.int"
1520
1521 [case testRegularNarrowingForNestedUnionWithAny]
1522 from typing import Any, Union
1523
1524 a: Any
1525 Int = Union[int, Any]
1526
1527 def test(x: Union[str, Int]) -> None:
1528 x = a
1529 reveal_type(x) # N: Revealed type is "Any"
1530 y: Union[str, Int]
1531 y = a
1532 reveal_type(y) # N: Revealed type is "Any"
1533
1534 [case testRegularNarrowingForNestedUnionWithNone]
1535 from typing import Any, Union
1536
1537 a: Any
1538 Int = Union[int, None]
1539
1540 def test(x: Union[str, Int]) -> None:
1541 x = a
1542 reveal_type(x) # N: Revealed type is "builtins.str | Any | builtins.int"
1543 y: Union[str, Int]
1544 y = a
1545 reveal_type(y) # N: Revealed type is "builtins.str | Any | builtins.int"
1546
1547 [case testNewRedefineWidenedArgumentDeferral]
1548 # flags: --allow-redefinition
1549 def foo(x: int) -> None:
1550 reveal_type(x) # N: Revealed type is "builtins.int"
1551 if bool():
1552 x = "no"
1553 c: C
1554 c.x
1555 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
1556
1557 class C:
1558 def __init__(self) -> None:
1559 self.x = defer()
1560
1561 def defer() -> int: ...