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 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241
|
-- Test cases for the new semantic analyzer
[case testNewAnalyzerEmpty]
[case testNewAnalyzerSimpleAssignment]
x = 1
x.y # E: "int" has no attribute "y"
y # E: Name "y" is not defined
[case testNewAnalyzerSimpleAnnotation]
x: int = 0
y: str = 0 \
# E: Incompatible types in assignment (expression has type "int", variable has type "str")
[case testNewAnalyzerSimpleClass]
class A:
x: int
a: A
a.x
a.y # E: "A" has no attribute "y"
[case testNewAnalyzerErrorInClassBody]
class A:
x # E: Name "x" is not defined
[case testNewAnalyzerTypeAnnotationForwardReference]
class A:
b: B
class B:
a: A
a: A
b: B
a.b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a.b = b
b.a = a
b.a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testNewAnalyzerTypeAnnotationCycle1]
import b
[file a.py]
import b
class A: pass
y: b.B
y() # E: "B" not callable
[file b.py]
import a
class B: pass
x: a.A
reveal_type(x) # N: Revealed type is "a.A"
[case testNewAnalyzerTypeAnnotationCycle2]
import a
[file a.py]
from b import B
class A: pass
y: B
y()
[file b.py]
from a import A
class B: pass
x: A
x()
[out]
tmp/b.py:4: error: "A" not callable
tmp/a.py:4: error: "B" not callable
[case testNewAnalyzerTypeAnnotationCycle3]
import b
[file a.py]
from b import bad # E: Module "b" has no attribute "bad"; maybe "bad2"?
[file b.py]
from a import bad2 # E: Module "a" has no attribute "bad2"; maybe "bad"?
[case testNewAnalyzerTypeAnnotationCycle4]
import b
[file a.py]
from b import bad # E: Module "b" has no attribute "bad"
[file b.py]
# TODO: Could we generate an error here as well?
from a import bad
[targets a, b, a, b, a, b, a, b, __main__]
[case testNewAnalyzerExportedValuesInImportAll]
from m import *
_ = a
_ = b
_ = c
_ = d
_e = e
_f = f # E: Name "f" is not defined
_ = _g # E: Name "_g" is not defined
reveal_type(_e) # N: Revealed type is "m.A"
[file m.py]
__all__ = ['a']
__all__ += ('b',)
__all__.append('c')
__all__.extend(('d', 'e'))
a = b = c = d = _g = 1
e: 'A'
f: 'A'
class A: ...
[builtins fixtures/module_all.pyi]
[case testNewAnalyzerSimpleFunction]
def f(x: int) -> str:
return 'x'
def g(x: int) -> int:
y = f(1)
return y # E: Incompatible return value type (got "str", expected "int")
[case testNewAnalyzerSimpleMethod]
class A:
def __init__(self, x: int) -> None:
self.x = x
def f(self) -> str:
return self.x # E: Incompatible return value type (got "int", expected "str")
def g(self) -> int:
return self.f() # E: Incompatible return value type (got "str", expected "int")
[case testNewAnalyzerFunctionForwardRef]
def f() -> None:
x = g(1) # E: Argument 1 to "g" has incompatible type "int"; expected "str"
reveal_type(x) # N: Revealed type is "builtins.str"
def g(x: str) -> str:
return x
[case testNewAnalyzerExportedImportThreePasses]
import b
[file a.py]
from b import b1 as a2
from b import b2 as a3
def a1() -> int: pass
reveal_type(a3()) # N: Revealed type is "builtins.int"
[file b.py]
from a import a1 as b2
from a import a2 as b3
def b1() -> str: pass
reveal_type(b3()) # N: Revealed type is "builtins.str"
[case testNewAnalyzerBool]
reveal_type(True) # N: Revealed type is "Literal[True]?"
reveal_type(False) # N: Revealed type is "Literal[False]?"
[case testNewAnalyzerNewTypeMultiplePasses]
import b
[file a.py]
from typing import NewType
import b
class A: pass
N2 = NewType('N2', b.N1)
def f1(x: A) -> None: pass
def f2(x: b.N1) -> None: pass
def f3(x: N2) -> None: pass
a = A()
n1 = b.N1(a)
n2 = N2(n1)
f1(a)
f1(n1)
f1(n2)
f2(a) # E: Argument 1 to "f2" has incompatible type "A"; expected "N1"
f2(n1)
f2(n2)
f3(a) # E: Argument 1 to "f3" has incompatible type "A"; expected "N2"
f3(n1) # E: Argument 1 to "f3" has incompatible type "N1"; expected "N2"
f3(n2)
# Test N2 etc.
[file b.py]
from typing import NewType
import a
N1 = NewType('N1', a.A)
[case testNewAnalyzerInheritanceForwardRef]
class C(B):
pass
class B(A):
pass
class A:
def __init__(self, x: str) -> None: pass
def f(self, x: int) -> None: pass
C(1) # E: Argument 1 to "C" has incompatible type "int"; expected "str"
B(1) # E: Argument 1 to "B" has incompatible type "int"; expected "str"
C('').f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
B('').f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
[case testNewAnalyzerInheritanceMROInCycle]
import a
[file a.py]
from b import A
import b
class B(A):
b: int
class D(b.C):
d: int
d = D()
reveal_type(d.a) # N: Revealed type is "builtins.int"
reveal_type(d.b) # N: Revealed type is "builtins.int"
reveal_type(d.c) # N: Revealed type is "builtins.int"
reveal_type(d.d) # N: Revealed type is "builtins.int"
[file b.py]
from a import B
class A:
a: int
class C(B):
c: int
[targets b, a, b, a, __main__]
[case testNewAnalyzerTypedDictClass]
from mypy_extensions import TypedDict
import a
class T1(TypedDict):
x: A
class A: pass
reveal_type(T1(x=A())) # E
[file a.py]
from mypy_extensions import TypedDict
from b import TD1 as TD2, TD3
class T2(TD3):
x: int
reveal_type(T2(x=2)) # E
[file b.py]
from a import TypedDict as TD1
from a import TD2 as TD3
[builtins fixtures/tuple.pyi]
[out]
tmp/a.py:5: note: Revealed type is "TypedDict('a.T2', {'x': builtins.int})"
main:6: note: Revealed type is "TypedDict('__main__.T1', {'x': __main__.A})"
[case testNewAnalyzerTypedDictClassInheritance]
from mypy_extensions import TypedDict
class T2(T1):
y: int
class T1(TypedDict):
x: str
class T3(TypedDict):
x: str
class T4(T3):
y: A
class A: pass
T2(x=0, y=0) # E: Incompatible types (expression has type "int", TypedDict item "x" has type "str")
x: T2
reveal_type(x) # N: Revealed type is "TypedDict('__main__.T2', {'x': builtins.str, 'y': builtins.int})"
y: T4
reveal_type(y) # N: Revealed type is "TypedDict('__main__.T4', {'x': builtins.str, 'y': __main__.A})"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerRedefinitionAndDeferral1a]
import a
[file a.py]
MYPY = False
if MYPY:
from b import x as y
x = 0
def y(): pass # E: Name "y" already defined on line 4
reveal_type(y) # N: Revealed type is "builtins.int"
y2 = y
class y2: pass # E: Name "y2" already defined on line 9
reveal_type(y2) # N: Revealed type is "builtins.int"
y3, y4 = y, y
if MYPY: # Tweak processing order
from b import f as y3 # E: Incompatible import of "y3" (imported name has type "Callable[[], Any]", local name has type "int")
reveal_type(y3) # N: Revealed type is "builtins.int"
[file b.py]
from a import x
def f(): pass
[targets a, b, a, a.y, b.f, __main__]
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerRedefinitionAndDeferral1b]
import a
[file a.py]
from b import x as y
x = 0
def y(): pass # E: Name "y" already defined on line 2
reveal_type(y) # N: Revealed type is "builtins.int"
y2 = y
class y2: pass # E: Name "y2" already defined on line 7
reveal_type(y2) # N: Revealed type is "builtins.int"
y3, y4 = y, y
from b import f as y3 # E: Incompatible import of "y3" (imported name has type "Callable[[], Any]", local name has type "int")
reveal_type(y3) # N: Revealed type is "builtins.int"
[file b.py]
MYPY = False
if MYPY: # Tweak processing order
from a import x
def f(): pass
[targets b, a, b, a, b.f, a.y, __main__]
[case testNewAnalyzerRedefinitionAndDeferral2a]
import a
[file a.py]
MYPY = False
if MYPY: # Tweak processing order
from b import C as C2
class C: pass
class C2: pass # E: Name "C2" already defined on line 4
[file b.py]
from a import C
[case testNewAnalyzerRedefinitionAndDeferral2b]
import a
[file a.py]
from b import C as C2
class C: pass
class C2: pass # E: Name "C2" already defined on line 2
[file b.py]
MYPY = False
if MYPY: # Tweak processing order
from a import C
[case testNewAnalyzerRedefinitionAndDeferral3]
import a
[file a.py]
from b import f as g
def f(): pass
a, *b = g()
class b(): pass # E: Name "b" already defined on line 4
reveal_type(b) # N: Revealed type is "Any"
[file b.py]
from a import f
[case testNewAnalyzerImportStarForwardRef1]
import a
[file a.py]
x: A
reveal_type(x) # N: Revealed type is "b.A"
from b import *
class A: pass # E: Name "A" already defined (possibly by an import)
[file b.py]
class A: pass
MYPY = False
if MYPY: # Tweak processing order
from a import x
[case testNewAnalyzerImportStarForwardRef2]
import a
[file a.py]
x: A
reveal_type(x) # N: Revealed type is "b.A"
MYPY = False
if MYPY: # Tweak processing order
from b import *
class A: pass # E: Name "A" already defined (possibly by an import)
[file b.py]
class A: pass
from a import x
[case testNewAnalyzerClassInFunction]
def main() -> None:
x: C
class C:
def __init__(self) -> None:
self.x: A
x() # E: "C" not callable
reveal_type(x.x) # N: Revealed type is "__main__.A@8"
class A: pass
[case testNewAnalyzerMutuallyRecursiveFunctions]
def main() -> None:
def f() -> int:
reveal_type(g()) # N: Revealed type is "builtins.str"
return int()
def g() -> str:
reveal_type(f()) # N: Revealed type is "builtins.int"
return str()
[case testNewAnalyzerMissingNamesInFunctions]
def main() -> None:
def f() -> None:
x # E: Name "x" is not defined
class C:
x # E: Name "x" is not defined
[case testNewAnalyzerCyclicDefinitions]
# flags: --disable-error-code used-before-def
gx = gy # E: Cannot resolve name "gy" (possible cyclic definition)
gy = gx
def main() -> None:
class C:
def meth(self) -> None:
lx = ly # E: Cannot resolve name "ly" (possible cyclic definition) \
# N: Recursive types are not allowed at function scope
ly = lx
[case testNewAnalyzerCyclicDefinitionCrossModule]
import b
[file a.py]
import b
x = b.x # E: Cannot resolve attribute "x" (possible cyclic definition) \
# E: Module has no attribute "x"
[file b.py]
import a
x = a.x
[builtins fixtures/module.pyi]
[case testNewAnalyzerMutuallyRecursiveOverloadedFunctions]
from typing import overload, Union
def main() -> None:
@overload
def f(x: int) -> int: ...
@overload
def f(x: str) -> str: ...
def f(x: Union[int, str]) -> Union[int, str]:
reveal_type(g(str())) # N: Revealed type is "builtins.str"
return x
@overload
def g(x: int) -> int: ...
@overload
def g(x: str) -> str: ...
def g(x: Union[int, str]) -> Union[int, str]:
reveal_type(f(int())) # N: Revealed type is "builtins.int"
return float() # E: Incompatible return value type (got "float", expected "Union[int, str]")
[case testNewAnalyzerNestedClassInMethod]
class C:
class D:
def meth(self) -> None:
x: Out.In
reveal_type(x.t) # N: Revealed type is "builtins.int"
class Out:
class In:
def meth(self) -> None:
self.t: int
[case testNewAnalyzerDeeplyNestedFunctions]
class Out:
class In:
def meth(self) -> None:
x: C.D
reveal_type(x.t) # N: Revealed type is "__main__.Test@10"
class C:
class D:
def meth(self) -> None:
self.t: Test
class Test:
def test(self) -> None:
def one() -> int:
reveal_type(other()) # N: Revealed type is "builtins.str"
return int()
def other() -> str:
reveal_type(one()) # N: Revealed type is "builtins.int"
return str()
[case testNewAnalyzerNestedClass1]
class A:
class B:
x: int
def __init__(self, x: int) -> None:
self.x = x
def f(self) -> str:
return self.x # E: Incompatible return value type (got "int", expected "str")
b: A.B
b = A.B('') # E: Argument 1 to "B" has incompatible type "str"; expected "int"
reveal_type(b) # N: Revealed type is "__main__.A.B"
reveal_type(b.x) # N: Revealed type is "builtins.int"
reveal_type(b.f()) # N: Revealed type is "builtins.str"
[case testNewAnalyzerNestedClass2]
class A:
class B:
x: int
def __init__(self, x: int) -> None:
self.x = x
def f(self) -> str:
return self.x # E: Incompatible return value type (got "int", expected "str")
b: A.B
b = A.B('') # E: Argument 1 to "B" has incompatible type "str"; expected "int"
reveal_type(b) # N: Revealed type is "__main__.A.B"
reveal_type(b.x) # N: Revealed type is "builtins.int"
reveal_type(b.f()) # N: Revealed type is "builtins.str"
[case testNewAnalyzerGenerics]
from typing import TypeVar, Generic
T = TypeVar('T')
class C(Generic[T]):
def __init__(self, x: T) -> None:
self.x = x
def get(self) -> T:
return self.x
c: C[int]
c2: C[int, str] # E: "C" expects 1 type argument, but 2 given
c3: C
c = C('') # E: Argument 1 to "C" has incompatible type "str"; expected "int"
reveal_type(c.get()) # N: Revealed type is "builtins.int"
reveal_type(c2) # N: Revealed type is "__main__.C[Any]"
reveal_type(c3) # N: Revealed type is "__main__.C[Any]"
[case testNewAnalyzerGenericsTypeVarForwardRef]
from typing import TypeVar, Generic
class C(Generic[T]):
def __init__(self, x: T) -> None:
self.x = x
def get(self) -> T:
return self.x
T = TypeVar('T')
c: C[int]
reveal_type(c) # N: Revealed type is "__main__.C[builtins.int]"
c = C('') # E: Argument 1 to "C" has incompatible type "str"; expected "int"
reveal_type(c.get()) # N: Revealed type is "builtins.int"
[case testNewAnalyzerTypeAlias]
from typing import Union, TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
class D(Generic[T, S]): pass
class C: pass
C2 = C
U = Union[C, int]
G = D[T, C]
c: C2
reveal_type(c) # N: Revealed type is "__main__.C"
u: U
reveal_type(u) # N: Revealed type is "Union[__main__.C, builtins.int]"
g: G[int]
reveal_type(g) # N: Revealed type is "__main__.D[builtins.int, __main__.C]"
[case testNewAnalyzerTypeAlias2]
from typing import Union
class C(D): pass
A = Union[C, int]
x: A
reveal_type(x) # N: Revealed type is "Union[__main__.C, builtins.int]"
class D: pass
[case testNewAnalyzerBuiltinTypeAliases]
from typing import List
x: List[C]
reveal_type(x) # N: Revealed type is "builtins.list[__main__.C]"
class C: pass
[builtins fixtures/list.pyi]
[case testNewAnalyzerVersionCheck]
import sys
if sys.version_info[0] < 2:
1()
import nonexistent
else:
def f(x: int) -> None: pass
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
def g() -> None:
if sys.version_info[0] < 3:
import nonexistent2
else:
1() # E: "int" not callable
[builtins fixtures/ops.pyi]
[case testNewAnalyzerVersionCheck2]
import sys
assert sys.version_info[0] == 3
1() # E: "int" not callable
assert sys.version_info[0] < 3
''()
[builtins fixtures/ops.pyi]
[case testNewAnalyzerOverload]
from typing import overload, Union
@overload
def f(x: int) -> int: ...
@overload
def f(x: str) -> str: ...
def f(x: Union[int, str]) -> Union[int, str]:
return 1.0 # E: Incompatible return value type (got "float", expected "Union[int, str]")
f(1)
f('')
f(1.0) # E: No overload variant of "f" matches argument type "float" \
# N: Possible overload variants: \
# N: def f(x: int) -> int \
# N: def f(x: str) -> str
[case testNewAnalyzerOverload2]
from typing import overload, Union
class A:
@overload
def f(self, x: int) -> int: ...
@overload
def f(self, x: str) -> str: ...
def f(self, x: Union[int, str]) -> Union[int, str]:
return 1.0 # E: Incompatible return value type (got "float", expected "Union[int, str]")
a = A()
a.f(1)
a.f('')
a.f(1.0) # E: No overload variant of "f" of "A" matches argument type "float" \
# N: Possible overload variants: \
# N: def f(self, x: int) -> int \
# N: def f(self, x: str) -> str
[case testNewAnalyzerPromotion]
def f(x: float) -> None: pass
y: int
f(y)
f(1)
[builtins fixtures/primitives.pyi]
[case testNewAnalyzerFunctionDecorator]
# flags: --disable-error-code used-before-def
from typing import Callable
@dec
def f1(x: int) -> int:
return '' # E: Incompatible return value type (got "str", expected "int")
def dec(f: Callable[[int], int]) -> Callable[[str], str]: ...
@dec
def f2(x: int) -> int:
return '' # E: Incompatible return value type (got "str", expected "int")
f1(1) # E: Argument 1 to "f1" has incompatible type "int"; expected "str"
reveal_type(f1('')) # N: Revealed type is "builtins.str"
f2(1) # E: Argument 1 to "f2" has incompatible type "int"; expected "str"
[case testNewAnalyzerTypeVarForwardReference]
# flags: --disable-error-code used-before-def
from typing import TypeVar, Generic
T = TypeVar('T')
XY = TypeVar('XY', X, Y)
class C(Generic[T]): pass
class D(C[XY], Generic[XY]): pass
class X: pass
class Y: pass
x: D[int] # E: Value of type variable "XY" of "D" cannot be "int"
y: D[Y]
[case testNewAnalyzerTypeVarForwardReference2]
from typing import TypeVar, Generic
T = TypeVar('T')
XY = TypeVar('XY', 'X', 'Y')
class C(Generic[T]): pass
class D(C[XY]): pass
class X: pass
class Y: pass
x: D[int] # E: Value of type variable "XY" of "D" cannot be "int"
y: D[Y]
[case testNewAnalyzerTypeVarForwardReferenceValuesDeferred]
from typing import TypeVar, Generic
T = TypeVar('T')
XY = TypeVar('XY', 'X', 'Y')
class C(Generic[T]): pass
class D(C[XY], Generic[XY]): pass
class X(Defer): pass
class Y(Defer): pass
class Defer: ...
x: D[int] # E: Value of type variable "XY" of "D" cannot be "int"
y: D[Y]
[builtins fixtures/list.pyi]
[case testNewAnalyzerTypeVarForwardReferenceBoundDeferred]
from typing import TypeVar, Generic
T = TypeVar('T')
TY = TypeVar('TY', bound='Y')
class C(Generic[T]): pass
class D(C[TY], Generic[TY]): pass
class Y(Defer): pass
class Defer: ...
x: D[int] # E: Type argument "int" of "D" must be a subtype of "Y"
y: D[Y]
[case testNewAnalyzerTypeVarForwardReferenceErrors]
from typing import TypeVar, Generic
class C(Generic[T]):
def __init__(self, x: T) -> None: ...
def func(x: U) -> U: ...
U = TypeVar('U', asdf, asdf) # E: Name "asdf" is not defined
T = TypeVar('T', bound='asdf') # E: Name "asdf" is not defined
reveal_type(C) # N: Revealed type is "def [T <: Any] (x: T`1) -> __main__.C[T`1]"
reveal_type(func) # N: Revealed type is "def [U in (Any, Any)] (x: U`-1) -> U`-1"
[case testNewAnalyzerSubModuleInCycle]
import a
[file a.py]
MYPY = False
if MYPY:
from b.c import x
[file b/__init__.pyi]
import b.c
[file b/c.pyi]
x = 0
import a
[case testNewAnalyzerBaseClassSelfReference]
from typing import TypeVar, Generic
T = TypeVar('T')
class A(Generic[T]): pass
class C(A[C]):
pass
class D(A['D']):
pass
a1: A[C] = C()
a2: A[D] = C() \
# E: Incompatible types in assignment (expression has type "C", variable has type "A[D]")
[case testNewAnalyzerTypeVarBoundForwardRef]
from typing import TypeVar
T = TypeVar('T', bound='C')
class C: pass
class D(C): pass
class E: pass
def f(x: T) -> T:
return x
reveal_type(f(D())) # N: Revealed type is "__main__.D"
f(E()) # E: Value of type variable "T" of "f" cannot be "E"
[case testNewAnalyzerNameExprRefersToIncompleteType]
import a
[file a.py]
from b import f
class C(D): pass
class D: pass
[file b.py]
from a import C
reveal_type(C()) # N: Revealed type is "a.C"
def f(): pass
[case testNewAnalyzerMemberExprRefersToIncompleteType]
import a
[file a.py]
from b import f
class C(D): pass
class D: pass
[file b.py]
import a
reveal_type(a.C()) # N: Revealed type is "a.C"
def f(): pass
[case testNewAnalyzerNamedTupleCall]
from typing import NamedTuple
class Other: pass
In = NamedTuple('In', [('s', str), ('t', Other)])
Out = NamedTuple('Out', [('x', In), ('y', Other)])
o: Out
i: In
reveal_type(o) # N: Revealed type is "Tuple[Tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.Out]"
reveal_type(o.x) # N: Revealed type is "Tuple[builtins.str, __main__.Other, fallback=__main__.In]"
reveal_type(o.y) # N: Revealed type is "__main__.Other"
reveal_type(o.x.t) # N: Revealed type is "__main__.Other"
reveal_type(i.t) # N: Revealed type is "__main__.Other"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNamedTupleClass]
from typing import NamedTuple
o: Out
i: In
class Out(NamedTuple):
x: In
y: Other
reveal_type(o) # N: Revealed type is "Tuple[Tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.Out]"
reveal_type(o.x) # N: Revealed type is "Tuple[builtins.str, __main__.Other, fallback=__main__.In]"
reveal_type(o.y) # N: Revealed type is "__main__.Other"
reveal_type(o.x.t) # N: Revealed type is "__main__.Other"
reveal_type(i.t) # N: Revealed type is "__main__.Other"
class In(NamedTuple):
s: str
t: Other
class Other: pass
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNamedTupleCallNested]
from typing import NamedTuple
o: C.Out
i: C.In
reveal_type(o) # N: Revealed type is "Tuple[Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In], __main__.C.Other, fallback=__main__.C.Out]"
reveal_type(o.x) # N: Revealed type is "Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In]"
reveal_type(o.y) # N: Revealed type is "__main__.C.Other"
reveal_type(o.x.t) # N: Revealed type is "__main__.C.Other"
reveal_type(i.t) # N: Revealed type is "__main__.C.Other"
class C:
In = NamedTuple('In', [('s', str), ('t', Other)])
Out = NamedTuple('Out', [('x', In), ('y', Other)])
class Other: pass
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNamedTupleClassNested]
from typing import NamedTuple
o: C.Out
i: C.In
reveal_type(o) # N: Revealed type is "Tuple[Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In], __main__.C.Other, fallback=__main__.C.Out]"
reveal_type(o.x) # N: Revealed type is "Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In]"
reveal_type(o.y) # N: Revealed type is "__main__.C.Other"
reveal_type(o.x.t) # N: Revealed type is "__main__.C.Other"
reveal_type(i.t) # N: Revealed type is "__main__.C.Other"
class C:
class Out(NamedTuple):
x: C.In
y: C.Other
class In(NamedTuple):
s: str
t: C.Other
class Other: pass
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNamedTupleCallNestedMethod]
from typing import NamedTuple
class C:
def get_tuple(self) -> None:
Out = NamedTuple('Out', [('x', 'In'), ('y', 'Other')])
In = NamedTuple('In', [('s', str), ('t', 'Other')])
class Other: pass
self.o: Out
c = C()
reveal_type(c.o) # N: Revealed type is "Tuple[Tuple[builtins.str, __main__.Other@7, fallback=__main__.C.In@6], __main__.Other@7, fallback=__main__.C.Out@5]"
reveal_type(c.o.x) # N: Revealed type is "Tuple[builtins.str, __main__.Other@7, fallback=__main__.C.In@6]"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNamedTupleClassNestedMethod]
from typing import NamedTuple
class C:
def get_tuple(self) -> None:
class Out(NamedTuple):
x: In
y: Other
def method(self) -> In: ...
class In(NamedTuple):
s: str
t: Other
class Other: pass
self.o: Out
c = C()
reveal_type(c.o) # N: Revealed type is "Tuple[Tuple[builtins.str, __main__.Other@12, fallback=__main__.C.In@9], __main__.Other@12, fallback=__main__.C.Out@5]"
reveal_type(c.o.x) # N: Revealed type is "Tuple[builtins.str, __main__.Other@12, fallback=__main__.C.In@9]"
reveal_type(c.o.method()) # N: Revealed type is "Tuple[builtins.str, __main__.Other@12, fallback=__main__.C.In@9]"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNamedTupleClassForwardMethod]
from typing import NamedTuple
n: NT
reveal_type(n.get_other()) # N: Revealed type is "Tuple[builtins.str, fallback=__main__.Other]"
reveal_type(n.get_other().s) # N: Revealed type is "builtins.str"
class NT(NamedTuple):
x: int
y: int
def get_other(self) -> Other: pass
class Other(NamedTuple):
s: str
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNamedTupleSpecialMethods]
from typing import NamedTuple
class Other: pass
In = NamedTuple('In', [('s', str), ('t', Other)])
Out = NamedTuple('Out', [('x', In), ('y', Other)])
class SubO(Out): pass
o: SubO
reveal_type(SubO._make) # N: Revealed type is "def (iterable: typing.Iterable[Any]) -> Tuple[Tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.SubO]"
reveal_type(o._replace(y=Other())) # N: Revealed type is "Tuple[Tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.SubO]"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNamedTupleBaseClass]
from typing import NamedTuple
class Other: pass
class In(NamedTuple):
s: str
t: Other
class Out(NamedTuple('Out', [('x', In), ('y', Other)])):
pass
o: Out
reveal_type(o) # N: Revealed type is "Tuple[Tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.Out]"
reveal_type(o.x) # N: Revealed type is "Tuple[builtins.str, __main__.Other, fallback=__main__.In]"
reveal_type(o.x.t) # N: Revealed type is "__main__.Other"
reveal_type(Out._make) # N: Revealed type is "def (iterable: typing.Iterable[Any]) -> Tuple[Tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.Out]"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerIncompleteRefShadowsBuiltin1]
import a
[file a.py]
from typing import TypeVar, Generic
from b import C as int
x: int[str]
reveal_type(x) # N: Revealed type is "a.C[builtins.str]"
T = TypeVar('T')
class C(Generic[T]): pass
[file b.py]
from a import C
[case testNewAnalyzerIncompleteRefShadowsBuiltin2]
import b
[file a.py]
import b
int = b.C
class C: pass
x: int
reveal_type(x) # N: Revealed type is "b.C"
[file b.py]
import a
int = a.C
class C: pass
x: int
reveal_type(x) # N: Revealed type is "a.C"
[case testNewAnalyzerNamespaceCompleteness]
import a
[file a.py]
import b
x: b.C
[file b.py]
from c import *
class C: pass
[file c.py]
import a
from b import C
[targets c, b, a, c, b, __main__]
[case testNewAnalyzerImportOverExistingInCycle]
import a
[file a.py]
C = 1
from b import C # E: Incompatible import of "C" (imported name has type "Type[C]", local name has type "int")
[file b.py]
import a
class C(B): ...
class B: ...
[case testNewAnalyzerImportOverExistingInCycleStar1]
import a
[file a.py]
C = 1
MYPY = False
if MYPY: # Tweak processing order
from b import * # E: Incompatible import of "C" (imported name has type "Type[C]", local name has type "int")
[file b.py]
import a
class C(B): ...
class B: ...
[case testNewAnalyzerImportOverExistingInCycleStar2]
import a
[file a.py]
C = 1
from b import * # E: Incompatible import of "C" (imported name has type "Type[C]", local name has type "int")
[file b.py]
MYPY = False
if MYPY: # Tweak processing order
import a
class C(B): ...
class B: ...
[case testNewAnalyzerIncompleteFixture]
from typing import Tuple
x: Tuple[int] # E: Name "tuple" is not defined
[builtins fixtures/complex.pyi]
[case testNewAnalyzerMetaclass1]
class A(metaclass=B):
pass
class B(type):
def f(cls) -> int:
return 0
reveal_type(A.f()) # N: Revealed type is "builtins.int"
[case testNewAnalyzerMetaclass2]
class B(type):
def f(cls) -> int:
return 0
class C: pass
class A(metaclass=B):
pass
class AA(metaclass=C): # E: Metaclasses not inheriting from "type" are not supported
pass
reveal_type(A.f()) # N: Revealed type is "builtins.int"
[case testNewAnalyzerMetaclassPlaceholder]
class B(C): pass
class A(metaclass=B):
pass
class C(type):
def f(cls) -> int:
return 0
reveal_type(A.f()) # N: Revealed type is "builtins.int"
[case testNewAnalyzerMetaclassSix1]
import six
class A(six.with_metaclass(B)):
pass
class B(type):
def f(cls) -> int:
return 0
reveal_type(A.f()) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerMetaclassSix2]
import six
@six.add_metaclass(B)
class A:
pass
class B(type):
def f(cls) -> int:
return 0
reveal_type(A.f()) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerMetaclassSix3]
import six
class A(six.with_metaclass(B, Defer)):
pass
class B(type):
def f(cls) -> int:
return 0
class Defer:
x: str
reveal_type(A.f()) # N: Revealed type is "builtins.int"
reveal_type(A.x) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerMetaclassSix4]
import six
class B(type):
def f(cls) -> int:
return 0
class A(six.with_metaclass(B, Defer)):
pass
class Defer:
x: str
reveal_type(A.f()) # N: Revealed type is "builtins.int"
reveal_type(A.x) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerMetaclassFuture1]
import future.utils
class A(future.utils.with_metaclass(B)):
pass
class B(type):
def f(cls) -> int:
return 0
reveal_type(A.f()) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerMetaclassFuture3]
import future.utils
class A(future.utils.with_metaclass(B, Defer)):
pass
class B(type):
def f(cls) -> int:
return 0
class Defer:
x: str
reveal_type(A.f()) # N: Revealed type is "builtins.int"
reveal_type(A.x) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerMetaclassFuture4]
# flags: --disable-error-code used-before-def
import future.utils
class B(type):
def f(cls) -> int:
return 0
reveal_type(A.f()) # N: Revealed type is "builtins.int"
reveal_type(A.x) # N: Revealed type is "builtins.str"
class A(future.utils.with_metaclass(B, Defer)):
pass
class Defer:
x: str
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerFinalDefiningModuleVar]
from typing import Final
class D(C): ...
class C: ...
x: Final = C()
y: Final[C] = D()
bad: Final[D] = C() # E: Incompatible types in assignment (expression has type "C", variable has type "D")
reveal_type(x) # N: Revealed type is "__main__.C"
reveal_type(y) # N: Revealed type is "__main__.C"
[case testNewAnalyzerFinalDefiningInstanceVar]
from typing import Final
class D: ...
class E(C): ...
class C:
def __init__(self, x: D) -> None:
self.x: Final = x
self.y: Final[C] = E(D())
reveal_type(C(D()).x) # N: Revealed type is "__main__.D"
reveal_type(C(D()).y) # N: Revealed type is "__main__.C"
[case testNewAnalyzerFinalReassignModuleVar]
from typing import Final
class A: ...
x: Final = A()
x = A() # E: Cannot assign to final name "x"
x2: Final = A()
def f2() -> None:
global x2
def f() -> None:
g()
x2 = A() # E: Cannot assign to final name "x2"
def g() -> None:
f()
[case testNewAnalyzerFinalReassignModuleReexport]
import a
[file a.py]
from b import ID, A
class C(A): ...
ID = C() # E: Cannot assign to final name "ID"
[file b.py]
from typing import Final
from a import C
class A:
x: C
ID: Final = A()
[case testNewAnalyzerFinalOverrideInSubclass]
from typing import Final
class B:
def __init__(self, x: int) -> None:
self.x: Final = x
class C(B):
x = 1 # E: Cannot assign to final name "x"
[case testNewAnalyzerAssignmentAfterStarImport]
import a
[file a.py]
from b import *
x = 1
def f(): ...
[file b.py]
from a import f
x: int
[case testNewAnalyzerClassLevelImport]
# flags: --ignore-missing-imports
class Test:
import a
def __init__(self) -> None:
some_module = self.a
[case testNewAnalyzerAliasToNotReadyClass]
import a
[file a.py]
from b import B
x: A
A = B
[file b.py]
from typing import List
from a import x
class B(List[B]): pass
reveal_type(x[0][0]) # N: Revealed type is "b.B"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyClass2]
from typing import List
x: A
class A(List[B]): pass
B = A
reveal_type(x[0][0]) # N: Revealed type is "__main__.A"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyClass3]
# flags: --disable-error-code used-before-def
from typing import List
x: B
B = A
A = C
class C(List[B]): pass
reveal_type(x[0][0]) # N: Revealed type is "__main__.C"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyNestedClass]
import a
[file a.py]
from b import Out
x: A
A = Out.B
[file b.py]
from typing import List
from a import x
class Out:
class B(List[B]): pass
reveal_type(x[0][0]) # N: Revealed type is "b.Out.B"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyNestedClass2]
from typing import List
x: Out.A
class Out:
class A(List[B]): pass
B = Out.A
reveal_type(x[0][0]) # N: Revealed type is "__main__.Out.A"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyClassGeneric]
import a
[file a.py]
from typing import Tuple
from b import B, T
x: A[int]
A = B[Tuple[T, T]]
[file b.py]
from typing import List, Generic, TypeVar
from a import x
class B(List[B], Generic[T]): pass
T = TypeVar('T')
reveal_type(x) # N: Revealed type is "b.B[Tuple[builtins.int, builtins.int]]"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyClassInGeneric]
import a
[file a.py]
from typing import Tuple
from b import B
x: A
A = Tuple[B, B]
[file b.py]
from typing import List
from a import x
class B(List[B]): pass
reveal_type(x) # N: Revealed type is "Tuple[b.B, b.B]"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyClassDoubleGeneric]
from typing import List, TypeVar, Union
T = TypeVar('T')
x: B[int]
A = Union[int, T]
B = A[List[T]]
class C(List[B[int]]): pass
y: C
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]"
reveal_type(y[0]) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]"
[builtins fixtures/list.pyi]
[case testNewAnalyzerForwardAliasFromUnion]
from typing import Union, List
A = Union['B', 'C']
class D:
x: List[A]
def test(self) -> None:
reveal_type(self.x[0].y) # N: Revealed type is "builtins.int"
class B:
y: int
class C:
y: int
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyTwoDeferrals]
# flags: --disable-error-code used-before-def
from typing import List
x: B
B = List[C]
A = C
class C(List[A]): pass
reveal_type(x) # N: Revealed type is "builtins.list[__main__.C]"
reveal_type(x[0][0]) # N: Revealed type is "__main__.C"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyDirectBase]
# flags: --disable-error-code used-before-def
from typing import List
def test() -> None:
x: B
B = List[C]
class C(B): pass
reveal_type(x)
reveal_type(x[0][0])
[builtins fixtures/list.pyi]
[out]
main:5: error: Cannot resolve name "B" (possible cyclic definition)
main:5: note: Recursive types are not allowed at function scope
main:6: error: Cannot resolve name "B" (possible cyclic definition)
main:6: note: Recursive types are not allowed at function scope
main:6: error: Cannot resolve name "C" (possible cyclic definition)
main:9: note: Revealed type is "Any"
main:10: note: Revealed type is "Any"
[case testNewAnalyzerAliasToNotReadyTwoDeferralsFunction]
# flags: --disable-error-code used-before-def
import a
[file a.py]
from typing import List
from b import D
def f(x: B) -> List[B]: ...
B = List[C]
A = C
class C(List[A]): pass
[file b.py]
from a import f
class D: ...
reveal_type(f) # N: Revealed type is "def (x: builtins.list[a.C]) -> builtins.list[builtins.list[a.C]]"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyDirectBaseFunction]
# flags: --disable-error-code used-before-def
import a
[file a.py]
from typing import List
from b import D
def f(x: B) -> List[B]: ...
B = List[C]
class C(B): pass
[file b.py]
from a import f
class D: ...
reveal_type(f) # N: Revealed type is "def (x: builtins.list[a.C]) -> builtins.list[builtins.list[a.C]]"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasToNotReadyMixed]
from typing import List, Union
x: A
class B(List[A]): pass
class C(List[A]): pass
A = Union[B, C]
reveal_type(x) # N: Revealed type is "Union[__main__.B, __main__.C]"
reveal_type(x[0]) # N: Revealed type is "Union[__main__.B, __main__.C]"
[builtins fixtures/list.pyi]
[case testNewAnalyzerTrickyAliasInFuncDef]
import a
[file a.py]
from b import B
def func() -> B: ...
reveal_type(func()) # N: Revealed type is "builtins.list[Tuple[b.C, b.C]]"
[file b.py]
from typing import List, Tuple
from a import func
class A: ...
class C(A): ...
B = List[Tuple[C, C]]
[builtins fixtures/list.pyi]
[case testNewAnalyzerListComprehension]
from typing import List
class A: pass
class B: pass
a: List[A]
a = [x for x in a]
b: List[B] = [x for x in a] # E: List comprehension has incompatible type List[A]; expected List[B]
[builtins fixtures/for.pyi]
[case testNewAnalyzerDictionaryComprehension]
from typing import Dict, List, Tuple
abd: Dict[A, B]
abl: List[Tuple[A, B]]
abd = {a: b for a, b in abl}
x: Dict[B, A] = {a: b for a, b in abl} # E: Key expression in dictionary comprehension has incompatible type "A"; expected type "B" \
# E: Value expression in dictionary comprehension has incompatible type "B"; expected type "A"
y: A = {a: b for a, b in abl} # E: Incompatible types in assignment (expression has type "Dict[A, B]", variable has type "A")
class A: pass
class B: pass
[builtins fixtures/dict.pyi]
[case testNewAnalyzerTypeArgBoundCheck]
from typing import TypeVar, Generic
class F(E): pass
class E: pass
T = TypeVar('T', bound=E)
class C(Generic[T]): pass
class D(B): pass
x: C[D] # E: Type argument "D" of "C" must be a subtype of "E"
y: C[F]
class B: pass
[case testNewAnalyzerTypeArgValueRestriction]
from typing import TypeVar, Generic
class F(E): pass
class E: pass
T = TypeVar('T', E, str)
class C(Generic[T]): pass
class D(B): pass
x: C[D] # E: Value of type variable "T" of "C" cannot be "D"
y: C[E]
z: C[str]
class B: pass
[case testNewAnalyzerTypeArgBoundCheckWithContext]
# flags: --show-error-context
import a
[file a.py]
from typing import TypeVar, Generic
T = TypeVar('T', bound=int)
class C(Generic[T]): pass
def f(x: C[str]) -> None: # E
y: C[str] # E
class A(C[str]): # E
z: C[str] # E
def g(self, x: C[str]) -> None: # E
a: C[str] # E
[out]
main:2: note: In module imported here:
tmp/a.py: note: In function "f":
tmp/a.py:6: error: Type argument "str" of "C" must be a subtype of "int"
tmp/a.py:7: error: Type argument "str" of "C" must be a subtype of "int"
tmp/a.py: note: In class "A":
tmp/a.py:8: error: Type argument "str" of "C" must be a subtype of "int"
tmp/a.py:9: error: Type argument "str" of "C" must be a subtype of "int"
tmp/a.py: note: In member "g" of class "A":
tmp/a.py:10: error: Type argument "str" of "C" must be a subtype of "int"
tmp/a.py:11: error: Type argument "str" of "C" must be a subtype of "int"
[case testNewAnalyzerTypeArgBoundCheckDifferentNodes]
from typing import TypeVar, Generic, NamedTuple, NewType, Union, Any, cast, overload
from mypy_extensions import TypedDict
T = TypeVar('T', bound=int)
class C(Generic[T]): pass
class C2(Generic[T]): pass
A = C[str] # E: Value of type variable "T" of "C" cannot be "str" \
# E: Type argument "str" of "C" must be a subtype of "int"
B = Union[C[str], int] # E: Type argument "str" of "C" must be a subtype of "int"
S = TypeVar('S', bound=C[str]) # E: Type argument "str" of "C" must be a subtype of "int"
U = TypeVar('U', C[str], str) # E: Type argument "str" of "C" must be a subtype of "int"
N = NamedTuple('N', [
('x', C[str])]) # E: Type argument "str" of "C" must be a subtype of "int"
class N2(NamedTuple):
x: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
class TD(TypedDict):
x: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
class TD2(TD):
y: C2[str] # E: Type argument "str" of "C2" must be a subtype of "int"
NT = NewType('NT',
C[str]) # E: Type argument "str" of "C" must be a subtype of "int"
class D(
C[str]): # E: Type argument "str" of "C" must be a subtype of "int"
pass
TD3 = TypedDict('TD3', {'x': C[str]}) # E: Type argument "str" of "C" must be a subtype of "int"
a: Any
for i in a: # type: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
pass
with a as w: # type: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
pass
cast(C[str], a) # E: Type argument "str" of "C" must be a subtype of "int"
C[str]() # E: Value of type variable "T" of "C" cannot be "str"
def f(s: S, y: U) -> None: pass # No error here
@overload
def g(x: C[str]) -> int: ... # E: Type argument "str" of "C" must be a subtype of "int"
@overload
def g(x: int) -> int: ...
def g(x: Union[C[str], int]) -> int: # E: Type argument "str" of "C" must be a subtype of "int"
y: C[object] # E: Type argument "object" of "C" must be a subtype of "int"
return 0
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerTypeArgBoundCheckWithStrictOptional]
# flags: --config-file tmp/mypy.ini
import a
[file b.py]
from typing import TypeVar, Generic
x: C[None]
y: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
z: C[int]
T = TypeVar('T', bound=int)
class C(Generic[T]):
pass
[file a.py]
from b import C
x: C[None] # E: Type argument "None" of "C" must be a subtype of "int"
y: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
z: C[int]
[file mypy.ini]
\[mypy-a]
strict_optional = True
\[mypy-b]
strict_optional = False
[case testNewAnalyzerTypeArgBoundCheckWithStrictOptionalPyProjectTOML]
# flags: --config-file tmp/pyproject.toml
import a
[file b.py]
from typing import TypeVar, Generic
x: C[None]
y: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
z: C[int]
T = TypeVar('T', bound=int)
class C(Generic[T]):
pass
[file a.py]
from b import C
x: C[None] # E: Type argument "None" of "C" must be a subtype of "int"
y: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
z: C[int]
[file pyproject.toml]
\[[tool.mypy.overrides]]
module = 'a'
strict_optional = true
\[[tool.mypy.overrides]]
module = 'b'
strict_optional = false
[case testNewAnalyzerProperty]
class A:
@property
def x(self) -> B:
return 0 # E: Incompatible return value type (got "int", expected "B")
@property
def y(self) -> B:
pass
@y.setter
def y(self, x: B) -> None:
pass
class B: pass
a = A()
reveal_type(a.x) # N: Revealed type is "__main__.B"
a.y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "B")
[builtins fixtures/property.pyi]
[case testNewAnalyzerAliasesFixedFew]
from typing import List, Generic, TypeVar
T = TypeVar('T')
class C(Generic[T]):
...
A = List[C]
x: A
def func(x: List[C[T]]) -> T:
...
reveal_type(x) # N: Revealed type is "builtins.list[__main__.C[Any]]"
reveal_type(func(x)) # N: Revealed type is "Any"
[builtins fixtures/list.pyi]
[case testNewAnalyzerAliasesFixedMany]
from typing import List, Generic, TypeVar
T = TypeVar('T')
class C(Generic[T]):
...
def func(x: List[C[T]]) -> T:
...
x: A
A = List[C[int, str]] # E: "C" expects 1 type argument, but 2 given
reveal_type(x) # N: Revealed type is "builtins.list[__main__.C[Any]]"
reveal_type(func(x)) # N: Revealed type is "Any"
[builtins fixtures/list.pyi]
[case testNewAnalyzerBuiltinAliasesFixed]
from typing import List, Optional
x: Optional[List] = None
y: List[str]
reveal_type(x) # N: Revealed type is "Union[builtins.list[Any], None]"
x = ['a', 'b']
reveal_type(x) # N: Revealed type is "builtins.list[Any]"
x.extend(y)
[builtins fixtures/list.pyi]
[case testNewAnalyzerImportPriosB]
import b
[file a.py]
from b import x
reveal_type(x) # N: Revealed type is "Tuple[builtins.int, builtins.int]"
[file b.py]
import a
x = (1, 2)
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerImportPriosA]
import a
[file a.py]
from b import x
reveal_type(x) # N: Revealed type is "Tuple[builtins.int, builtins.int]"
[file b.py]
import a
x = (1, 2)
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerConditionalFunc]
if int():
def f(x: int) -> None:
pass
def g(x: int) -> None:
pass
elif bool():
def f(x: int) -> None:
1() # E: "int" not callable
def g(x: str) -> None: # E: All conditional function variants must have identical signatures \
# N: Original: \
# N: def g(x: int) -> None \
# N: Redefinition: \
# N: def g(x: str) -> None
pass
else:
def f(x: int) -> None:
''() # E: "str" not callable
reveal_type(g) # N: Revealed type is "def (x: builtins.int)"
[case testNewAnalyzerConditionalFuncDefer]
if int():
def f(x: A) -> None:
pass
def g(x: A) -> None:
pass
else:
def f(x: A) -> None:
1() # E: "int" not callable
def g(x: str) -> None: # E: All conditional function variants must have identical signatures \
# N: Original: \
# N: def g(x: A) -> None \
# N: Redefinition: \
# N: def g(x: str) -> None
pass
reveal_type(g) # N: Revealed type is "def (x: __main__.A)"
class A: pass
[case testNewAnalyzerConditionalDecoratedFunc]
from typing import Callable
def dec(f: Callable[[int], None]) -> Callable[[str], None]:
pass
if int():
from m import f
else:
@dec
def f(x: int) -> None:
1() # E: "int" not callable
reveal_type(f) # N: Revealed type is "def (builtins.str)"
[file m.py]
def f(x: str, /) -> None: pass
[case testNewAnalyzerConditionallyDefineFuncOverVar]
from typing import Callable
if int():
f: Callable[[str], None]
else:
def f(x: str) -> None:
...
reveal_type(f) # N: Revealed type is "def (builtins.str)"
[case testNewAnalyzerConditionallyDefineFuncOverClass]
class C:
1() # E: "int" not callable
def C() -> None: # E: Name "C" already defined on line 1
''() # E: "str" not callable
[case testNewAnalyzerTupleIteration]
from typing import Union, Tuple, NamedTuple
class T(Tuple[B, C]):
pass
class A: pass
class B(A): pass
class C(A): pass
class NTInt(NamedTuple):
x: int
y: int
class NTStr(NamedTuple):
x: str
y: str
t1: T
reveal_type(t1.__iter__) # N: Revealed type is "def () -> typing.Iterator[Union[__main__.B, __main__.C]]"
t2: NTInt
reveal_type(t2.__iter__) # N: Revealed type is "def () -> typing.Iterator[builtins.int]"
nt: Union[NTInt, NTStr]
reveal_type(nt.__iter__) # N: Revealed type is "Union[def () -> typing.Iterator[builtins.int], def () -> typing.Iterator[builtins.str]]"
for nx in nt:
reveal_type(nx) # N: Revealed type is "Union[builtins.int, builtins.str]"
t: Union[Tuple[int, int], Tuple[str, str]]
for x in t:
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
[builtins fixtures/for.pyi]
[case testNewAnalyzerFallbackUpperBoundCheckAndFallbacks]
from typing import TypeVar, Generic, Tuple
class A: pass
class B: pass
class C(B): pass
S = TypeVar('S', bound='Tuple[G[A], ...]')
class GG(Generic[S]): pass
g: GG[Tuple[G[B], G[C]]] # E: Type argument "Tuple[G[B], G[C]]" of "GG" must be a subtype of "Tuple[G[A], ...]" \
# E: Type argument "B" of "G" must be a subtype of "A" \
# E: Type argument "C" of "G" must be a subtype of "A"
T = TypeVar('T', bound=A, covariant=True)
class G(Generic[T]): pass
t: Tuple[G[B], G[C]] # E: Type argument "B" of "G" must be a subtype of "A" \
# E: Type argument "C" of "G" must be a subtype of "A"
reveal_type(t.__iter__) # N: Revealed type is "def () -> typing.Iterator[__main__.G[__main__.B]]"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerClassKeywordsForward]
class C(B, other=A): ...
class B: ...
class A: ...
[case testNewAnalyzerClassKeywordsCyclic]
from typing import List
class C(List[C], other=C): ...
[builtins fixtures/list.pyi]
[case testNewAnalyzerClassKeywordsError]
class C(other=asdf): ... # E: Name "asdf" is not defined
[case testNewAnalyzerMissingImport]
# flags: --ignore-missing-imports
import non_existing
x: C
class C: ...
[case testNewAnalyzerMissingImportFrom]
# flags: --ignore-missing-imports
from non_existing import stuff
x: C
class C: ...
[case testNewAnalyzerFollowSkip]
# flags: --follow-imports=skip
from other import y
x: C
class C: ...
[file other.py]
y = 1
[case testNewAnalyzerMissingImportErrors]
# flags: --ignore-missing-imports
from non_existing import stuff, other_stuff
stuff = 1 # OK
other_stuff: int = 1 # E: Name "other_stuff" already defined (possibly by an import)
x: C
class C: ...
[case testNewAnalyzerMissingImportErrorsRedefinition]
# flags: --ignore-missing-imports
class Other: ...
from non_existing import Other # E: Name "Other" already defined on line 3
from non_existing import Cls
class Cls: ... # E: Name "Cls" already defined (possibly by an import)
x: C
class C: ...
[case testNewAnalyzerTupleInit]
from typing import Tuple
c: C
class C(Tuple[int, str]):
def __init__(self) -> None: pass
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNotAnAlias]
class Meta(type):
x = int()
class C(metaclass=Meta):
pass
y = C.x
reveal_type(y) # N: Revealed type is "builtins.int"
[case testNewAnalyzerFunctionError]
def f(x: asdf) -> None: # E: Name "asdf" is not defined
pass
[case testNewAnalyzerEnumRedefinition]
from enum import Enum
A = Enum('A', ['x', 'y'])
A = Enum('A', ['z', 't']) # E: Name "A" already defined on line 3
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerNewTypeRedefinition]
from typing import NewType
A = NewType('A', int)
A = NewType('A', str) # E: Cannot redefine "A" as a NewType \
# E: Name "A" already defined on line 3
[case testNewAnalyzerNewTypeForwardClass]
from typing import NewType, List
x: C
reveal_type(x[0]) # N: Revealed type is "__main__.C"
C = NewType('C', 'B')
class B(List[C]):
pass
[builtins fixtures/list.pyi]
[case testNewAnalyzerNewTypeForwardClassAlias]
from typing import NewType, List
x: D
reveal_type(x[0]) # N: Revealed type is "__main__.C"
C = NewType('C', 'B')
D = C
class B(List[D]):
pass
[builtins fixtures/list.pyi]
[case testNewAnalyzerNewTypeForwardClassAliasReversed]
from typing import NewType, List
x: D
reveal_type(x[0][0]) # N: Revealed type is "__main__.C"
D = C # E: Name "C" is used before definition
C = NewType('C', 'List[B]')
class B(List[C]):
pass
[builtins fixtures/list.pyi]
[case testNewAnalyzerNewTypeForwardClassAliasDirect]
# flags: --disable-error-code used-before-def
from typing import NewType, List
def test() -> None:
x: D
reveal_type(x[0][0])
D = List[C]
C = NewType('C', 'B')
class B(D):
pass
[builtins fixtures/list.pyi]
[out]
main:5: error: Cannot resolve name "D" (possible cyclic definition)
main:5: note: Recursive types are not allowed at function scope
main:6: note: Revealed type is "Any"
main:8: error: Cannot resolve name "D" (possible cyclic definition)
main:8: note: Recursive types are not allowed at function scope
main:8: error: Cannot resolve name "C" (possible cyclic definition)
main:9: error: Argument 2 to NewType(...) must be a valid type
main:9: error: Cannot resolve name "B" (possible cyclic definition)
main:9: note: Recursive types are not allowed at function scope
-- Copied from check-classes.test (tricky corner cases).
[case testNewAnalyzerNoCrashForwardRefToBrokenDoubleNewTypeClass]
from typing import Any, Dict, List, NewType
Foo = NewType('NotFoo', int) # type: ignore
Foos = NewType('Foos', List[Foo])
x: C
class C:
def frob(self, foos: Dict[Any, Foos]) -> None:
foo = foos.get(1)
assert foo
dict(foo)
[builtins fixtures/dict.pyi]
[case testNewAnalyzerForwardTypeAliasInBase]
from typing import List, Generic, TypeVar, NamedTuple
T = TypeVar('T')
def test() -> None:
class C(A, B): # E: Cannot resolve name "A" (possible cyclic definition) \
# N: Recursive types are not allowed at function scope
pass
class G(Generic[T]): pass
A = G[C] # E: Cannot resolve name "A" (possible cyclic definition) \
# N: Recursive types are not allowed at function scope
class B(NamedTuple):
x: int
y: C
reveal_type(y.x) # N: Revealed type is "builtins.int"
reveal_type(y[0]) # N: Revealed type is "builtins.int"
x: A
reveal_type(x) # N: Revealed type is "__main__.G@7[Tuple[builtins.int, fallback=__main__.C@5]]"
[builtins fixtures/list.pyi]
[case testNewAnalyzerDuplicateTypeVar]
from typing import TypeVar, Generic, Any
T = TypeVar('T', bound='B[Any]')
# The "int" error is because of typing fixture.
T = TypeVar('T', bound='C') # E: Cannot redefine "T" as a type variable \
# E: Invalid assignment target
class B(Generic[T]):
x: T
class C: ...
x: B[int] # E: Type argument "int" of "B" must be a subtype of "B[Any]"
y: B[B[Any]]
reveal_type(y.x) # N: Revealed type is "__main__.B[Any]"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]
[case testNewAnalyzerDuplicateTypeVarImportCycle]
# flags: --disable-error-code used-before-def
import a
[file a.py]
from typing import TypeVar, Any
from b import B, C
T = TypeVar('T', bound=B[Any])
T = TypeVar('T', bound=C)
[file b.py]
from typing import Generic, Any
from a import T
class B(Generic[T]):
x: T
class C: ...
x: B[int]
y: B[B[Any]]
reveal_type(y.x)
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]
[out]
tmp/b.py:8: error: Type argument "int" of "B" must be a subtype of "B[Any]"
tmp/b.py:10: note: Revealed type is "b.B[Any]"
tmp/a.py:5: error: Cannot redefine "T" as a type variable
tmp/a.py:5: error: Invalid assignment target
[case testNewAnalyzerDuplicateTypeVarImportCycleWithAliases]
# flags: --disable-error-code used-before-def
import a
[file a.py]
from typing import TypeVar, Any
from b import BA, C
T = TypeVar('T', bound=BAA[Any])
T = TypeVar('T', bound=C)
BAA = BA
[file b.py]
from typing import Generic, Any
from a import T
BA = B
class B(Generic[T]):
x: T
class C: ...
x: B[int]
y: B[B[Any]]
reveal_type(y.x)
[out]
tmp/b.py:9: error: Type argument "int" of "B" must be a subtype of "B[Any]"
tmp/b.py:11: note: Revealed type is "b.B[Any]"
tmp/a.py:5: error: Cannot redefine "T" as a type variable
tmp/a.py:5: error: Invalid assignment target
[case testNewAnalyzerTypeVarBoundInCycle]
import factory, box
[file factory.py]
from typing import Generic, Type
from box import BoxT
class Factory(Generic[BoxT]):
value: int
def create(self, boxClass: Type[BoxT]) -> BoxT:
reveal_type(boxClass.create(self)) # N: Revealed type is "BoxT`1"
return boxClass.create(self)
[file box.py]
from typing import TYPE_CHECKING, Type, TypeVar
if TYPE_CHECKING:
from factory import Factory
BoxT = TypeVar('BoxT', bound='Box')
class Box:
@classmethod
def create(cls: Type[BoxT], f: Factory) -> BoxT:
return cls(f.value)
def __init__(self, value: int) -> None: ...
[builtins fixtures/classmethod.pyi]
[typing fixtures/typing-medium.pyi]
[case testNewAnalyzerCastForward1]
from typing import cast
x = cast('C', None)
class A:
def foo(self) -> None:
self.x = cast('C', None)
reveal_type(x) # N: Revealed type is "__main__.C"
reveal_type(A().x) # N: Revealed type is "__main__.C"
class C(A): ...
[case testNewAnalyzerCastForward2]
from typing import cast
x = cast('C', None)
reveal_type(x) # N: Revealed type is "builtins.int"
C = int
[case testNewAnalyzerCastForward3]
from typing import cast, NamedTuple
x = cast('C', None)
reveal_type(x) # N: Revealed type is "Tuple[builtins.int, fallback=__main__.C]"
reveal_type(x.x) # N: Revealed type is "builtins.int"
C = NamedTuple('C', [('x', int)])
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerApplicationForward1]
# flags: --disable-error-code used-before-def
from typing import Generic, TypeVar
x = C[int]()
reveal_type(x) # N: Revealed type is "__main__.C[builtins.int]"
T = TypeVar('T')
class C(Generic[T]): ...
[case testNewAnalyzerApplicationForward2]
from typing import Generic, TypeVar
T = TypeVar('T')
class C(Generic[T]): ...
x = C['A']()
reveal_type(x) # N: Revealed type is "__main__.C[__main__.A]"
class A: ...
[case testNewAnalyzerApplicationForward3]
from typing import Generic, TypeVar
class A: ...
T = TypeVar('T')
class C(Generic[T]): ...
x = C[A]()
reveal_type(x) # N: Revealed type is "__main__.C[__main__.A]"
[case testNewAnalyzerApplicationForward4]
# flags: --disable-error-code used-before-def
from typing import Generic, TypeVar
x = C[A]() # E: Value of type variable "T" of "C" cannot be "A"
reveal_type(x) # N: Revealed type is "__main__.C[__main__.A]"
T = TypeVar('T', bound='D')
class C(Generic[T]): ...
class A: ...
class D: ...
[case testNewAnalyzerAddedSubStarImport_incremental]
# TODO: This can be removed once testAddedSubStarImport is enabled in check-incremental.test.
# cmd: mypy -m a pack pack.mod b
# cmd2: mypy -m other
[file a.py]
from pack import *
[file pack/__init__.py]
[file pack/mod.py]
[file b.py]
import pack.mod
[file other.py]
import a
[out]
[out2]
[case testNewAnalyzerModuleGetattrSerialize_incremental]
import a
[file a.py]
import p
[file a.py.2]
import p
reveal_type(p.y)
[file p.pyi]
from pp import x
y = x
[file pp.pyi]
def __getattr__(attr): ...
[out2]
tmp/a.py:2: note: Revealed type is "Any"
[case testNewAnanlyzerTrickyImportPackage]
from lib import config
import lib
reveal_type(lib.config.x) # N: Revealed type is "builtins.int"
reveal_type(config.x) # N: Revealed type is "builtins.int"
[file lib/__init__.py]
from lib.config import config
[file lib/config.py]
class Config:
x: int
config = Config()
[builtins fixtures/module.pyi]
[case testNewAnanlyzerTrickyImportPackageAlt]
import lib.config
import lib.config as tmp
reveal_type(lib.config.x) # N: Revealed type is "builtins.int"
# TODO: this actually doesn't match runtime behavior, variable wins.
tmp.x # E: Module has no attribute "x"
[file lib/__init__.py]
from lib.config import config
[file lib/config.py]
class Config:
x: int
config = Config()
[builtins fixtures/module.pyi]
[case testNewAnanlyzerTrickyImportPackage_incremental]
import a
[file a.py]
from lib import config
import lib
[file a.py.2]
from lib import config
import lib
reveal_type(lib.config.x)
reveal_type(config.x)
[file lib/__init__.py]
from lib.config import config
[file lib/config.py]
class Config:
x: int
config = Config()
[builtins fixtures/module.pyi]
[out2]
tmp/a.py:4: note: Revealed type is "builtins.int"
tmp/a.py:5: note: Revealed type is "builtins.int"
[case testNewAnalyzerRedefineAsClass]
from typing import Any
from other import C # type: ignore
y = 'bad'
class C: # E: Name "C" already defined (possibly by an import)
def meth(self, other: int) -> None:
y() # E: "str" not callable
[case testNewAnalyzerRedefineAsOverload]
from typing import overload
y = 'bad'
if int():
def f(x: int) -> None:
pass
else:
@overload # E: Name "f" already defined on line 6
def f(x: int) -> None: ...
@overload
def f(x: str) -> None: ...
def f(x) -> None:
y() # E: "str" not callable
[case testNewAnalyzerFirstAliasTargetWins]
class DesiredTarget:
attr: int
if int():
Alias = DesiredTarget
else:
class DummyTarget:
pass
Alias = DummyTarget # type: ignore
x: Alias
reveal_type(x.attr) # N: Revealed type is "builtins.int"
[case testNewAnalyzerFirstVarDefinitionWins]
x = y # E: Name "y" is used before definition
x = 1
# We want to check that the first definition creates the variable.
def x() -> None: ... # E: Name "x" already defined on line 1
y = 2
[case testNewAnalyzerImportStarSpecialCase]
import unittest
[file unittest/__init__.pyi]
from unittest.suite import *
def load_tests() -> TestSuite: ...
[file unittest/suite.pyi]
from typing import Union # Iterable not imported
import unittest.case
_TestType = Union[unittest.case.TestCase]
class BaseTestSuite(Iterable[_TestType]):
...
class TestSuite(BaseTestSuite):
...
[file unittest/case.pyi]
class TestCase: ...
[out]
tmp/unittest/suite.pyi:6: error: Name "Iterable" is not defined
tmp/unittest/suite.pyi:6: note: Did you forget to import it from "typing"? (Suggestion: "from typing import Iterable")
[case testNewAnalyzerNewTypeSpecialCase]
from typing import NewType
from typing_extensions import Final, Literal
X = NewType('X', int)
var1: Final = 1
def force1(x: Literal[1]) -> None: pass
force1(reveal_type(var1)) # N: Revealed type is "Literal[1]"
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerReportLoopInMRO]
class A(A): ... # E: Cannot resolve name "A" (possible cyclic definition)
[out]
[case testNewSemanticAnalyzerUnimportedSpecialCase]
# flags: --ignore-missing-imports
import other
import p.u
[file p/__init__.py]
[file p/u.pyi]
from . import c
x: c.C
[file other.py]
from p.c import B
[out]
[case testNewSemanticAnalyzerQualifiedFunctionAsType]
import m
x: m.C.a.b # E: Name "m.C.a.b" is not defined
[file m.py]
def C(): pass
[case testNewSemanticAnalyzerModulePrivateRefInMiddleOfQualified]
import m
x: m.n.C # E: Name "m.n.C" is not defined
reveal_type(x) # N: Revealed type is "Any"
[file m.pyi]
import n
[file n.pyi]
class C: pass
[case testNewAnalyzerModuleGetAttr]
import m
import n
x: m.n.C
y: n.D
[file m.py]
import n
[file n.py]
def __getattr__(x): pass
[case testNewAnalyzerReportLoopInMRO2]
def f() -> None:
class A(A): ... # E: Cannot resolve name "A" (possible cyclic definition) \
# N: Recursive types are not allowed at function scope
[case testNewAnalyzerUnsupportedBaseClassInsideFunction]
class C:
class E: pass
def f(self) -> None:
# TODO: Error message could be better
class D(self.E): # E: Name "self.E" is not defined
pass
[case testNewAnalyzerShadowOuterDefinitionBasedOnOrderSinglePass]
# Only one semantic analysis pass
class X: pass
class C:
X = X
reveal_type(X) # N: Revealed type is "def () -> __main__.X"
reveal_type(C.X) # N: Revealed type is "def () -> __main__.X"
[case testNewAnalyzerShadowOuterDefinitionBasedOnOrderTwoPasses]
c: C # Force second semantic analysis pass
class X: pass
class C:
X = X
reveal_type(X) # N: Revealed type is "def () -> __main__.X"
reveal_type(C.X) # N: Revealed type is "def () -> __main__.X"
[case testNewAnalyzerAnnotationConflictsWithAttributeSinglePass]
class C:
def x(self) -> int:
return 0
def __init__(self) -> None:
self.int = ''
def y(self) -> int:
return 0
z: str
def str(self) -> str:
return 0 # E: Incompatible return value type (got "int", expected "str")
zz: str # E: Function "__main__.C.str" is not valid as a type \
# N: Perhaps you need "Callable[...]" or a callback protocol?
reveal_type(C().x()) # N: Revealed type is "builtins.int"
reveal_type(C().y()) # N: Revealed type is "builtins.int"
reveal_type(C().z) # N: Revealed type is "builtins.str"
reveal_type(C().str()) # N: Revealed type is "builtins.str"
[case testNewAnalyzerAnnotationConflictsWithAttributeTwoPasses]
c: C # Force second semantic analysis pass
class C:
def x(self) -> int:
return 0
def __init__(self) -> None:
self.int = ''
def y(self) -> int:
return 0
z: str
def str(self) -> str:
return 0 # E: Incompatible return value type (got "int", expected "str")
zz: str # E: Function "__main__.C.str" is not valid as a type \
# N: Perhaps you need "Callable[...]" or a callback protocol?
reveal_type(C().x()) # N: Revealed type is "builtins.int"
reveal_type(C().y()) # N: Revealed type is "builtins.int"
reveal_type(C().z) # N: Revealed type is "builtins.str"
reveal_type(C().str()) # N: Revealed type is "builtins.str"
[case testNewAnalyzerNameConflictsAndMultiLineDefinition]
c: C # Force second semantic analysis pass
class X: pass
class C:
X = (
X)
def str(self
) -> str:
return 0 # E: Incompatible return value type (got "int", expected "str")
reveal_type(C.X) # E: # N: Revealed type is "def () -> __main__.X"
reveal_type(C().str()) # N: Revealed type is "builtins.str"
[case testNewAnalyzerNameNotDefinedYetInClassBody]
class C:
X = Y # E: Name "Y" is not defined
Y = 1
f = g # E: Name "g" is not defined
def g(self) -> None: pass
reveal_type(C.X) # N: Revealed type is "Any"
[case testNewAnalyzerImportedNameUsedInClassBody]
import m
[file m.py]
class C:
from mm import f # E: Unsupported class scoped import
@dec(f)
def m(self): pass
def dec(f): pass
[file mm.py]
# 1 padding to increase line number of 'f'
# 2 padding
# 3 padding
# 4 padding
# 5 padding
# 6 padding
def f(): pass
[case testNewAnalyzerImportedNameUsedInClassBody2]
import m
[file m/__init__.py]
class C:
from m.m import f # E: Unsupported class scoped import
@dec(f)
def m(self): pass
def dec(f): pass
[file m/m.py]
# 1 padding to increase line number of 'f'
# 2 padding
# 3 padding
# 4 padding
# 5 padding
# 6 padding
def f(): pass
[case testNewAnalyzerOverrideClassWithTypeAlias]
from typing import Generic, TypeVar
T = TypeVar('T')
class C(Generic[T]):
pass
C = C[int] # E: Cannot assign to a type \
# E: Incompatible types in assignment (expression has type "Type[C[int]]", variable has type "Type[C[T]]")
x: C
reveal_type(x) # N: Revealed type is "__main__.C[Any]"
[case testNewAnalyzerClassVariableOrdering]
def foo(x: str) -> None: pass
class Something:
def run(self) -> None:
foo(self.IDS[0]) # E: Argument 1 to "foo" has incompatible type "int"; expected "str"
IDS = [87]
[builtins fixtures/list.pyi]
[case testNewAnalyzerPlaceholderFromOuterScope]
import b
[file a.py]
import b
class A(B): ...
class B: ...
[file b.py]
from a import A
class C:
A = A # Initially rvalue will be a placeholder
reveal_type(C.A) # N: Revealed type is "def () -> a.A"
[case testNewAnalyzerFinalLiteralInferredAsLiteralWithDeferral]
from typing_extensions import Final, Literal
defer: Yes
var: Final = 42
def force(x: Literal[42]) -> None: pass
force(reveal_type(var)) # N: Revealed type is "Literal[42]"
class Yes: ...
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerImportCycleWithIgnoreMissingImports]
# flags: --ignore-missing-imports
import p
reveal_type(p.get) # N: Revealed type is "def () -> builtins.int"
[file p/__init__.pyi]
from . import api
get = api.get
[file p/api.pyi]
import p
def get() -> int: ...
[case testUseObsoleteNameForTypeVar3]
import typing
t = typing.typevar('t') # E: Module has no attribute "typevar"
[builtins fixtures/module.pyi]
[case testNewAnalyzerImportFromTopLevelFunction]
import a.b # This works at runtime
reveal_type(a.b) # N
[file a/__init__.py]
from .b import B
from . import b as c
def b() -> None: pass
reveal_type(b) # N
reveal_type(c.B()) # N
x: Forward
class Forward:
...
[file a/b.py]
class B: ...
[builtins fixtures/module.pyi]
[out]
tmp/a/__init__.py:4: note: Revealed type is "def ()"
tmp/a/__init__.py:5: note: Revealed type is "a.b.B"
main:2: note: Revealed type is "def ()"
[case testNewAnalyzerImportFromTopLevelAlias]
import a.b # This works at runtime
reveal_type(a.b) # N
[file a/__init__.py]
from .b import B
from . import b as c
b = int
y: b
reveal_type(y) # N
reveal_type(c.B) # N
x: Forward
class Forward:
...
[file a/b.py]
class B: ...
[builtins fixtures/module.pyi]
[out]
tmp/a/__init__.py:5: note: Revealed type is "builtins.int"
tmp/a/__init__.py:6: note: Revealed type is "def () -> a.b.B"
main:2: note: Revealed type is "def () -> builtins.int"
[case testNewAnalyzerImportAmbiguousWithTopLevelFunction]
import a.b # This works at runtime
x: a.b.B # E
reveal_type(a.b) # N
[file a/__init__.py]
import a.b
import a.b as c
def b() -> None: pass
reveal_type(b) # N
reveal_type(c.B()) # N
x: Forward
class Forward:
...
[file a/b.py]
class B: ...
[builtins fixtures/module.pyi]
[out]
tmp/a/__init__.py:4: note: Revealed type is "def ()"
tmp/a/__init__.py:5: note: Revealed type is "a.b.B"
main:2: error: Name "a.b.B" is not defined
main:3: note: Revealed type is "def ()"
[case testNewAnalyzerConfusingImportConflictingNames]
# flags: --follow-imports=skip --ignore-missing-imports
# cmd: mypy -m other a.b a
[file a/__init__.py]
[file a/b/__init__.py]
import other
import a.b.a
import a.b.c
[file other.py]
from a.b.a import foo
[builtins fixtures/module.pyi]
[out]
[case testNewAnalyzerNamedTupleMethod]
from typing import NamedTuple
g: N
class N(NamedTuple):
def f(self) -> None:
b = (
a
for a in [1]
)
b
[builtins fixtures/tuple.pyi]
[case testWithMultipleTargetsDeferred]
a: A
class A:
def __enter__(self) -> int: pass
def __exit__(self, x, y, z): pass
with A() as x, A() as y: # type: int, int
pass
[case testNewAnalyzerLessErrorsNeedAnnotation]
from typing import TypeVar, Optional
T = TypeVar('T')
def f(x: Optional[T] = None) -> T: ...
x = f() # E: Need type annotation for "x"
y = x
def g() -> None:
x = f() # E: Need type annotation for "x"
y = x
[case testNewAnalyzerLessErrorsNeedAnnotationList]
x = [] # type: ignore
reveal_type(x) # N: Revealed type is "builtins.list[Any]"
def g() -> None:
x = [] # type: ignore
reveal_type(x) # N: Revealed type is "builtins.list[Any]"
[builtins fixtures/list.pyi]
[case testNewAnalyzerLessErrorsNeedAnnotationNested]
from typing import TypeVar, Optional, Generic
T = TypeVar('T')
class G(Generic[T]): ...
def f(x: Optional[T] = None) -> G[T]: ...
x = f() # E: Need type annotation for "x"
y = x
reveal_type(y) # N: Revealed type is "__main__.G[Any]"
def g() -> None:
x = f() # E: Need type annotation for "x"
y = x
reveal_type(y) # N: Revealed type is "__main__.G[Any]"
[case testNewAnalyzerRedefinedNonlocal]
# flags: --disable-error-code=annotation-unchecked
import typing
def f():
bar = [] # type: typing.List[int]
def foo():
nonlocal bar
bar = [] # type: typing.List[int]
def g() -> None:
bar = [] # type: typing.List[int]
def foo() -> None:
nonlocal bar
bar = [] # type: typing.List[int] # E: Name "bar" already defined on line 12
[builtins fixtures/list.pyi]
[case testNewAnalyzerMoreInvalidTypeVarArgumentsDeferred]
from typing import TypeVar, Generic
defer: Yes
S = TypeVar('S', covariant=True, contravariant=True) # E: TypeVar cannot be both covariant and contravariant \
# E: "int" not callable
class Yes: ...
[builtins fixtures/bool.pyi]
[case testNewAnalyzerDisallowAnyGenericsMessages]
# mypy: disallow-any-generics
from a import B
x: B
[file a.py]
from typing import TypeVar, List
T = TypeVar('T')
A = List[T]
B = A
[builtins fixtures/list.pyi]
[case testNewAnalyzerVarTypeVarNoCrash]
from typing import Callable, TypeVar
FooT = TypeVar('FooT', bound='Foo')
class Foo: ...
f = lambda x: True # type: Callable[[FooT], bool]
reveal_type(f) # N: Revealed type is "def [FooT <: __main__.Foo] (FooT`-1) -> builtins.bool"
[builtins fixtures/bool.pyi]
[case testNewAnalyzerVarTypeVarNoCrashImportCycle]
import a
[file a.py]
from b import B
from typing import TypeVar
FooT = TypeVar('FooT', bound='Foo')
class Foo: ...
[file b.py]
from a import FooT
from typing import Callable
f = lambda x: True # type: Callable[[FooT], bool]
reveal_type(f) # N: Revealed type is "def [FooT <: a.Foo] (FooT`-1) -> builtins.bool"
class B: ...
[builtins fixtures/bool.pyi]
[case testNewAnalyzerFuncTypeVarNoCrashImportCycle]
import a
[file a.py]
from b import B
from typing import TypeVar
FooT = TypeVar('FooT', bound='Foo')
class Foo: ...
[file b.py]
from a import FooT
from typing import Callable
def f(x: FooT) -> bool: ...
reveal_type(f) # N: Revealed type is "def [FooT <: a.Foo] (x: FooT`-1) -> builtins.bool"
class B: ...
[builtins fixtures/bool.pyi]
[case testNewAnalyzerNoCrashOnStarInference]
from typing import Tuple
def f() -> None:
t: Tuple[str, Tuple[str, str, str]]
x, (y, *z) = t
reveal_type(z) # N: Revealed type is "builtins.list[builtins.str]"
[builtins fixtures/list.pyi]
[case testNewAnalyzerIdentityAssignment1]
from foo import *
try:
X = X
except:
class X: # E: Name "X" already defined (possibly by an import)
pass
reveal_type(X()) # N: Revealed type is "foo.X"
[file foo.py]
class X: pass
[case testNewAnalyzerIdentityAssignment2]
try:
int = int
reveal_type(int()) # N: Revealed type is "builtins.int"
except:
class int: # E: Name "int" already defined (possibly by an import)
pass
reveal_type(int()) # N: Revealed type is "builtins.int"
[case testNewAnalyzerIdentityAssignment3]
forwardref: C
try:
int = int
reveal_type(int()) # N: Revealed type is "builtins.int"
except:
class int: # E: Name "int" already defined (possibly by an import)
pass
reveal_type(int()) # N: Revealed type is "builtins.int"
class C: pass
[case testNewAnalyzerIdentityAssignment4]
try:
C = C
C
except:
class C:
pass
reveal_type(C()) # N: Revealed type is "__main__.C"
[case testNewAnalyzerIdentityAssignment5]
forwardref: D
try:
C = C
C
except:
class C:
pass
class D: pass
reveal_type(C()) # N: Revealed type is "__main__.C"
[case testNewAnalyzerIdentityAssignment6]
x: C
class C:
pass
C = C
reveal_type(C()) # N: Revealed type is "__main__.C"
reveal_type(x) # N: Revealed type is "__main__.C"
[case testNewAnalyzerIdentityAssignment7]
C = C # E: Name "C" is not defined
reveal_type(C) # E: Name "C" is not defined \
# N: Revealed type is "Any"
[case testNewAnalyzerIdentityAssignment8]
from typing import Final
x: Final = 0
x = x # E: Cannot assign to final name "x"
[case testNewAnalyzerClassPropertiesInAllScopes]
from abc import abstractmethod, ABCMeta
class TopLevel(metaclass=ABCMeta):
@abstractmethod
def f(self) -> None: pass
TopLevel() # E: Cannot instantiate abstract class "TopLevel" with abstract attribute "f"
def func() -> None:
class Function(metaclass=ABCMeta):
@abstractmethod
def f(self) -> None: pass
Function() # E: Cannot instantiate abstract class "Function" with abstract attribute "f"
class C:
def meth(self) -> None:
class Method(metaclass=ABCMeta):
@abstractmethod
def f(self) -> None: pass
Method() # E: Cannot instantiate abstract class "Method" with abstract attribute "f"
[case testModulesAndFuncsTargetsInCycle]
import a
[file a.py]
import b
defer: Yes
def func() -> int: ...
class Yes: ...
[file b.py]
import a
def func() -> int: ...
[targets b, a, a, b.func, a.func, __main__]
[case testNewAnalyzerForwardReferenceInFunction]
def f(x: 'A') -> 'A':
return A()
class A:
pass
[targets __main__, __main__.f]
[case testNewAnalyzerSimpleImportStarNoDeferral]
from m import *
x: A
f()
[file m.py]
class A: pass
def f() -> None: pass
[targets m, m.f, __main__]
[case testNewAnalyzerNoCrashOnCustomProperty]
# flags: --ignore-missing-imports
from unimported import custom
class User:
first_name: str
@custom
def name(self) -> str:
return self.first_name
@name.setter # type: ignore
def name(self, value: str) -> None:
self.first_name = value
def __init__(self, name: str) -> None:
self.name = name # E: Cannot assign to a method
[case testNewAnalyzerMemberNameMatchesTypedDict]
from typing import Union, Any
from typing_extensions import TypedDict
class T(TypedDict):
b: b.T
class b:
T: Union[Any]
[builtins fixtures/tuple.pyi]
[case testNewAnalyzerMemberNameMatchesNamedTuple]
from typing import Union, Any, NamedTuple
class T(NamedTuple):
b: b.T
class b:
T = Union[Any]
[builtins fixtures/tuple.pyi]
[case testSelfReferentialSubscriptExpression]
x = x[1] # E: Cannot resolve name "x" (possible cyclic definition)
y = 1[y] # E: Value of type "int" is not indexable \
# E: Cannot determine type of "y"
|