File: check-statements.test

package info (click to toggle)
mypy 1.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,464 kB
  • sloc: python: 114,757; ansic: 13,343; cpp: 11,380; makefile: 254; sh: 31
file content (2363 lines) | stat: -rw-r--r-- 61,665 bytes parent folder | download | duplicates (3)
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
-- Return statement
-- ----------------


[case testReturnValue]
import typing
def f() -> 'A':
    return A()
def g() -> 'B':
    return A()
class A:
    pass
class B:
    pass
[out]
main:5: error: Incompatible return value type (got "A", expected "B")

[case testReturnSubtype]
import typing
def f() -> 'B':
    return A()
def g() -> 'A':
    return B()
class A:
    pass
class B(A):
    pass
[out]
main:3: error: Incompatible return value type (got "A", expected "B")

[case testReturnWithoutAValue]
import typing
def f() -> 'A':
    return
def g() -> None:
    return
class A:
    pass
[out]
main:3: error: Return value expected

[case testReturnNoneInFunctionReturningNone]
import typing
def f() -> None:
    return None
def g() -> None:
    return f()
[out]

[case testReturnInGenerator]
from typing import Generator
def f() -> Generator[int, None, str]:
    yield 1
    return "foo"
[out]

[case testEmptyReturnInGenerator]
from typing import Generator
def f() -> Generator[int, None, str]:
    yield 1
    return  # E: Return value expected
[out]

[case testNoReturnInGenerator]
from typing import Generator
def f() -> Generator[int, None, str]:  # E: Missing return statement
    yield 1
[out]

[case testEmptyReturnInNoneTypedGenerator]
from typing import Generator
def f() -> Generator[int, None, None]:
    yield 1
    return
[out]

[case testNonEmptyReturnInNoneTypedGenerator]
from typing import Generator
def f() -> Generator[int, None, None]:
    yield 1
    return 42  # E: No return value expected
[out]

[case testReturnInIterator]
from typing import Iterator
def f() -> Iterator[int]:
    yield 1
    return "foo" # E: No return value expected
[out]


-- If statement
-- ------------


[case testIfStatement]

a: A
a2: A
a3: A
b: bool
if a:
    a = b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
elif a2:
    a = b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
elif a3:
    a = b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
else:
    a = b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if b:
    pass
elif b:
    pass
if b:
    pass

class A: pass
[builtins fixtures/bool.pyi]


-- Loops
-- -----


[case testWhileStatement]

a: A
b: bool
while a:
    a = b    # Fail
else:
    a = b    # Fail
while b:
    b = b

class A: pass
[builtins fixtures/bool.pyi]
[out]
main:5: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
main:7: error: Incompatible types in assignment (expression has type "bool", variable has type "A")

[case testForStatement]
class A: pass

a: A
b: object
for a in [A()]:
    a = b  # E: Incompatible types in assignment (expression has type "object", variable has type "A")
else:
    a = b  # E: Incompatible types in assignment (expression has type "object", variable has type "A")
[builtins fixtures/list.pyi]

[case testBreakStatement]
import typing
while None:
    break
[builtins fixtures/bool.pyi]
[out]

[case testContinueStatement]
import typing
while None:
    continue
[builtins fixtures/bool.pyi]
[out]

[case testForStatementTypeComments]

from typing import List, Union
x = []  # type: List[int]

for y in x:  # type: str  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
    pass

for z in x:  # type: int
    pass

for w in x:  # type: Union[int, str]
    reveal_type(w)  # N: Revealed type is "Union[builtins.int, builtins.str]"

for v in x:  # type: int, int  # E: Syntax error in type annotation # N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn)
    pass
[builtins fixtures/list.pyi]

[case testForStatementMultipleTypeComments]

from typing import List, Tuple
x = []  # type: List[Tuple[int, int]]

for y in x:  # type: int, int  # E: Syntax error in type annotation # N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn)
    pass

for z in x:  # type: Tuple[int, int]
    pass

for w,v in x:  # type: int, str  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
    pass

for a, b in x:  # type: int, int, int  # E: Incompatible number of tuple items
    pass
[builtins fixtures/list.pyi]


-- Operator assignment
-- -------------------


[case testPlusAssign]
a: A
b: B
c: C
a += b   # Fail
b += a   # Fail
c += a   # Fail
a += c

class A:
    def __add__(self, x: 'C') -> 'A': pass

class B:
    def __add__(self, x: A) -> 'C': pass

class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:4: error: Unsupported operand types for + ("A" and "B")
main:5: error: Incompatible types in assignment (expression has type "C", variable has type "B")
main:6: error: Unsupported left operand type for + ("C")

[case testMinusAssign]
a: A
b: B
c: C
a -= b   # Fail
b -= a   # Fail
c -= a   # Fail
a -= c

class A:
    def __sub__(self, x: 'C') -> 'A': pass

class B:
    def __sub__(self, x: A) -> 'C': pass

class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:4: error: Unsupported operand types for - ("A" and "B")
main:5: error: Incompatible types in assignment (expression has type "C", variable has type "B")
main:6: error: Unsupported left operand type for - ("C")

[case testMulAssign]
a: A
c: C
a *= a   # Fail
c *= a   # Fail
a *= c

class A:
    def __mul__(self, x: 'C') -> 'A': pass

class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Unsupported operand types for * ("A" and "A")
main:4: error: Unsupported left operand type for * ("C")

[case testMatMulAssign]
a: A
c: C
a @= a   # E: Unsupported operand types for @ ("A" and "A")
c @= a   # E: Unsupported left operand type for @ ("C")
a @= c

class A:
    def __matmul__(self, x: 'C') -> 'A': pass

class C: pass
[builtins fixtures/tuple.pyi]

[case testDivAssign]
a: A
c: C
a /= a   # Fail
c /= a   # Fail
a /= c

class A:
    def __truediv__(self, x: 'C') -> 'A': pass

class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Unsupported operand types for / ("A" and "A")
main:4: error: Unsupported left operand type for / ("C")

[case testPowAssign]
a: A
c: C
a **= a   # Fail
c **= a   # Fail
a **= c

class A:
    def __pow__(self, x: 'C') -> 'A': pass

class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Unsupported operand types for ** ("A" and "A")
main:4: error: Unsupported left operand type for ** ("C")

[case testSubtypesInOperatorAssignment]
a: A
b: B
b += b
b += a
a += b

class A:
    def __add__(self, x: 'A') -> 'B': pass

class B(A): pass
[builtins fixtures/tuple.pyi]
[out]

[case testAdditionalOperatorsInOpAssign]
a: A
c: C
a &= a  # Fail
a >>= a # Fail
a //= a # Fail
a &= c
a >>= c
a //= c
class A:
    def __and__(self, x: 'C') -> 'A': pass
    def __rshift__(self, x: 'C') -> 'A': pass
    def __floordiv__(self, x: 'C') -> 'A': pass
class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Unsupported operand types for & ("A" and "A")
main:4: error: Unsupported operand types for >> ("A" and "A")
main:5: error: Unsupported operand types for // ("A" and "A")

[case testInplaceOperatorMethods]
import typing
class A:
    def __iadd__(self, x: int) -> 'A': pass
    def __imul__(self, x: str) -> 'A': pass
    def __imatmul__(self, x: str) -> 'A': pass
a = A()
a += 1
a *= ''
a @= ''
a += '' # E: Argument 1 to "__iadd__" of "A" has incompatible type "str"; expected "int"
a *= 1  # E: Argument 1 to "__imul__" of "A" has incompatible type "int"; expected "str"
a @= 1  # E: Argument 1 to "__imatmul__" of "A" has incompatible type "int"; expected "str"

[case testInplaceSetitem]
class A(object):
    def __init__(self) -> None:
        self.a = [1]

    def __iadd__(self, a):
        # type: (int) -> A
        self.a += [2]
        return self

a = A()
b = [a]
b[0] += 1
[builtins fixtures/list.pyi]
[out]


-- Assert statement
-- ----------------


[case testAssert]
import typing
assert None + None # Fail
assert None
[out]
main:2: error: Unsupported left operand type for + ("None")


-- Exception handling
-- ------------------


[case testRaiseStatement]

e: BaseException
f: MyError
a: A
raise a # Fail
raise e
raise f
class A: pass
class MyError(BaseException): pass
[builtins fixtures/exception.pyi]
[out]
main:5: error: Exception must be derived from BaseException

[case testRaiseClassObject]
class A: pass
class MyError(BaseException): pass
def f(): pass
if object():
    raise BaseException
if object():
    raise MyError
if object():
    raise A # E: Exception must be derived from BaseException
if object():
    raise object # E: Exception must be derived from BaseException
if object():
    raise f # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]

[case testRaiseClassObjectCustomInit]
class MyBaseError(BaseException):
    def __init__(self, required) -> None:
        ...
class MyError(Exception):
    def __init__(self, required1, required2) -> None:
        ...
class MyKwError(Exception):
    def __init__(self, *, kwonly) -> None:
        ...
class MyErrorWithDefault(Exception):
    def __init__(self, optional=1) -> None:
        ...
if object():
    raise BaseException
if object():
    raise Exception
if object():
    raise BaseException(1)
if object():
    raise Exception(2)
if object():
    raise MyBaseError(4)
if object():
    raise MyError(5, 6)
if object():
    raise MyKwError(kwonly=7)
if object():
    raise MyErrorWithDefault(8)
if object():
    raise MyErrorWithDefault
if object():
    raise MyBaseError  # E: Too few arguments for "MyBaseError"
if object():
    raise MyError  # E: Too few arguments for "MyError"
if object():
    raise MyKwError  # E: Missing named argument "kwonly" for "MyKwError"
[builtins fixtures/exception.pyi]

[case testRaiseExceptionType]
import typing
x: typing.Type[BaseException]
raise x
[builtins fixtures/exception.pyi]

[case testRaiseNonExceptionTypeFails]
import typing
x = int # type: typing.Type[int]
raise x # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]

[case testRaiseUnion]
import typing
x: typing.Union[BaseException, typing.Type[BaseException]]
raise x
[builtins fixtures/exception.pyi]

[case testRaiseNonExceptionUnionFails]
import typing
x: typing.Union[BaseException, int]
raise x # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]

[case testRaiseFromStatement]
e: BaseException
f: MyError
a: A
x: BaseException
del x
if object():
    raise e from a # E: Exception must be derived from BaseException
if object():
    raise e from e
if object():
    raise e from f
if object():
    raise e from x # E: Trying to read deleted variable "x"
class A: pass
class MyError(BaseException): pass
[builtins fixtures/exception.pyi]

[case testRaiseFromClassobject]
import typing
class A: pass
class MyError(BaseException): pass
def f(): pass
if object():
    raise BaseException from BaseException
if object():
    raise BaseException from MyError
if object():
    raise BaseException from A # E: Exception must be derived from BaseException
if object():
    raise BaseException from object # E: Exception must be derived from BaseException
if object():
    raise BaseException from f # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]

[case testRaiseNotImplementedFails]
if object():
    raise NotImplemented    # E: Exception must be derived from BaseException; did you mean "NotImplementedError"?
if object():
    raise NotImplemented()  # E: Exception must be derived from BaseException; did you mean "NotImplementedError"?
[builtins fixtures/notimplemented.pyi]

[case testTryFinallyStatement]
import typing
try:
    b = object() # type: A # Fail
finally:
    c = object() # type: A # Fail
class A: pass
[out]
main:3: error: Incompatible types in assignment (expression has type "object", variable has type "A")
main:5: error: Incompatible types in assignment (expression has type "object", variable has type "A")

[case testSimpleTryExcept]

try:
  pass
except BaseException as e:
  a: BaseException
  o: object
  e = a
  e = o # Fail
class A: pass
class B: pass
[builtins fixtures/exception.pyi]
[out]
main:8: error: Incompatible types in assignment (expression has type "object", variable has type "BaseException")

[case testTypeErrorInBlock]
class A: pass
class B: pass
while int():
    x: A
    if int():
        x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
        x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testTypeErrorInvolvingBaseException]
class A: pass

x: BaseException
a: A
if int():
    a = BaseException()  # E: Incompatible types in assignment (expression has type "BaseException", variable has type "A")
if int():
    a = object()         # E: Incompatible types in assignment (expression has type "object", variable has type "A")
if int():
    x = object()         # E: Incompatible types in assignment (expression has type "object", variable has type "BaseException")
if int():
    x = A()              # E: Incompatible types in assignment (expression has type "A", variable has type "BaseException")
if int():
    x = BaseException()
[builtins fixtures/exception.pyi]

[case testSimpleTryExcept2]
import typing
try:
  pass
except BaseException as e:
  e = object() # Fail
  e = BaseException()
[builtins fixtures/exception.pyi]
[out]
main:5: error: Incompatible types in assignment (expression has type "object", variable has type "BaseException")

[case testBaseClassAsExceptionTypeInExcept]
import typing
class Err(BaseException): pass
try:
  pass
except Err as e:
  e = BaseException()  # E: Incompatible types in assignment (expression has type "BaseException", variable has type "Err")
  e = Err()
[builtins fixtures/exception.pyi]
[case testMultipleExceptHandlers]
import typing
class Err(BaseException): pass
try:
    pass
except BaseException as e:
    pass
except Err as f:
    f = BaseException()  # E: Incompatible types in assignment (expression has type "BaseException", variable has type "Err")
    f = Err()
[builtins fixtures/exception.pyi]
[case testTryExceptStatement]
import typing
class A: pass
class B: pass
class Err(BaseException): pass
try:
    a = B() # type: A  # E: Incompatible types in assignment (expression has type "B", variable has type "A")
except BaseException as e:
    e = A()  # E: Incompatible types in assignment (expression has type "A", variable has type "BaseException")
    e = Err()
except Err as f:
    f = BaseException()  # E: Incompatible types in assignment (expression has type "BaseException", variable has type "Err")
    f = Err()
[builtins fixtures/exception.pyi]
[case testTryExceptWithinFunction]
import typing
def f() -> None:
  try: pass
  except BaseException as e:
    e = object() # Fail
    e = BaseException()
  except Err as f:
    f = BaseException() # Fail
    f = Err()
class Err(BaseException): pass
[builtins fixtures/exception.pyi]
[out]
main:5: error: Incompatible types in assignment (expression has type "object", variable has type "BaseException")
main:8: error: Incompatible types in assignment (expression has type "BaseException", variable has type "Err")

[case testTryExceptFlow]
def f() -> None:
  x = 1
  try:
    pass
  except:
    raise
  x + 'a' # E: Unsupported left operand type for + ("int")
[builtins fixtures/exception.pyi]
[out]

[case testTryWithElse]
import typing
try: pass
except BaseException: pass
else:
  object(None) # E: Too many arguments for "object"
[builtins fixtures/exception.pyi]

[case testRedefinedFunctionInTryWithElse]
def f() -> None: pass
try:
    pass
except BaseException:
    f2 = f
else:
    def f2() -> str: pass
try:
    pass
except BaseException:
    f3 = f
else:
    def f3() -> None: pass
[builtins fixtures/exception.pyi]
[out]
main:7: error: Incompatible redefinition (redefinition with type "Callable[[], str]", original type "Callable[[], None]")

[case testExceptWithoutType]
import typing
try:
    -None # E: Unsupported operand type for unary - ("None")
except:
    ~None # E: Unsupported operand type for ~ ("None")
[builtins fixtures/exception.pyi]

[case testRaiseWithoutArgument]
import typing
try:
    None
except:
    raise
[builtins fixtures/exception.pyi]

[case testExceptWithMultipleTypes]
import typing
class E1(BaseException): pass
class E2(E1): pass
try:
    pass
except (E1, E2): pass
except (E1, object): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
except (object, E2): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
except (E1, (E2,)): pass  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)

except (E1, E2): pass
except ((E1, E2)): pass
except (((E1, E2))): pass
[builtins fixtures/exception.pyi]

[case testExceptWithTypeType]
import typing
E = BaseException  # type: typing.Type[BaseException]

try:
    pass
except E:
    pass
[builtins fixtures/exception.pyi]

[case testExceptWithMultipleTypes2]
import typing
class E1(BaseException): pass
class E2(E1): pass
try:
    pass
except (E1, E2) as e1:
    x = e1 # type: E1
    y = e1 # type: E2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E2")
except (E2, E1) as e2:
    a = e2 # type: E1
    b = e2 # type: E2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E2")
except (E1, E2, int) as e3: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
    pass
[builtins fixtures/exception.pyi]

[case testExceptWithMultipleTypes3]
import typing
class E1(BaseException): pass
class E1_1(E1): pass
class E1_2(E1): pass
try: pass
except (E1, E1_1, E1_2) as e1:
    x = e1 # type: E1
    y = e1 # type: E1_1 # E: Incompatible types in assignment (expression has type "E1", variable has type "E1_1")
    z = e1 # type: E1_2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E1_2")
except (E1_1, E1_2) as e2:
    a = e2 # type: E1
    b = e2 # type: E1_1 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_1")
    c = e2 # type: E1_2 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_2")
[builtins fixtures/exception.pyi]

[case testExceptWithMultipleTypes4]
from typing import Tuple, Type, Union

class E1(BaseException): pass
class E2(BaseException): pass
class E3(BaseException): pass

def variadic(exc: Tuple[Type[E1], ...]) -> None:
    try:
        pass
    except exc as e:
        reveal_type(e)  # N: Revealed type is "__main__.E1"

def union(exc: Union[Type[E1], Type[E2]]) -> None:
    try:
        pass
    except exc as e:
        reveal_type(e)  # N: Revealed type is "Union[__main__.E1, __main__.E2]"

def tuple_in_union(exc: Union[Type[E1], Tuple[Type[E2], Type[E3]]]) -> None:
    try:
        pass
    except exc as e:
        reveal_type(e)  # N: Revealed type is "Union[__main__.E1, __main__.E2, __main__.E3]"

def variadic_in_union(exc: Union[Type[E1], Tuple[Type[E2], ...]]) -> None:
    try:
        pass
    except exc as e:
        reveal_type(e)  # N: Revealed type is "Union[__main__.E1, __main__.E2]"

def nested_union(exc: Union[Type[E1], Union[Type[E2], Type[E3]]]) -> None:
    try:
        pass
    except exc as e:
        reveal_type(e)  # N: Revealed type is "Union[__main__.E1, __main__.E2, __main__.E3]"

def error_in_union(exc: Union[Type[E1], int]) -> None:
    try:
        pass
    except exc as e:  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
        pass

def error_in_variadic(exc: Tuple[int, ...]) -> None:
    try:
        pass
    except exc as e:  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
        pass

[builtins fixtures/tuple.pyi]

[case testExceptWithAnyTypes]
from typing import Any

E1 = None  # type: Any
class E2(BaseException): pass
class NotBaseDerived: pass

try:
    pass
except BaseException as e1:
    reveal_type(e1)  # N: Revealed type is "builtins.BaseException"
except (E1, BaseException) as e2:
    reveal_type(e2)  # N: Revealed type is "Union[Any, builtins.BaseException]"
except (E1, E2) as e3:
    reveal_type(e3)  # N: Revealed type is "Union[Any, __main__.E2]"
except (E1, E2, BaseException) as e4:
    reveal_type(e4)  # N: Revealed type is "Union[Any, builtins.BaseException]"

try: pass
except E1 as e1:
    reveal_type(e1)  # N: Revealed type is "Any"
except E2 as e2:
    reveal_type(e2)  # N: Revealed type is "__main__.E2"
except NotBaseDerived as e3:  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
    pass
except (NotBaseDerived, E1) as e4:  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
    pass
except (NotBaseDerived, E2) as e5:  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
    pass
except (NotBaseDerived, E1, E2) as e6:  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
    pass
except (E1, E2, NotBaseDerived) as e6:  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
    pass
[builtins fixtures/exception.pyi]

[case testReuseTryExceptionVariable]
import typing
class E1(BaseException): pass
class E2(BaseException): pass
try: pass
except E1 as e: pass
try: pass
except E1 as e: pass
try: pass
except E2 as e: pass
e + 1 # E: Trying to read deleted variable "e"  # E: Name "e" is used before definition
e = E1() # E: Assignment to variable "e" outside except: block
[builtins fixtures/exception.pyi]

[case testReuseDefinedTryExceptionVariable]
import typing
class E1(BaseException): pass
class E2(BaseException): pass
e = 1
def f(): e  # Prevent redefinition
e = 1
try: pass
except E1 as e: pass
e = 1 # E: Assignment to variable "e" outside except: block
e = E1() # E: Assignment to variable "e" outside except: block
[builtins fixtures/exception.pyi]

[case testExceptionVariableReuseInDeferredNode1]
def f(*a: BaseException) -> int:
    x
    try: pass
    except BaseException as err: pass
    try: pass
    except BaseException as err: f(err)
    return 0
x = f()
[builtins fixtures/exception.pyi]

[case testExceptionVariableReuseInDeferredNode2]
def f(*a: BaseException) -> int:
    try: pass
    except BaseException as err: pass
    x
    try: pass
    except BaseException as err: f(err)
    return 0
x = f()
[builtins fixtures/exception.pyi]

[case testExceptionVariableReuseInDeferredNode3]
def f(*a: BaseException) -> int:
    try: pass
    except BaseException as err: pass
    try: pass
    except BaseException as err: f(err)
    x
    return 0
x = f()
[builtins fixtures/exception.pyi]

[case testExceptionVariableReuseInDeferredNode4]
class EA(BaseException):
    a = None  # type: int
class EB(BaseException):
    b = None  # type: str
def f(*arg: BaseException) -> int:
    x
    try: pass
    except EA as err:
        f(err)
        a = err.a
        reveal_type(a)
    try: pass
    except EB as err:
        f(err)
        b = err.b
        reveal_type(b)
    return 0
x = f()
[builtins fixtures/exception.pyi]
[out]
main:11: note: Revealed type is "builtins.int"
main:16: note: Revealed type is "builtins.str"

[case testExceptionVariableReuseInDeferredNode5]
class EA(BaseException):
    a = None  # type: int
class EB(BaseException):
    b = None  # type: str
def f(*arg: BaseException) -> int:
    try: pass
    except EA as err:
        f(err)
        a = err.a
        reveal_type(a)
    x
    try: pass
    except EB as err:
        f(err)
        b = err.b
        reveal_type(b)
    return 0
x = f()
[builtins fixtures/exception.pyi]
[out]
main:10: note: Revealed type is "builtins.int"
main:16: note: Revealed type is "builtins.str"

[case testExceptionVariableReuseInDeferredNode6]
class EA(BaseException):
    a = None  # type: int
class EB(BaseException):
    b = None  # type: str
def f(*arg: BaseException) -> int:
    try: pass
    except EA as err:
        f(err)
        a = err.a
        reveal_type(a)
    try: pass
    except EB as err:
        f(err)
        b = err.b
        reveal_type(b)
    x
    return 0
x = f()
[builtins fixtures/exception.pyi]
[out]
main:10: note: Revealed type is "builtins.int"
main:15: note: Revealed type is "builtins.str"

[case testExceptionVariableWithDisallowAnyExprInDeferredNode]
# flags: --disallow-any-expr
def f() -> int:
    x
    try:
        pass
    except Exception as ex:
        pass
    return 0
x = f()
[builtins fixtures/exception.pyi]

[case testArbitraryExpressionAsExceptionType]
import typing
a = BaseException
try: pass
except a as b:
    b = BaseException()
    b = object() # E: Incompatible types in assignment (expression has type "object", variable has type "BaseException")
[builtins fixtures/exception.pyi]

[case testInvalidExceptionCallable]
import typing
def exc() -> BaseException: pass
try: pass
except exc as e: pass             # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
except BaseException() as b: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
[builtins fixtures/exception.pyi]

[case testTupleValueAsExceptionType]
import typing
def exc() -> BaseException: pass
class E1(BaseException): pass
class E1_1(E1): pass
class E1_2(E1): pass

exs1 = (E1, E1_1, E1_2)
try: pass
except exs1 as e1:
    x = e1 # type: E1
    y = e1 # type: E1_1 # E: Incompatible types in assignment (expression has type "E1", variable has type "E1_1")
    z = e1 # type: E1_2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E1_2")

exs2 = (E1_1, E1_2)
try: pass
except exs2 as e2:
    a = e2 # type: E1
    b = e2 # type: E1_1 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_1")
    c = e2 # type: E1_2 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_2")

exs3 = (E1, (E1_1, (E1_2,)))
try: pass
except exs3 as e3: pass  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
[builtins fixtures/exception.pyi]

[case testInvalidTupleValueAsExceptionType]
import typing
def exc() -> BaseException: pass
class E1(BaseException): pass
class E2(E1): pass

exs1 = (E1, E2, int)
try: pass
except exs1 as e: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
[builtins fixtures/exception.pyi]

[case testOverloadedExceptionType]
from foo import *
[file foo.pyi]
from typing import overload
class E(BaseException):
    @overload
    def __init__(self) -> None: pass
    @overload
    def __init__(self, x) -> None: pass
try:
    pass
except E as e:
    e = E()
    e = BaseException() # E: Incompatible types in assignment (expression has type "BaseException", variable has type "E")
[builtins fixtures/exception.pyi]

[case testExceptionWithAnyBaseClass]
from typing import Any
E = None  # type: Any
class EE(E): pass
raise EE()
raise EE
[builtins fixtures/exception.pyi]

[case testExceptionIsType]
from typing import Type
class B(BaseException): pass
def f(e: Type[B]):
    try: pass
    except e: pass
def g(e: Type[BaseException]):
    try: pass
    except e as err:
        reveal_type(err)
def h(e: Type[int]):
    try: pass
    except e: pass
[builtins fixtures/exception.pyi]
[out]
main:9: note: Revealed type is "builtins.BaseException"
main:12: error: Exception type must be derived from BaseException (or be a tuple of exception classes)


-- Del statement
-- -------------


[case testDelStmtWithIndex]
a: A
b: B
del b[a]
del b[b] # E: Argument 1 to "__delitem__" of "B" has incompatible type "B"; expected "A"
del a[a] # E: "A" has no attribute "__delitem__"
del a[b] # E: "A" has no attribute "__delitem__"
class B:
  def __delitem__(self, index: 'A'): pass
class A: pass
[builtins fixtures/tuple.pyi]

[case testDelStmtWithAttribute]
class A:
    def f(self): pass
    x = 0
a = A()
del a.f
del a.x
del a.z # E: "A" has no attribute "z"

[case testDelStatementWithTuple]
class A:
    x = 0
a = A()
del a.x, a.y # E: "A" has no attribute "y"
[builtins fixtures/tuple.pyi]

[case testDelStmtWithTypeInfo]
class Foo: ...
del Foo
Foo + 1  # E: Trying to read deleted variable "Foo"

[case testDelStatementWithAssignmentSimple]
a = 1
a + 1
del a
a + 1 # E: Trying to read deleted variable "a"
[builtins fixtures/ops.pyi]

[case testDelStatementWithAssignmentTuple]
a = 1
b = 1
del (a, b)
b + 1 # E: Trying to read deleted variable "b"
[builtins fixtures/ops.pyi]

[case testDelStatementWithAssignmentList]
a = 1
b = 1
del [a, b]
b + 1 # E: Trying to read deleted variable "b"
[builtins fixtures/list.pyi]

[case testDelStatementWithAssignmentClass]
class C:
    a = 1

c = C()
c.a = 1
c.a + 1
del c.a
c.a + 1
[builtins fixtures/ops.pyi]

[case testDelStatementWithConditions]
x = 5
del x
if x: ...  # E: Trying to read deleted variable "x"

def f(x):
    return x

if 0: ...
elif f(x): ...  # E: Trying to read deleted variable "x"

while x == 5: ...  # E: Trying to read deleted variable "x"

-- Yield statement
-- ---------------


[case testSimpleYield]
from typing import Iterator
def f() -> Iterator[int]:
    yield 1
    yield '' # E: Incompatible types in "yield" (actual type "str", expected type "int")
[builtins fixtures/for.pyi]
[out]

[case testYieldInFunctionReturningGenerator]
from typing import Generator
def f() -> Generator[int, None, None]:
    yield 1
[builtins fixtures/for.pyi]
[out]

[case testYieldInFunctionReturningIterable]
from typing import Iterable
def f() -> Iterable[int]:
    yield 1
[builtins fixtures/for.pyi]
[out]

[case testYieldInFunctionReturningObject]
def f() -> object:
    yield 1
[builtins fixtures/for.pyi]
[out]

[case testYieldInFunctionReturningAny]
from typing import Any
def f() -> Any:
    yield object()
[out]

[case testYieldInFunctionReturningFunction]
from typing import Callable
def f() -> Callable[[], None]: # E: The return type of a generator function should be "Generator" or one of its supertypes
    yield object()
[out]

[case testYieldInDynamicallyTypedFunction]
import typing
def f():
    yield f

[case testWithInvalidInstanceReturnType]
import typing
def f() -> int: # E: The return type of a generator function should be "Generator" or one of its supertypes
    yield 1
[builtins fixtures/for.pyi]
[out]

[case testTypeInferenceContextAndYield]
from typing import List, Iterator
def f() -> 'Iterator[List[int]]':
    yield []
    yield [object()] # E: List item 0 has incompatible type "object"; expected "int"
[builtins fixtures/for.pyi]
[out]

[case testYieldAndReturnWithoutValue]
from typing import Iterator
def f() -> Iterator[int]:
    yield 1
    return
[builtins fixtures/for.pyi]

[case testYieldWithNoValue]
from typing import Iterator
def f() -> Iterator[None]:
    yield
[builtins fixtures/for.pyi]

[case testYieldWithNoValueWhenValueRequired]
from typing import Iterator
def f() -> Iterator[int]:
    yield  # E: Yield value expected
[builtins fixtures/for.pyi]
[out]

[case testYieldWithExplicitNone]
from typing import Iterator
def f() -> Iterator[None]:
    yield None
[builtins fixtures/for.pyi]
[out]


-- Yield from statement
-- --------------------
--
-- (It's not really a statement, but don't want to move the tests.)

[case testSimpleYieldFromWithIterator]
from typing import Iterator
def g() -> Iterator[str]:
    yield '42'
def h() -> Iterator[int]:
    yield 42
def f() -> Iterator[str]:
    yield from g()
    yield from h()  # E: Incompatible types in "yield from" (actual type "int", expected type "str")
[out]

[case testYieldFromAppliedToAny]
from typing import Any
def g() -> Any:
    yield object()
def f() -> Any:
    yield from g()
[out]

[case testYieldFromInFunctionReturningFunction]
from typing import Iterator, Callable
def g() -> Iterator[int]:
    yield 42
def f() -> Callable[[], None]:  # E: The return type of a generator function should be "Generator" or one of its supertypes
    yield from g()
[out]

[case testYieldFromNotIterableReturnType]
from typing import Iterator
def g() -> Iterator[int]:
    yield 42
def f() -> int:  # E: The return type of a generator function should be "Generator" or one of its supertypes
    yield from g()
[out]

[case testYieldFromNotAppliedIterator]
from typing import Iterator
def g() -> int:
    return 42
def f() -> Iterator[int]:
    yield from g()  # E: "yield from" can't be applied to "int"
[out]

[case testYieldFromCheckIncompatibleTypesTwoIterables]
from typing import List, Iterator
def g() -> Iterator[List[int]]:
    yield [2, 3, 4]
def f() -> Iterator[List[int]]:
    yield from g()
    yield from [1, 2, 3]  # E: Incompatible types in "yield from" (actual type "int", expected type "list[int]")
[builtins fixtures/for.pyi]
[out]

[case testYieldFromNotAppliedToNothing]
def h():
    yield from  # E: Invalid syntax
[out]

[case testYieldFromAndYieldTogether]
from typing import Iterator
def f() -> Iterator[str]:
    yield "g1 ham"
    yield from g()
    yield "g1 eggs"
def g() -> Iterator[str]:
    yield "g2 spam"
    yield "g2 more spam"
[out]

[case testYieldFromAny]
from typing import Iterator
def f(a):
    b = yield from a
    return b
[out]

[case testYieldFromGenericCall]
from typing import Generator, TypeVar
T = TypeVar('T')
def f(a: T) -> Generator[int, str, T]: pass
def g() -> Generator[int, str, float]:
    r = yield from f('')
    reveal_type(r)  # N: Revealed type is "builtins.str"
    return 3.14

[case testYieldFromTupleStatement]
from typing import Generator
def g() -> Generator[int, None, None]:
    yield from ()
    yield from (0, 1, 2)
    yield from (0, "ERROR")  # E: Incompatible types in "yield from" (actual type "Union[int, str]", expected type "int")
    yield from ("ERROR",)  # E: Incompatible types in "yield from" (actual type "str", expected type "int")
[builtins fixtures/tuple.pyi]

-- With statement
-- --------------


[case testSimpleWith]
import typing
class A:
    def __enter__(self) -> None: pass
    def __exit__(self, x, y, z) -> None: pass
with A():
    object(A) # E: Too many arguments for "object"

[case testWithStmtAndInvalidExit]
import typing
class A:
    def __enter__(self) -> None: pass
    def __exit__(self, x, y) -> None: pass
with A(): # E: Too many arguments for "__exit__" of "A"
    pass

[case testWithStmtAndMissingExit]
import typing
class A:
    def __enter__(self) -> None: pass
with A(): # E: "A" has no attribute "__exit__"
    pass

[case testWithStmtAndInvalidEnter]
import typing
class A:
    def __enter__(self, x) -> None: pass
    def __exit__(self, x, y, z) -> None: pass
with A(): # E: Too few arguments for "__enter__" of "A"
    pass

[case testWithStmtAndMissingEnter]
import typing
class A:
    def __exit__(self, x, y, z) -> None: pass
with A(): # E: "A" has no attribute "__enter__"
    pass

[case testWithStmtAndMultipleExprs]
import typing
class A:
    def __enter__(self) -> None: pass
    def __exit__(self, x, y, z) -> None: pass
class B:
    def __enter__(self) -> None: pass
with A(), B(): # E: "B" has no attribute "__exit__"
    pass
with B(), A(): # E: "B" has no attribute "__exit__"
    pass

[case testWithStmtAndResult]
import typing
class B: pass
class A:
    def __enter__(self) -> B: pass
    def __exit__(self, x, y, z): pass
with A() as b:
    b = B()
    b = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")

[case testWithStmtAndMultipleResults]
from typing import TypeVar, Generic
t = TypeVar('t')
class B: pass
class C: pass
class A(Generic[t]):
    def __enter__(self) -> t: pass
    def __exit__(self, x, y, z): pass
a_b = A() # type: A[B]
a_c = A() # type: A[C]
with a_b as b, a_c as c:
    b = B()
    c = C()
    b = c # E: Incompatible types in assignment (expression has type "C", variable has type "B")
    c = b # E: Incompatible types in assignment (expression has type "B", variable has type "C")

[case testWithStmtAndComplexTarget]
from typing import Tuple
class A:
    def __enter__(self) -> Tuple[int, str]: pass
    def __exit__(self, x, y, z): pass
with A() as (a, b):
    a = 1
    b = ''
    a = b # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[builtins fixtures/tuple.pyi]

[case testWithStmtTypeComment]

from typing import Union
class A:
    def __enter__(self) -> int: pass
    def __exit__(self, x, y, z): pass

with A():  # type: int  # E: Invalid type comment: "with" statement has no targets
    pass

with A() as a:  # type: int
    pass

with A() as b:  # type: str  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
    pass

with A() as c:  # type: int, int  # E: Syntax error in type annotation # N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn)
    pass

with A() as d:  # type: Union[int, str]
    reveal_type(d)  # N: Revealed type is "Union[builtins.int, builtins.str]"

[case testWithStmtTupleTypeComment]

from typing import Tuple
class A:
    def __enter__(self) -> Tuple[int, int]: pass
    def __exit__(self, x, y, z): pass

with A():
    pass

with A() as a:  # type: Tuple[int, int]
    pass

with A() as b:  # type: Tuple[int, str]  # E: Incompatible types in assignment (expression has type "tuple[int, int]", variable has type "tuple[int, str]")
    pass

with A() as (c, d):  # type: int, int
    pass

with A() as (e, f):  # type: Tuple[int, int]
    pass

with A() as (g, h):  # type: int  # E: Tuple type expected for multiple variables
    pass

with A() as (i, j):  # type: int, int, str  # E: Incompatible number of tuple items
    pass

with A() as (k, l):  # type: int, str  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
    pass
[builtins fixtures/tuple.pyi]

[case testWithStmtComplexTypeComment]

from typing import Tuple
class A:
    def __enter__(self) -> Tuple[int, int]: pass
    def __exit__(self, x, y, z): pass

class B:
    def __enter__(self) -> str: pass
    def __exit__(self, x, y, z): pass

with A() as a, A() as (b, c), B() as d:  # type: Tuple[int, int], (int, int), str
    pass

with A() as e, A() as (f, g), B() as h:  # type: Tuple[int, int], Tuple[int, int], str
    pass

with A() as i, A() as (j, k), B() as l:  # type: (int, int), (int, int), str  # E: Syntax error in type annotation # N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn)
    pass

with A(), A(), B() as m, A() as n, B(), B() as o:  # type: int, Tuple[int, int]  # E: Incompatible number of types for "with" targets
    pass

with A(), B(), B() as p, A(), A():  # type: str
    pass
[builtins fixtures/tuple.pyi]

[case testWithStmtBoolExitReturnWithResultFalse]
from typing import Optional

class InvalidReturn1:
    def __exit__(self, x, y, z) -> bool:  # E: "bool" is invalid as return type for "__exit__" that always returns False \
# N: Use "typing.Literal[False]" as the return type or change it to "None" \
# N: If return type of "__exit__" implies that it may return True, the context manager may swallow exceptions
        return False

class InvalidReturn2:
    def __exit__(self, x, y, z) -> Optional[bool]:  # E: "bool" is invalid as return type for "__exit__" that always returns False \
# N: Use "typing.Literal[False]" as the return type or change it to "None" \
# N: If return type of "__exit__" implies that it may return True, the context manager may swallow exceptions
        if int():
            return False
        else:
            return False

class InvalidReturn3:
    def __exit__(self, x, y, z) -> bool:  # E: "bool" is invalid as return type for "__exit__" that always returns False \
# N: Use "typing.Literal[False]" as the return type or change it to "None" \
# N: If return type of "__exit__" implies that it may return True, the context manager may swallow exceptions
        def nested() -> bool:
            return True
        return False
[builtins fixtures/bool.pyi]

[case testWithStmtBoolExitReturnOkay]
from typing import Literal

class GoodReturn1:
    def __exit__(self, x, y, z) -> bool:
        if int():
            return True
        else:
            return False

class GoodReturn2:
    def __exit__(self, x, y, z) -> bool:
        if int():
            return False
        else:
            return True

class GoodReturn3:
    def __exit__(self, x, y, z) -> bool:
        return bool()

class GoodReturn4:
    def __exit__(self, x, y, z) -> None:
        return

class GoodReturn5:
    def __exit__(self, x, y, z) -> None:
        return None

class GoodReturn6:
    def exit(self, x, y, z) -> bool:
        return False

class GoodReturn7:
    def exit(self, x, y, z) -> bool:
        pass

class MissingReturn:
    def exit(self, x, y, z) -> bool: # E: Missing return statement
        x = 0

class LiteralReturn:
    def __exit__(self, x, y, z) -> Literal[False]:
        return False
[builtins fixtures/bool.pyi]

[case testWithStmtBoolExitReturnInStub]
import stub

[file stub.pyi]
from typing import Optional

class C1:
    def __exit__(self, x, y, z) -> bool: ...

class C2:
    def __exit__(self, x, y, z) -> bool: pass

class C3:
    def __exit__(self, x, y, z) -> Optional[bool]: pass
[builtins fixtures/bool.pyi]

[case testWithStmtScopeBasics]
from m import A, B

def f1() -> None:
    with A() as x:
        reveal_type(x)  # N: Revealed type is "m.A"
    with B() as x:
        reveal_type(x)  # N: Revealed type is "m.B"

def f2() -> None:
    with A() as x:
        reveal_type(x)  # N: Revealed type is "m.A"
    y = x  # Use outside with makes the scope function-level
    with B() as x: \
        # E: Incompatible types in assignment (expression has type "B", variable has type "A")
        reveal_type(x)  # N: Revealed type is "m.A"

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeAndFuncDef]
from m import A, B

with A() as x:
    reveal_type(x)  # N: Revealed type is "m.A"

def f() -> None:
    pass  # Don't support function definition in the middle

with B() as x: \
    # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    reveal_type(x)  # N: Revealed type is "m.A"

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeAndFuncDef2]
from m import A, B

def f() -> None:
    pass  # function before with is unsupported

with A() as x:
    reveal_type(x)  # N: Revealed type is "m.A"

with B() as x: \
    # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    reveal_type(x)  # N: Revealed type is "m.A"

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeAndFuncDef3]
from m import A, B

with A() as x:
    reveal_type(x)  # N: Revealed type is "m.A"

with B() as x: \
    # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    reveal_type(x)  # N: Revealed type is "m.A"

def f() -> None:
    pass  # function after with is unsupported

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeAndFuncDef4]
from m import A, B

with A() as x:
    def f() -> None:
        pass  # Function within with is unsupported

    reveal_type(x)  # N: Revealed type is "m.A"

with B() as x: \
    # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    reveal_type(x)  # N: Revealed type is "m.A"

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeAndImport1]
from m import A, B, x

with A() as x: \
    # E: Incompatible types in assignment (expression has type "A", variable has type "B")
    reveal_type(x)  # N: Revealed type is "m.B"

with B() as x:
    reveal_type(x)  # N: Revealed type is "m.B"

[file m.pyi]
x: B

class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeAndImport2]
from m import A, B
import m as x

with A() as x: \
     # E: Incompatible types in assignment (expression has type "A", variable has type Module)
    pass

with B() as x: \
     # E: Incompatible types in assignment (expression has type "B", variable has type Module)
    pass

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...
[builtins fixtures/module.pyi]

[case testWithStmtScopeAndImportStar]
from m import A, B
from m import *

with A() as x:
    pass

with B() as x: \
     # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    pass

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeNestedWith1]
from m import A, B

with A() as x:
    with B() as x: \
        # E: Incompatible types in assignment (expression has type "B", variable has type "A")
        reveal_type(x)  # N: Revealed type is "m.A"

with B() as x:
    with A() as x: \
        # E: Incompatible types in assignment (expression has type "A", variable has type "B")
        reveal_type(x)  # N: Revealed type is "m.B"

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeNestedWith2]
from m import A, B

with A() as x:
    with A() as y:
        reveal_type(y)  # N: Revealed type is "m.A"
    with B() as y:
        reveal_type(y)  # N: Revealed type is "m.B"

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeInnerAndOuterScopes]
from m import A, B

x = A()  # Outer scope should have no impact

with A() as x:
    pass

def f() -> None:
    with A() as x:
        reveal_type(x)  # N: Revealed type is "m.A"
    with B() as x:
        reveal_type(x)  # N: Revealed type is "m.B"

y = x

with A() as x:
    pass

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeMultipleContextManagers]
from m import A, B

with A() as x, B() as y:
    reveal_type(x)  # N: Revealed type is "m.A"
    reveal_type(y)  # N: Revealed type is "m.B"
with B() as x, A() as y:
    reveal_type(x)  # N: Revealed type is "m.B"
    reveal_type(y)  # N: Revealed type is "m.A"

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeMultipleAssignment]
from m import A, B

with A() as (x, y):
    reveal_type(x)  # N: Revealed type is "m.A"
    reveal_type(y)  # N: Revealed type is "builtins.int"
with B() as [x, y]:
    reveal_type(x)  # N: Revealed type is "m.B"
    reveal_type(y)  # N: Revealed type is "builtins.str"

[file m.pyi]
from typing import Tuple

class A:
    def __enter__(self) -> Tuple[A, int]: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> Tuple[B, str]: ...
    def __exit__(self, x, y, z) -> None: ...
[builtins fixtures/tuple.pyi]

[case testWithStmtScopeComplexAssignments]
from m import A, B, f

with A() as x:
    pass
with B() as x: \
    # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    pass
with B() as f(x).x:
    pass

with A() as y:
    pass
with B() as y: \
    # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    pass
with B() as f(y)[0]:
    pass

[file m.pyi]
def f(x): ...

class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeAndClass]
from m import A, B

with A() as x:
    pass

class C:
    with A() as y:
        pass
    with B() as y:
        pass

with B() as x: \
    # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    pass

[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeInnerScopeReference]
from m import A, B

with A() as x:
    def f() -> A:
        return x
    f()

with B() as x: \
    # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    pass
[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...

[case testWithStmtScopeAndLambda]
from m import A, B

# This is technically not correct, since the lambda can outlive the with
# statement, but this behavior seems more intuitive.

with A() as x:
    lambda: reveal_type(x)  # N: Revealed type is "m.A"

with B() as x:
    pass
[file m.pyi]
class A:
    def __enter__(self) -> A: ...
    def __exit__(self, x, y, z) -> None: ...
class B:
    def __enter__(self) -> B: ...
    def __exit__(self, x, y, z) -> None: ...


-- Chained assignment
-- ------------------


[case testChainedAssignment]
import typing
class A: pass
class B: pass
x = y = A()
if int():
    x = A()
if int():
    y = A()
if int():
    x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
    y = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testChainedAssignment2]
import typing
def f() -> None:
    x = 1
    y = 'x'
    if int():
        x = y = 'x' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
        x = y = 1   # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[builtins fixtures/primitives.pyi]
[out]

[case testChainedAssignmentWithType]
# flags: --no-strict-optional
x = y = None # type: int
if int():
    x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if int():
    y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if int():
    x = 1
if int():
    y = 1


-- Star assignment
-- ---------------


[case testAssignListToStarExpr]
from typing import List
bs: List[A]
cs: List[B]
if int():
    *bs, b = bs
if int():
    *bs, c = cs  # E: Incompatible types in assignment (expression has type "list[B]", variable has type "list[A]")
    if int():
        *ns, c = cs
if int():
    nc = cs

class A: pass
class B: pass
[builtins fixtures/list.pyi]


-- Type aliases
-- ------------


[case testSimpleTypeAlias]
import typing
foo = int
def f(x: foo) -> None: pass
f(1)
f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int"

[case testTypeAliasDefinedInAModule]
import typing
import m
def f(x: m.foo) -> None: pass
f(1)
f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
[file m.py]
import typing
foo = int

[case testTypeAliasDefinedInAModule2]
import typing
from m import foo
def f(x: foo) -> None: pass
f(1)
f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
[file m.py]
import typing
foo = int


-- nonlocal and global
-- -------------------


[case testTypeOfGlobalUsed]
import typing
class A(): pass
class B(): pass
g = A()
def f() -> None:
    global g
    g = B()  # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testTypeOfNonlocalUsed]
import typing
def f() -> None:
    a = A()
    def g() -> None:
        nonlocal a
        a = B()

class A(): pass
class B(): pass
[out]
main:6: error: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testTypeOfOuterMostNonlocalUsed]
import typing
def f() -> None:
    a = A()
    def g() -> None:
        a = B()
        def h() -> None:
            nonlocal a
            a = A()
            a = B()

class A(): pass
class B(): pass
[out]
main:8: error: Incompatible types in assignment (expression has type "A", variable has type "B")

[case testAugmentedAssignmentIntFloat]
weight0 = 65.5
reveal_type(weight0)  # N: Revealed type is "builtins.float"
if int():
    weight0 = 65
    reveal_type(weight0)  # N: Revealed type is "builtins.int"
    weight0 *= 'a'  # E: Incompatible types in assignment (expression has type "str", variable has type "float")
    weight0 *= 0.5
    reveal_type(weight0)  # N: Revealed type is "builtins.float"
    weight0 *= object()  # E: Unsupported operand types for * ("float" and "object")
    reveal_type(weight0) # N: Revealed type is "builtins.float"

[builtins fixtures/float.pyi]

[case testAugmentedAssignmentIntFloatMember]
class A:
    def __init__(self) -> None:
        self.weight0 = 65.5
        reveal_type(self.weight0)  # N: Revealed type is "builtins.float"
        self.weight0 = 65
        reveal_type(self.weight0)  # N: Revealed type is "builtins.int"
        self.weight0 *= 'a'  # E: Incompatible types in assignment (expression has type "str", variable has type "float")
        self.weight0 *= 0.5
        reveal_type(self.weight0)  # N: Revealed type is "builtins.float"
        self.weight0 *= object()  # E: Unsupported operand types for * ("float" and "object")
        reveal_type(self.weight0) # N: Revealed type is "builtins.float"

[builtins fixtures/float.pyi]

[case testAugmentedAssignmentIntFloatDict]
from typing import Dict
d = {'weight0': 65.5}
reveal_type(d['weight0'])  # N: Revealed type is "builtins.float"
d['weight0'] = 65
reveal_type(d['weight0'])  # N: Revealed type is "builtins.float"
d['weight0'] *= 'a'  # E: Unsupported operand types for * ("float" and "str")
d['weight0'] *= 0.5
reveal_type(d['weight0'])  # N: Revealed type is "builtins.float"
d['weight0'] *= object()  # E: Unsupported operand types for * ("float" and "object")
reveal_type(d['weight0']) # N: Revealed type is "builtins.float"

[builtins fixtures/floatdict.pyi]

[case testForwardRefsInForStatementImplicit]
from typing import List, NamedTuple
lst: List[N]

for i in lst:
    reveal_type(i.x)  # N: Revealed type is "builtins.int"
    a: str = i[0] # E: Incompatible types in assignment (expression has type "int", variable has type "str")

N = NamedTuple('N', [('x', int)])
[builtins fixtures/list.pyi]
[out]

[case testForwardRefsInForStatement]
from typing import List, NamedTuple
lst: List[M]

for i in lst: # type: N
    reveal_type(i.x)  # N: Revealed type is "builtins.int"
    a: str = i[0] # E: Incompatible types in assignment (expression has type "int", variable has type "str")

N = NamedTuple('N', [('x', int)])
class M(N): pass
[builtins fixtures/list.pyi]
[out]

[case testForwardRefsInWithStatementImplicit]
from typing import ContextManager, Any, TypedDict
cm: ContextManager[N]

with cm as g:
    a: int = g['x']

N = TypedDict('N', {'x': int})
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
[out]

[case testForwardRefsInWithStatement]
from typing import ContextManager, Any, TypedDict
cm: ContextManager[Any]

with cm as g:  # type: N
    a: str = g['x']  # E: Incompatible types in assignment (expression has type "int", variable has type "str")

N = TypedDict('N', {'x': int})
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
[out]

[case testGlobalWithoutInitialization]
# flags: --disable-error-code=annotation-unchecked
from typing import List

def foo() -> None:
    global bar
    # TODO: Confusing error message
    bar = []  # type: List[str]  # E: Name "bar" already defined (possibly by an import)
    bar  # E: Name "bar" is not defined

def foo2():
    global bar2
    bar2 = []  # type: List[str]
    bar2
[builtins fixtures/list.pyi]

[case testNoteUncheckedAnnotation]
def foo():
    x: int = "no"  # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs
    y = "no"  # type: int  # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs
    z: int  # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs

[case testGeneratorUnion]
from typing import Generator, Union

class A: pass
class B: pass

def foo(x: int) -> Union[Generator[A, None, None], Generator[B, None, None]]:
    yield x  # E: Incompatible types in "yield" (actual type "int", expected type "Union[A, B]")

[case testYieldFromUnionOfGenerators]
from typing import Generator, Union

class T: pass

def foo(arg: Union[Generator[int, None, T], Generator[str, None, T]]) -> Generator[Union[int, str], None, T]:
    return (yield from arg)

[case testYieldFromInvalidUnionReturn]
from typing import Generator, Union

class A: pass
class B: pass

def foo(arg: Union[A, B]) -> Generator[Union[int, str], None, A]:
    return (yield from arg) # E: "yield from" can't be applied to "Union[A, B]"

[case testYieldFromUnionOfGeneratorWithIterableStr]
from typing import Generator, Union, Iterable, Optional

def foo(arg: Union[Generator[int, None, bytes], Iterable[str]]) -> Generator[Union[int, str], None, Optional[bytes]]:
    return (yield from arg)

def bar(arg: Generator[str, None, str]) -> Generator[str, None, str]:
    return foo(arg)  # E: Incompatible return value type (got "Generator[Union[int, str], None, Optional[bytes]]", expected "Generator[str, None, str]")

def launder(arg: Iterable[str]) -> Generator[Union[int, str], None, Optional[bytes]]:
    return foo(arg)

def baz(arg: Generator[str, None, str]) -> Generator[Union[int, str], None, Optional[bytes]]:
    # this is unsound, the Generator return type will actually be str
    return launder(arg)
[builtins fixtures/tuple.pyi]

[case testYieldIteratorReturn]
from typing import Iterator

def get_strings(foo: bool) -> Iterator[str]:
    if foo:
        return ["foo1", "foo2"]  # E: No return value expected
    else:
        yield "bar1"
        yield "bar2"
[builtins fixtures/tuple.pyi]

[case testYieldFromInvalidType]
from collections.abc import Iterator

class A:
    def list(self) -> None: ...

    def foo(self) -> list[int]:  # E: Function "__main__.A.list" is not valid as a type \
                                 # N: Perhaps you need "Callable[...]" or a callback protocol?
        return []

def fn() -> Iterator[int]:
    yield from A().foo()  # E: "list?[builtins.int]" has no attribute "__iter__" (not iterable)
[builtins fixtures/tuple.pyi]

[case testNoCrashOnStarRightHandSide]
x = *(1, 2, 3)  # E: can't use starred expression here
[builtins fixtures/tuple.pyi]


[case testTypingExtensionsSuggestion]
from typing import _FutureFeatureFixture

# This import is only needed in tests. In real life, mypy will always have typing_extensions in its
# build due to its pervasive use in typeshed. This assumption may one day prove False, but when
# that day comes this suggestion will also be less helpful than it is today.
import typing_extensions
[out]
main:1: error: Module "typing" has no attribute "_FutureFeatureFixture"
main:1: note: Use `from typing_extensions import _FutureFeatureFixture` instead
main:1: note: See https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-new-additions-to-the-typing-module
[builtins fixtures/tuple.pyi]

[case testNoCrashOnBreakOutsideLoopFunction]
def foo():
    for x in [1, 2]:
        def inner():
            break  # E: "break" outside loop
[builtins fixtures/list.pyi]

[case testNoCrashOnBreakOutsideLoopClass]
class Outer:
    for x in [1, 2]:
        class Inner:
            break  # E: "break" outside loop
[builtins fixtures/list.pyi]

[case testCallableInstanceOverlapAllowed]
# flags: --warn-unreachable
from typing import Any, Callable, List

class CAny:
    def __call__(self) -> Any: ...
class CNone:
    def __call__(self) -> None: ...
class CWrong:
    def __call__(self, x: int) -> None: ...

def describe(func: Callable[[], None]) -> str:
    if isinstance(func, CAny):
        return "CAny"
    elif isinstance(func, CNone):
        return "CNone"
    elif isinstance(func, CWrong):
        return "CWrong"  # E: Statement is unreachable
    else:
        return "other"

class C(CAny):
    def __call__(self) -> None: ...

def f():
    pass

describe(CAny())
describe(C())
describe(CNone())
describe(CWrong())  # E: Argument 1 to "describe" has incompatible type "CWrong"; expected "Callable[[], None]" \
                    # N: "CWrong.__call__" has type "def __call__(self, x: int) -> None"
describe(f)
[builtins fixtures/isinstancelist.pyi]