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
|
[case testGetAttribute]
class A:
x: int
def f(a: A) -> int:
return a.x
[out]
def f(a):
a :: __main__.A
r0 :: int
L0:
r0 = a.x
return r0
[case testSetAttribute]
class A:
x: int
def f(a: A) -> None:
a.x = 1
[out]
def f(a):
a :: __main__.A
r0 :: bool
L0:
a.x = 2; r0 = is_error
return 1
[case testUserClassInList]
class C:
x: int
def f() -> int:
c = C()
c.x = 5
a = [c]
d = a[0]
return d.x + 1
[out]
def f():
r0, c :: __main__.C
r1 :: bool
r2 :: list
r3 :: ptr
a :: list
r4 :: object
r5, d :: __main__.C
r6, r7 :: int
L0:
r0 = C()
c = r0
c.x = 10; r1 = is_error
r2 = PyList_New(1)
r3 = list_items r2
buf_init_item r3, 0, c
keep_alive r2
a = r2
r4 = CPyList_GetItemShort(a, 0)
r5 = cast(__main__.C, r4)
d = r5
r6 = borrow d.x
r7 = CPyTagged_Add(r6, 2)
keep_alive d
return r7
[case testMethodCall]
class A:
def f(self, x: int, y: str) -> int:
return x + 10
def g(a: A) -> None:
a.f(1, 'hi')
[out]
def A.f(self, x, y):
self :: __main__.A
x :: int
y :: str
r0 :: int
L0:
r0 = CPyTagged_Add(x, 20)
return r0
def g(a):
a :: __main__.A
r0 :: str
r1 :: int
L0:
r0 = 'hi'
r1 = a.f(2, r0)
return 1
[case testForwardUse]
def g(a: A) -> int:
return a.n
class A:
n : int
[out]
def g(a):
a :: __main__.A
r0 :: int
L0:
r0 = a.n
return r0
[case testOptionalMember]
from typing import Optional
class Node:
next: Optional[Node]
def length(self) -> int:
if self.next is not None:
return 1 + self.next.length()
return 1
[out]
def Node.length(self):
self :: __main__.Node
r0 :: union[__main__.Node, None]
r1 :: object
r2 :: bit
r3 :: union[__main__.Node, None]
r4 :: __main__.Node
r5, r6 :: int
L0:
r0 = borrow self.next
r1 = load_address _Py_NoneStruct
r2 = r0 != r1
keep_alive self
if r2 goto L1 else goto L2 :: bool
L1:
r3 = self.next
r4 = cast(__main__.Node, r3)
r5 = r4.length()
r6 = CPyTagged_Add(2, r5)
return r6
L2:
return 2
[case testSubclass]
class A:
def __init__(self) -> None:
self.x = 10
class B(A):
def __init__(self) -> None:
self.x = 20
self.y = 30
[out]
def A.__init__(self):
self :: __main__.A
L0:
self.x = 20
return 1
def B.__init__(self):
self :: __main__.B
L0:
self.x = 40
self.y = 60
return 1
[case testAttrLvalue]
class O(object):
def __init__(self) -> None:
self.x = 1
def increment(o: O) -> O:
o.x += 1
return o
[out]
def O.__init__(self):
self :: __main__.O
L0:
self.x = 2
return 1
def increment(o):
o :: __main__.O
r0, r1 :: int
r2 :: bool
L0:
r0 = borrow o.x
r1 = CPyTagged_Add(r0, 2)
o.x = r1; r2 = is_error
return o
[case testSubclass_withgil_toplevel]
from typing import TypeVar, Generic
from mypy_extensions import trait
T = TypeVar('T')
class C:
pass
@trait
class S:
pass
class D(C, S, Generic[T]):
pass
[out]
def __top_level__():
r0, r1 :: object
r2 :: bit
r3 :: str
r4, r5 :: object
r6 :: str
r7 :: dict
r8, r9 :: object
r10 :: str
r11 :: dict
r12 :: object
r13 :: str
r14 :: dict
r15 :: str
r16 :: object
r17 :: object[1]
r18 :: object_ptr
r19 :: object
r20 :: dict
r21 :: str
r22 :: i32
r23 :: bit
r24 :: object
r25 :: str
r26, r27 :: object
r28 :: bool
r29 :: str
r30 :: tuple
r31 :: i32
r32 :: bit
r33 :: dict
r34 :: str
r35 :: i32
r36 :: bit
r37 :: object
r38 :: str
r39, r40 :: object
r41 :: str
r42 :: tuple
r43 :: i32
r44 :: bit
r45 :: dict
r46 :: str
r47 :: i32
r48 :: bit
r49, r50 :: object
r51 :: dict
r52 :: str
r53 :: object
r54 :: dict
r55 :: str
r56, r57 :: object
r58 :: tuple
r59 :: str
r60, r61 :: object
r62 :: bool
r63, r64 :: str
r65 :: tuple
r66 :: i32
r67 :: bit
r68 :: dict
r69 :: str
r70 :: i32
r71 :: bit
L0:
r0 = builtins :: module
r1 = load_address _Py_NoneStruct
r2 = r0 != r1
if r2 goto L2 else goto L1 :: bool
L1:
r3 = 'builtins'
r4 = PyImport_Import(r3)
builtins = r4 :: module
L2:
r5 = ('TypeVar', 'Generic')
r6 = 'typing'
r7 = __main__.globals :: static
r8 = CPyImport_ImportFromMany(r6, r5, r5, r7)
typing = r8 :: module
r9 = ('trait',)
r10 = 'mypy_extensions'
r11 = __main__.globals :: static
r12 = CPyImport_ImportFromMany(r10, r9, r9, r11)
mypy_extensions = r12 :: module
r13 = 'T'
r14 = __main__.globals :: static
r15 = 'TypeVar'
r16 = CPyDict_GetItem(r14, r15)
r17 = [r13]
r18 = load_address r17
r19 = PyObject_Vectorcall(r16, r18, 1, 0)
keep_alive r13
r20 = __main__.globals :: static
r21 = 'T'
r22 = CPyDict_SetItem(r20, r21, r19)
r23 = r22 >= 0 :: signed
r24 = <error> :: object
r25 = '__main__'
r26 = __main__.C_template :: type
r27 = CPyType_FromTemplate(r26, r24, r25)
r28 = C_trait_vtable_setup()
r29 = '__mypyc_attrs__'
r30 = CPyTuple_LoadEmptyTupleConstant()
r31 = PyObject_SetAttr(r27, r29, r30)
r32 = r31 >= 0 :: signed
__main__.C = r27 :: type
r33 = __main__.globals :: static
r34 = 'C'
r35 = PyDict_SetItem(r33, r34, r27)
r36 = r35 >= 0 :: signed
r37 = <error> :: object
r38 = '__main__'
r39 = __main__.S_template :: type
r40 = CPyType_FromTemplate(r39, r37, r38)
r41 = '__mypyc_attrs__'
r42 = CPyTuple_LoadEmptyTupleConstant()
r43 = PyObject_SetAttr(r40, r41, r42)
r44 = r43 >= 0 :: signed
__main__.S = r40 :: type
r45 = __main__.globals :: static
r46 = 'S'
r47 = PyDict_SetItem(r45, r46, r40)
r48 = r47 >= 0 :: signed
r49 = __main__.C :: type
r50 = __main__.S :: type
r51 = __main__.globals :: static
r52 = 'Generic'
r53 = CPyDict_GetItem(r51, r52)
r54 = __main__.globals :: static
r55 = 'T'
r56 = CPyDict_GetItem(r54, r55)
r57 = PyObject_GetItem(r53, r56)
r58 = PyTuple_Pack(3, r49, r50, r57)
r59 = '__main__'
r60 = __main__.D_template :: type
r61 = CPyType_FromTemplate(r60, r58, r59)
r62 = D_trait_vtable_setup()
r63 = '__mypyc_attrs__'
r64 = '__dict__'
r65 = PyTuple_Pack(1, r64)
r66 = PyObject_SetAttr(r61, r63, r65)
r67 = r66 >= 0 :: signed
__main__.D = r61 :: type
r68 = __main__.globals :: static
r69 = 'D'
r70 = PyDict_SetItem(r68, r69, r61)
r71 = r70 >= 0 :: signed
return 1
[case testIsInstance]
class A: pass
class B(A): pass
def f(x: A) -> B:
if isinstance(x, B):
return x
return B()
[out]
def f(x):
x :: __main__.A
r0 :: object
r1 :: ptr
r2 :: object
r3 :: bit
r4, r5 :: __main__.B
L0:
r0 = __main__.B :: type
r1 = get_element_ptr x ob_type :: PyObject
r2 = borrow load_mem r1 :: builtins.object*
keep_alive x
r3 = r2 == r0
if r3 goto L1 else goto L2 :: bool
L1:
r4 = cast(__main__.B, x)
return r4
L2:
r5 = B()
return r5
[case testIsInstanceTuple]
from typing import Union
class R: pass
class A(R): pass
class B(R): pass
class C(R): pass
def f(x: R) -> Union[A, B]:
if isinstance(x, (A, B)):
return x
return A()
[out]
def f(x):
x :: __main__.R
r0 :: object
r1 :: ptr
r2 :: object
r3 :: bit
r4 :: bool
r5 :: object
r6 :: ptr
r7 :: object
r8 :: bit
r9 :: union[__main__.A, __main__.B]
r10 :: __main__.A
L0:
r0 = __main__.A :: type
r1 = get_element_ptr x ob_type :: PyObject
r2 = borrow load_mem r1 :: builtins.object*
keep_alive x
r3 = r2 == r0
if r3 goto L1 else goto L2 :: bool
L1:
r4 = r3
goto L3
L2:
r5 = __main__.B :: type
r6 = get_element_ptr x ob_type :: PyObject
r7 = borrow load_mem r6 :: builtins.object*
keep_alive x
r8 = r7 == r5
r4 = r8
L3:
if r4 goto L4 else goto L5 :: bool
L4:
r9 = cast(union[__main__.A, __main__.B], x)
return r9
L5:
r10 = A()
return r10
[case testIsInstanceFewSubclasses]
class R: pass
class A(R): pass
def f(x: object) -> R:
if isinstance(x, R):
return x
return A()
[out]
def f(x):
x, r0 :: object
r1 :: ptr
r2 :: object
r3 :: bit
r4 :: bool
r5 :: object
r6 :: ptr
r7 :: object
r8 :: bit
r9 :: __main__.R
r10 :: __main__.A
L0:
r0 = __main__.A :: type
r1 = get_element_ptr x ob_type :: PyObject
r2 = borrow load_mem r1 :: builtins.object*
keep_alive x
r3 = r2 == r0
if r3 goto L1 else goto L2 :: bool
L1:
r4 = r3
goto L3
L2:
r5 = __main__.R :: type
r6 = get_element_ptr x ob_type :: PyObject
r7 = borrow load_mem r6 :: builtins.object*
keep_alive x
r8 = r7 == r5
r4 = r8
L3:
if r4 goto L4 else goto L5 :: bool
L4:
r9 = cast(__main__.R, x)
return r9
L5:
r10 = A()
return r10
[case testIsInstanceFewSubclassesTrait]
from mypy_extensions import trait
class B: pass
@trait
class R: pass
class A(B, R): pass
class C(B, R): pass
def f(x: object) -> R:
if isinstance(x, R):
return x
return A()
[out]
def f(x):
x, r0 :: object
r1 :: ptr
r2 :: object
r3 :: bit
r4 :: bool
r5 :: object
r6 :: ptr
r7 :: object
r8 :: bit
r9 :: __main__.R
r10 :: __main__.A
L0:
r0 = __main__.A :: type
r1 = get_element_ptr x ob_type :: PyObject
r2 = borrow load_mem r1 :: builtins.object*
keep_alive x
r3 = r2 == r0
if r3 goto L1 else goto L2 :: bool
L1:
r4 = r3
goto L3
L2:
r5 = __main__.C :: type
r6 = get_element_ptr x ob_type :: PyObject
r7 = borrow load_mem r6 :: builtins.object*
keep_alive x
r8 = r7 == r5
r4 = r8
L3:
if r4 goto L4 else goto L5 :: bool
L4:
r9 = cast(__main__.R, x)
return r9
L5:
r10 = A()
return r10
[case testIsInstanceManySubclasses]
class R: pass
class A(R): pass
class B(R): pass
class C(R): pass
def f(x: object) -> R:
if isinstance(x, R):
return x
return B()
[out]
def f(x):
x, r0 :: object
r1 :: bool
r2 :: __main__.R
r3 :: __main__.B
L0:
r0 = __main__.R :: type
r1 = CPy_TypeCheck(x, r0)
if r1 goto L1 else goto L2 :: bool
L1:
r2 = cast(__main__.R, x)
return r2
L2:
r3 = B()
return r3
[case testFakeSuper]
class A:
def __init__(self, x: int) -> None:
self.x = x
class B(A):
def __init__(self, x: int, y: int) -> None:
A.__init__(self, x)
self.y = y
[out]
def A.__init__(self, x):
self :: __main__.A
x :: int
L0:
self.x = x
return 1
def B.__init__(self, x, y):
self :: __main__.B
x, y :: int
r0 :: None
L0:
r0 = A.__init__(self, x)
self.y = y
return 1
[case testClassMethod]
class C:
@staticmethod
def foo(x: int) -> int: return 10 + x
@classmethod
def bar(cls, x: int) -> int: return 10 + x
def lol() -> int:
return C.foo(1) + C.bar(2)
[out]
def C.foo(x):
x, r0 :: int
L0:
r0 = CPyTagged_Add(20, x)
return r0
def C.bar(cls, x):
cls :: object
x, r0 :: int
L0:
r0 = CPyTagged_Add(20, x)
return r0
def lol():
r0 :: int
r1 :: object
r2, r3 :: int
L0:
r0 = C.foo(2)
r1 = __main__.C :: type
r2 = C.bar(r1, 4)
r3 = CPyTagged_Add(r0, r2)
return r3
[case testCallClassMethodViaCls_64bit]
class C:
@classmethod
def f(cls, x: int) -> int:
return cls.g(x)
@classmethod
def g(cls, x: int) -> int:
return x
class D:
@classmethod
def f(cls, x: int) -> int:
# TODO: This could also be optimized, since g is not ever overridden
return cls.g(x)
@classmethod
def g(cls, x: int) -> int:
return x
class DD(D):
pass
[out]
def C.f(cls, x):
cls :: object
x :: int
r0 :: object
r1 :: int
L0:
r0 = __main__.C :: type
r1 = C.g(r0, x)
return r1
def C.g(cls, x):
cls :: object
x :: int
L0:
return x
def D.f(cls, x):
cls :: object
x :: int
r0 :: str
r1 :: object
r2 :: object[2]
r3 :: object_ptr
r4 :: object
r5 :: int
L0:
r0 = 'g'
r1 = box(int, x)
r2 = [cls, r1]
r3 = load_address r2
r4 = PyObject_VectorcallMethod(r0, r3, 9223372036854775810, 0)
keep_alive cls, r1
r5 = unbox(int, r4)
return r5
def D.g(cls, x):
cls :: object
x :: int
L0:
return x
[case testCannotAssignToClsArgument]
from typing import Any, cast
class C:
@classmethod
def m(cls) -> None:
cls = cast(Any, D) # E: Cannot assign to the first argument of classmethod
cls, x = cast(Any, D), 1 # E: Cannot assign to the first argument of classmethod
cls, x = cast(Any, [1, 2]) # E: Cannot assign to the first argument of classmethod
cls.m()
class D:
pass
[case testSuper1]
class A:
def __init__(self, x: int) -> None:
self.x = x
class B(A):
def __init__(self, x: int, y: int) -> None:
super().__init__(x)
self.y = y
[out]
def A.__init__(self, x):
self :: __main__.A
x :: int
L0:
self.x = x
return 1
def B.__init__(self, x, y):
self :: __main__.B
x, y :: int
r0 :: None
L0:
r0 = A.__init__(self, x)
self.y = y
return 1
[case testSuper2]
from mypy_extensions import trait
@trait
class T:
def foo(self) -> None: pass
class X(T):
def foo(self) -> None:
super().foo()
[out]
def T.foo(self):
self :: __main__.T
L0:
return 1
def X.foo(self):
self :: __main__.X
r0 :: None
L0:
r0 = T.foo(self)
return 1
[case testSuperCallToObjectInitIsOmitted]
class C:
def __init__(self) -> None:
super().__init__()
class D: pass
class E(D):
def __init__(self) -> None:
super().__init__()
class F(C):
def __init__(self) -> None:
super().__init__()
class DictSubclass(dict):
def __init__(self) -> None:
super().__init__()
[out]
def C.__init__(self):
self :: __main__.C
L0:
return 1
def E.__init__(self):
self :: __main__.E
L0:
return 1
def F.__init__(self):
self :: __main__.F
r0 :: None
L0:
r0 = C.__init__(self)
return 1
def DictSubclass.__init__(self):
self :: dict
r0 :: object
r1 :: str
r2, r3 :: object
r4 :: object[2]
r5 :: object_ptr
r6 :: object
r7 :: str
r8, r9 :: object
L0:
r0 = builtins :: module
r1 = 'super'
r2 = CPyObject_GetAttr(r0, r1)
r3 = __main__.DictSubclass :: type
r4 = [r3, self]
r5 = load_address r4
r6 = PyObject_Vectorcall(r2, r5, 2, 0)
keep_alive r3, self
r7 = '__init__'
r8 = CPyObject_GetAttr(r6, r7)
r9 = PyObject_Vectorcall(r8, 0, 0, 0)
return 1
[case testClassVariable]
from typing import ClassVar
class A:
x = 10 # type: ClassVar[int]
def f() -> int:
return A.x
[out]
def f():
r0 :: object
r1 :: str
r2 :: object
r3 :: int
L0:
r0 = __main__.A :: type
r1 = 'x'
r2 = CPyObject_GetAttr(r0, r1)
r3 = unbox(int, r2)
return r3
[case testNoEqDefined]
class A:
pass
def f(a: A, b: A) -> bool:
return a == b
def f2(a: A, b: A) -> bool:
return a != b
[out]
def f(a, b):
a, b :: __main__.A
r0 :: bit
L0:
r0 = a == b
return r0
def f2(a, b):
a, b :: __main__.A
r0 :: bit
L0:
r0 = a != b
return r0
[case testEqDefined]
class Base:
def __eq__(self, other: object) -> bool:
return False
class Derived(Base):
def __eq__(self, other: object) -> bool:
return True
def f(a: Base, b: Base) -> bool:
return a == b
def f2(a: Base, b: Base) -> bool:
return a != b
def fOpt(a: Derived, b: Derived) -> bool:
return a == b
def fOpt2(a: Derived, b: Derived) -> bool:
return a != b
[out]
def Base.__eq__(self, other):
self :: __main__.Base
other, r0 :: object
L0:
r0 = box(bool, 0)
return r0
def Base.__ne__(__mypyc_self__, rhs):
__mypyc_self__ :: __main__.Base
rhs, r0, r1 :: object
r2 :: bit
r3 :: object
r4, r5 :: bit
r6 :: object
r7 :: bit
r8 :: i32
r9 :: bit
r10 :: bool
r11 :: object
L0:
r0 = __mypyc_self__.__eq__(rhs)
r1 = load_address _Py_NotImplementedStruct
r2 = r0 == r1
if r2 goto L7 else goto L1 :: bool
L1:
r3 = load_global Py_True :: static
r4 = r0 == r3
if r4 goto L2 else goto L3 :: bool
L2:
r5 = 0
goto L6
L3:
r6 = load_global Py_False :: static
r7 = r0 == r6
if r7 goto L4 else goto L5 :: bool
L4:
r5 = 1
goto L6
L5:
r8 = PyObject_Not(r0)
r9 = r8 >= 0 :: signed
r10 = truncate r8: i32 to builtins.bool
r5 = r10
L6:
r11 = box(bit, r5)
return r11
L7:
return r1
def Derived.__eq__(self, other):
self :: __main__.Derived
other, r0 :: object
L0:
r0 = box(bool, 1)
return r0
def f(a, b):
a, b :: __main__.Base
r0 :: object
r1 :: bool
L0:
r0 = PyObject_RichCompare(a, b, 2)
r1 = unbox(bool, r0)
return r1
def f2(a, b):
a, b :: __main__.Base
r0 :: object
r1 :: bool
L0:
r0 = PyObject_RichCompare(a, b, 3)
r1 = unbox(bool, r0)
return r1
def fOpt(a, b):
a, b :: __main__.Derived
r0 :: object
r1 :: bool
L0:
r0 = a.__eq__(b)
r1 = unbox(bool, r0)
return r1
def fOpt2(a, b):
a, b :: __main__.Derived
r0 :: object
r1 :: bool
L0:
r0 = a.__ne__(b)
r1 = unbox(bool, r0)
return r1
[case testEqDefinedLater_64bit]
def f(a: 'Base', b: 'Base') -> bool:
return a == b
def f2(a: 'Base', b: 'Base') -> bool:
return a != b
def fOpt(a: 'Derived', b: 'Derived') -> bool:
return a == b
def fOpt2(a: 'Derived', b: 'Derived') -> bool:
return a != b
class Base:
pass
class Derived(Base):
def __eq__(self, other: object) -> bool:
return True
[out]
def f(a, b):
a, b :: __main__.Base
r0 :: object
r1 :: bool
L0:
r0 = PyObject_RichCompare(a, b, 2)
r1 = unbox(bool, r0)
return r1
def f2(a, b):
a, b :: __main__.Base
r0 :: object
r1 :: bool
L0:
r0 = PyObject_RichCompare(a, b, 3)
r1 = unbox(bool, r0)
return r1
def fOpt(a, b):
a, b :: __main__.Derived
r0 :: object
r1 :: bool
L0:
r0 = a.__eq__(b)
r1 = unbox(bool, r0)
return r1
def fOpt2(a, b):
a, b :: __main__.Derived
r0 :: str
r1 :: object[2]
r2 :: object_ptr
r3 :: object
r4 :: bool
L0:
r0 = '__ne__'
r1 = [a, b]
r2 = load_address r1
r3 = PyObject_VectorcallMethod(r0, r2, 9223372036854775810, 0)
keep_alive a, b
r4 = unbox(bool, r3)
return r4
def Derived.__eq__(self, other):
self :: __main__.Derived
other, r0 :: object
L0:
r0 = box(bool, 1)
return r0
def Derived.__ne__(__mypyc_self__, rhs):
__mypyc_self__ :: __main__.Derived
rhs, r0, r1 :: object
r2 :: bit
r3 :: object
r4, r5 :: bit
r6 :: object
r7 :: bit
r8 :: i32
r9 :: bit
r10 :: bool
r11 :: object
L0:
r0 = __mypyc_self__.__eq__(rhs)
r1 = load_address _Py_NotImplementedStruct
r2 = r0 == r1
if r2 goto L7 else goto L1 :: bool
L1:
r3 = load_global Py_True :: static
r4 = r0 == r3
if r4 goto L2 else goto L3 :: bool
L2:
r5 = 0
goto L6
L3:
r6 = load_global Py_False :: static
r7 = r0 == r6
if r7 goto L4 else goto L5 :: bool
L4:
r5 = 1
goto L6
L5:
r8 = PyObject_Not(r0)
r9 = r8 >= 0 :: signed
r10 = truncate r8: i32 to builtins.bool
r5 = r10
L6:
r11 = box(bit, r5)
return r11
L7:
return r1
[case testDefaultVars]
from typing import ClassVar, Optional
class A:
x = 10
def lol(self) -> None:
self.x = 100
LOL = 'lol'
class B(A):
y = LOL
z: Optional[str] = None
b = True
bogus = None # type: int
[out]
def A.lol(self):
self :: __main__.A
r0 :: bool
L0:
self.x = 200; r0 = is_error
return 1
def A.__mypyc_defaults_setup(__mypyc_self__):
__mypyc_self__ :: __main__.A
L0:
__mypyc_self__.x = 20
return 1
def B.__mypyc_defaults_setup(__mypyc_self__):
__mypyc_self__ :: __main__.B
r0 :: dict
r1 :: str
r2 :: object
r3 :: str
r4 :: object
L0:
__mypyc_self__.x = 20
r0 = __main__.globals :: static
r1 = 'LOL'
r2 = CPyDict_GetItem(r0, r1)
r3 = cast(str, r2)
__mypyc_self__.y = r3
r4 = box(None, 1)
__mypyc_self__.z = r4
__mypyc_self__.b = 1
return 1
[case testSubclassDictSpecalized]
from typing import Dict
class WelpDict(Dict[str, int]):
pass
def foo(x: WelpDict) -> None:
# we care that the specalized op gets used
x.update(x)
[out]
def foo(x):
x :: dict
r0 :: i32
r1 :: bit
L0:
r0 = CPyDict_Update(x, x)
r1 = r0 >= 0 :: signed
return 1
[case testNoSpuriousLinearity]
# Make sure that the non-trait MRO linearity check isn't affected by processing order
class A(B): pass
class B(C): pass
class C: pass
[out]
[case testDeletableSemanticAnalysis]
class Err1:
__deletable__ = 'x' # E: "__deletable__" must be initialized with a list or tuple expression
class Err2:
__deletable__ = [
1 # E: Invalid "__deletable__" item; string literal expected
]
class Err3:
__deletable__ = ['x', ['y'], 'z'] # E: Invalid "__deletable__" item; string literal expected
class Err4:
__deletable__ = (1,) # E: Invalid "__deletable__" item; string literal expected
a = ['x']
class Err5:
__deletable__ = a # E: "__deletable__" must be initialized with a list or tuple expression
class Ok1:
__deletable__ = ('x',)
x: int
class Ok2:
__deletable__ = ['x']
x: int
[case testInvalidDeletableAttribute]
class NotDeletable:
__deletable__ = ['x']
x: int
y: int
def g(o: NotDeletable) -> None:
del o.x
del o.y # E: "y" cannot be deleted \
# N: Using "__deletable__ = ['<attr>']" in the class body enables "del obj.<attr>"
class Base:
x: int
class Deriv(Base):
__deletable__ = ['x'] # E: Attribute "x" not defined in "Deriv" (defined in "Base")
class UndefinedDeletable:
__deletable__ = ['x'] # E: Attribute "x" not defined
class DeletableProperty:
__deletable__ = ['prop'] # E: Cannot make property "prop" deletable
@property
def prop(self) -> int:
return 5
[case testFinalDeletable]
from typing import Final
class DeletableFinal1:
x: Final[int] # E: Deletable attribute cannot be final
__deletable__ = ['x']
def __init__(self, x: int) -> None:
self.x = x
class DeletableFinal2:
X: Final = 0 # E: Deletable attribute cannot be final
__deletable__ = ['X']
[case testNeedAnnotateClassVar]
from typing import Final, ClassVar, Type
class C:
a = 'A'
b: str = 'B'
f: Final = 'F'
c: ClassVar = 'C'
class D(C):
pass
def f() -> None:
C.a # E: Cannot access instance attribute "a" through class object \
# N: (Hint: Use "x: Final = ..." or "x: ClassVar = ..." to define a class attribute)
C.b # E: Cannot access instance attribute "b" through class object \
# N: (Hint: Use "x: Final = ..." or "x: ClassVar = ..." to define a class attribute)
C.f
C.c
D.a # E: Cannot access instance attribute "a" through class object \
# N: (Hint: Use "x: Final = ..." or "x: ClassVar = ..." to define a class attribute)
D.b # E: Cannot access instance attribute "b" through class object \
# N: (Hint: Use "x: Final = ..." or "x: ClassVar = ..." to define a class attribute)
D.f
D.c
def g(c: Type[C], d: Type[D]) -> None:
c.a # E: Cannot access instance attribute "a" through class object \
# N: (Hint: Use "x: Final = ..." or "x: ClassVar = ..." to define a class attribute)
c.f
c.c
d.a # E: Cannot access instance attribute "a" through class object \
# N: (Hint: Use "x: Final = ..." or "x: ClassVar = ..." to define a class attribute)
d.f
d.c
[case testSetAttributeWithDefaultInInit]
class C:
s = ''
def __init__(self, s: str) -> None:
self.s = s
[out]
def C.__init__(self, s):
self :: __main__.C
s :: str
r0 :: bool
L0:
self.s = s; r0 = is_error
return 1
def C.__mypyc_defaults_setup(__mypyc_self__):
__mypyc_self__ :: __main__.C
r0 :: str
L0:
r0 = ''
__mypyc_self__.s = r0
return 1
[case testBorrowAttribute]
def f(d: D) -> int:
return d.c.x
class C:
x: int
class D:
c: C
[out]
def f(d):
d :: __main__.D
r0 :: __main__.C
r1 :: int
L0:
r0 = borrow d.c
r1 = r0.x
keep_alive d
return r1
[case testNoBorrowOverPropertyAccess]
class C:
d: D
class D:
@property
def e(self) -> E:
return E()
class E:
x: int
def f(c: C) -> int:
return c.d.e.x
[out]
def D.e(self):
self :: __main__.D
r0 :: __main__.E
L0:
r0 = E()
return r0
def f(c):
c :: __main__.C
r0 :: __main__.D
r1 :: __main__.E
r2 :: int
L0:
r0 = c.d
r1 = r0.e
r2 = r1.x
return r2
[case testBorrowResultOfCustomGetItemInIfStatement]
from typing import List
class C:
def __getitem__(self, x: int) -> List[int]:
return []
def f(x: C) -> None:
# In this case the keep_alive must come before the branch, as otherwise
# reference count transform will get confused.
if x[1][0] == 2:
y = 1
else:
y = 2
[out]
def C.__getitem__(self, x):
self :: __main__.C
x :: int
r0 :: list
L0:
r0 = PyList_New(0)
return r0
def f(x):
x :: __main__.C
r0 :: list
r1 :: object
r2 :: int
r3 :: bit
y :: int
L0:
r0 = x.__getitem__(2)
r1 = CPyList_GetItemShortBorrow(r0, 0)
r2 = unbox(int, r1)
r3 = int_eq r2, 4
keep_alive r0
if r3 goto L1 else goto L2 :: bool
L1:
y = 2
goto L3
L2:
y = 4
L3:
return 1
[case testIncompatibleDefinitionOfAttributeInSubclass]
from mypy_extensions import trait
class Base:
x: int
class Bad1(Base):
x: bool # E: Type of "x" is incompatible with definition in class "Base"
class Good1(Base):
x: int
class Good2(Base):
x: int = 0
class Good3(Base):
x = 0
class Good4(Base):
def __init__(self) -> None:
self.x = 0
class Good5(Base):
def __init__(self) -> None:
self.x: int = 0
class Base2(Base):
pass
class Bad2(Base2):
x: bool = False # E: Type of "x" is incompatible with definition in class "Base"
class Bad3(Base):
x = False # E: Type of "x" is incompatible with definition in class "Base"
@trait
class T:
y: object
class E(T):
y: str # E: Type of "y" is incompatible with definition in trait "T"
[case testNestedClasses]
def outer():
class Inner: # E: Nested class definitions not supported
pass
return Inner
if True:
class OtherInner: # E: Nested class definitions not supported
pass
[case testEnumClassAlias]
from enum import Enum
from typing import Literal, Union
class SomeEnum(Enum):
AVALUE = "a"
ALIAS = Literal[SomeEnum.AVALUE]
ALIAS2 = Union[Literal[SomeEnum.AVALUE], None]
[case testMypycAttrNativeClassErrors]
from mypy_extensions import mypyc_attr
@mypyc_attr(native_class=False)
class AnnontatedNonExtensionClass:
pass
@mypyc_attr(native_class=False)
class DerivedExplicitNonNativeClass(AnnontatedNonExtensionClass):
pass
def decorator(cls):
return cls
@mypyc_attr(native_class=True)
@decorator
class NonNativeClassContradiction(): # E: Class is marked as native_class=True but it can't be a native class. Classes that have decorators other than supported decorators can't be native classes.
pass
@mypyc_attr(native_class="yes")
class BadUse(): # E: native_class must be used with True or False only
pass
[case testMypycAttrNativeClassMetaError]
from mypy_extensions import mypyc_attr
@mypyc_attr(native_class=True)
class M(type): # E: Inheriting from most builtin types is unimplemented \
# N: Potential workaround: @mypy_extensions.mypyc_attr(native_class=False) \
# N: https://mypyc.readthedocs.io/en/stable/native_classes.html#defining-non-native-classes
pass
@mypyc_attr(native_class=True)
class A(metaclass=M): # E: Class is marked as native_class=True but it can't be a native class. Classes with a metaclass other than ABCMeta, TypingMeta or GenericMeta can't be native classes.
pass
[case testReservedName]
from typing import Any, overload
def decorator(cls):
return cls
class TestMethod:
def __mypyc_generator_helper__(self) -> None: # E: Method name "__mypyc_generator_helper__" is reserved for mypyc internal use
pass
class TestDecorator:
@decorator # E: Method name "__mypyc_generator_helper__" is reserved for mypyc internal use
def __mypyc_generator_helper__(self) -> None:
pass
class TestOverload:
@overload # E: Method name "__mypyc_generator_helper__" is reserved for mypyc internal use
def __mypyc_generator_helper__(self, x: int) -> int: ...
@overload
def __mypyc_generator_helper__(self, x: str) -> str: ...
def __mypyc_generator_helper__(self, x: Any) -> Any:
return x
[case testNativeBufferFastPath]
from typing import Final
from mypy_extensions import u8
from librt.internal import (
WriteBuffer, ReadBuffer, write_bool, read_bool, write_str, read_str, write_float, read_float,
write_int, read_int, write_tag, read_tag, write_bytes, read_bytes,
cache_version,
)
Tag = u8
TAG: Final[Tag] = 1
def foo() -> None:
b = WriteBuffer()
write_str(b, "foo")
write_bytes(b, b"bar")
write_bool(b, True)
write_float(b, 0.1)
write_int(b, 1)
write_tag(b, TAG)
rb = ReadBuffer(b.getvalue())
x = read_str(rb)
xb = read_bytes(rb)
y = read_bool(rb)
z = read_float(rb)
t = read_int(rb)
u = read_tag(rb)
v = cache_version()
[out]
def foo():
r0, b :: librt.internal.WriteBuffer
r1 :: str
r2 :: None
r3 :: bytes
r4, r5, r6, r7, r8 :: None
r9 :: bytes
r10, rb :: librt.internal.ReadBuffer
r11, x :: str
r12, xb :: bytes
r13, y :: bool
r14, z :: float
r15, t :: int
r16, u, r17, v :: u8
L0:
r0 = WriteBuffer_internal()
b = r0
r1 = 'foo'
r2 = write_str_internal(b, r1)
r3 = b'bar'
r4 = write_bytes_internal(b, r3)
r5 = write_bool_internal(b, 1)
r6 = write_float_internal(b, 0.1)
r7 = write_int_internal(b, 2)
r8 = write_tag_internal(b, 1)
r9 = WriteBuffer_getvalue_internal(b)
r10 = ReadBuffer_internal(r9)
rb = r10
r11 = read_str_internal(rb)
x = r11
r12 = read_bytes_internal(rb)
xb = r12
r13 = read_bool_internal(rb)
y = r13
r14 = read_float_internal(rb)
z = r14
r15 = read_int_internal(rb)
t = r15
r16 = read_tag_internal(rb)
u = r16
r17 = cache_version_internal()
v = r17
return 1
[case testEnumFastPath]
from enum import Enum
def test(e: E) -> bool:
return e.is_one()
class E(Enum):
ONE = 1
TWO = 2
def is_one(self) -> bool:
return self == E.ONE
[out]
def test(e):
e :: __main__.E
r0 :: bool
L0:
r0 = e.__mypyc_fast_is_one()
return r0
def is_one_E_obj.__get__(__mypyc_self__, instance, owner):
__mypyc_self__, instance, owner, r0 :: object
r1 :: bit
r2 :: object
L0:
r0 = load_address _Py_NoneStruct
r1 = instance == r0
if r1 goto L1 else goto L2 :: bool
L1:
return __mypyc_self__
L2:
r2 = PyMethod_New(__mypyc_self__, instance)
return r2
def is_one_E_obj.__call__(__mypyc_self__, self):
__mypyc_self__ :: __main__.is_one_E_obj
self, r0 :: __main__.E
r1 :: bool
r2 :: bit
L0:
r0 = __main__.E.ONE :: static
if is_error(r0) goto L1 else goto L2
L1:
r1 = raise NameError('value for final name "ONE" was not set')
unreachable
L2:
r2 = self == r0
return r2
def E.__mypyc_fast_is_one(self):
self, r0 :: __main__.E
r1 :: bool
r2 :: bit
L0:
r0 = __main__.E.ONE :: static
if is_error(r0) goto L1 else goto L2
L1:
r1 = raise NameError('value for final name "ONE" was not set')
unreachable
L2:
r2 = self == r0
return r2
[case testTypeObjectName_python3_11]
from typing import Any
class C: pass
class D(C): pass
def n1(t: type[object]) -> str:
return t.__name__
def n2(t: Any) -> str:
return t.__name__
def n3() -> str:
return C.__name__
def n4(t: type[C]) -> str:
return t.__name__
[out]
def n1(t):
t, r0 :: object
r1 :: str
L0:
r0 = CPy_GetName(t)
r1 = cast(str, r0)
return r1
def n2(t):
t, r0 :: object
r1 :: str
L0:
r0 = CPy_GetName(t)
r1 = cast(str, r0)
return r1
def n3():
r0, r1 :: object
r2 :: str
L0:
r0 = __main__.C :: type
r1 = CPy_GetName(r0)
r2 = cast(str, r1)
return r2
def n4(t):
t, r0 :: object
r1 :: str
L0:
r0 = CPy_GetName(t)
r1 = cast(str, r0)
return r1
[case testTypeOfObject]
class C: pass
class D(C): pass
def generic_type(x: object) -> type[object]:
return type(x)
def generic_class(x: object) -> type[object]:
return x.__class__
def native_type(x: C) -> type[object]:
return type(x)
def native_class(x: C) -> type[object]:
return x.__class__
[out]
def generic_type(x):
x, r0 :: object
L0:
r0 = CPy_TYPE(x)
return r0
def generic_class(x):
x :: object
r0 :: str
r1 :: object
L0:
r0 = '__class__'
r1 = CPyObject_GetAttr(x, r0)
return r1
def native_type(x):
x :: __main__.C
r0 :: object
L0:
r0 = CPy_TYPE(x)
return r0
def native_class(x):
x :: __main__.C
r0 :: object
L0:
r0 = CPy_TYPE(x)
return r0
[case testDunderNew]
from __future__ import annotations
from typing import Any
class Test:
val: int
def __new__(cls, val: int) -> Test:
obj = super().__new__(cls)
obj.val = val
return obj
class Test2:
def __new__(cls) -> Test2:
return super().__new__(cls)
class Sub(Test2):
pass
def fn() -> Test:
return Test.__new__(Test, 42)
class NewClassMethod:
val: int
@classmethod
def __new__(cls, val: int) -> NewClassMethod:
obj = super().__new__(cls)
obj.val = val
return obj
def fn2() -> NewClassMethod:
return NewClassMethod.__new__(42)
class NotTransformed:
def __new__(cls, val: int) -> Any:
return super().__new__(str)
def factory(cls: Any, val: int) -> Any:
cls = str
return super().__new__(cls)
[out]
def Test.__new__(cls, val):
cls :: object
val :: int
r0, obj :: __main__.Test
r1 :: bool
L0:
r0 = __mypyc__Test_setup(cls)
obj = r0
obj.val = val; r1 = is_error
return obj
def Test2.__new__(cls):
cls, r0 :: object
r1 :: __main__.Test2
L0:
r0 = CPy_SetupObject(cls)
r1 = cast(__main__.Test2, r0)
return r1
def fn():
r0 :: object
r1 :: __main__.Test
L0:
r0 = __main__.Test :: type
r1 = Test.__new__(r0, 84)
return r1
def NewClassMethod.__new__(cls, val):
cls :: object
val :: int
r0, obj :: __main__.NewClassMethod
r1 :: bool
L0:
r0 = __mypyc__NewClassMethod_setup(cls)
obj = r0
obj.val = val; r1 = is_error
return obj
def fn2():
r0 :: object
r1 :: __main__.NewClassMethod
L0:
r0 = __main__.NewClassMethod :: type
r1 = NewClassMethod.__new__(r0, 84)
return r1
def NotTransformed.__new__(cls, val):
cls :: object
val :: int
r0 :: object
r1 :: str
r2, r3 :: object
r4 :: object[2]
r5 :: object_ptr
r6 :: object
r7 :: str
r8, r9 :: object
r10 :: object[1]
r11 :: object_ptr
r12 :: object
r13 :: str
L0:
r0 = builtins :: module
r1 = 'super'
r2 = CPyObject_GetAttr(r0, r1)
r3 = __main__.NotTransformed :: type
r4 = [r3, cls]
r5 = load_address r4
r6 = PyObject_Vectorcall(r2, r5, 2, 0)
keep_alive r3, cls
r7 = '__new__'
r8 = CPyObject_GetAttr(r6, r7)
r9 = load_address PyUnicode_Type
r10 = [r9]
r11 = load_address r10
r12 = PyObject_Vectorcall(r8, r11, 1, 0)
keep_alive r9
r13 = cast(str, r12)
return r13
def NotTransformed.factory(cls, val):
cls :: object
val :: int
r0, r1 :: object
r2 :: str
r3, r4 :: object
r5 :: object[2]
r6 :: object_ptr
r7 :: object
r8 :: str
r9 :: object
r10 :: object[1]
r11 :: object_ptr
r12 :: object
L0:
r0 = load_address PyUnicode_Type
cls = r0
r1 = builtins :: module
r2 = 'super'
r3 = CPyObject_GetAttr(r1, r2)
r4 = __main__.NotTransformed :: type
r5 = [r4, cls]
r6 = load_address r5
r7 = PyObject_Vectorcall(r3, r6, 2, 0)
keep_alive r4, cls
r8 = '__new__'
r9 = CPyObject_GetAttr(r7, r8)
r10 = [cls]
r11 = load_address r10
r12 = PyObject_Vectorcall(r9, r11, 1, 0)
keep_alive cls
return r12
[case testObjectDunderNew_64bit]
from __future__ import annotations
from mypy_extensions import mypyc_attr
from typing import Any
class Test:
val: int
def __new__(cls, val: int) -> Test:
obj = object.__new__(cls)
obj.val = val
return obj
class Test2:
def __new__(cls) -> Test2:
return object.__new__(cls)
class Sub(Test2):
pass
def fn() -> Test:
return Test.__new__(Test, 42)
class NewClassMethod:
val: int
@classmethod
def __new__(cls, val: int) -> NewClassMethod:
obj = object.__new__(cls)
obj.val = val
return obj
def fn2() -> NewClassMethod:
return NewClassMethod.__new__(42)
class NotTransformed:
def __new__(cls, val: int) -> Any:
return object.__new__(str)
def factory(cls: Any, val: int) -> Any:
cls = str
return object.__new__(cls)
@mypyc_attr(native_class=False)
class NonNative:
def __new__(cls: Any) -> Any:
cls = str
return cls("str")
class InheritsPython(dict):
def __new__(cls: Any) -> Any:
cls = dict
return cls({})
class ObjectNewOutsideDunderNew:
def __init__(self) -> None:
object.__new__(ObjectNewOutsideDunderNew)
def object_new_outside_class() -> None:
object.__new__(Test)
[out]
def Test.__new__(cls, val):
cls :: object
val :: int
r0, obj :: __main__.Test
r1 :: bool
L0:
r0 = __mypyc__Test_setup(cls)
obj = r0
obj.val = val; r1 = is_error
return obj
def Test2.__new__(cls):
cls, r0 :: object
r1 :: __main__.Test2
L0:
r0 = CPy_SetupObject(cls)
r1 = cast(__main__.Test2, r0)
return r1
def fn():
r0 :: object
r1 :: __main__.Test
L0:
r0 = __main__.Test :: type
r1 = Test.__new__(r0, 84)
return r1
def NewClassMethod.__new__(cls, val):
cls :: object
val :: int
r0, obj :: __main__.NewClassMethod
r1 :: bool
L0:
r0 = __mypyc__NewClassMethod_setup(cls)
obj = r0
obj.val = val; r1 = is_error
return obj
def fn2():
r0 :: object
r1 :: __main__.NewClassMethod
L0:
r0 = __main__.NewClassMethod :: type
r1 = NewClassMethod.__new__(r0, 84)
return r1
def NotTransformed.__new__(cls, val):
cls :: object
val :: int
r0 :: object
r1 :: str
r2, r3 :: object
r4 :: str
r5 :: object[2]
r6 :: object_ptr
r7 :: object
r8 :: str
L0:
r0 = builtins :: module
r1 = 'object'
r2 = CPyObject_GetAttr(r0, r1)
r3 = load_address PyUnicode_Type
r4 = '__new__'
r5 = [r2, r3]
r6 = load_address r5
r7 = PyObject_VectorcallMethod(r4, r6, 9223372036854775810, 0)
keep_alive r2, r3
r8 = cast(str, r7)
return r8
def NotTransformed.factory(cls, val):
cls :: object
val :: int
r0, r1 :: object
r2 :: str
r3 :: object
r4 :: str
r5 :: object[2]
r6 :: object_ptr
r7 :: object
L0:
r0 = load_address PyUnicode_Type
cls = r0
r1 = builtins :: module
r2 = 'object'
r3 = CPyObject_GetAttr(r1, r2)
r4 = '__new__'
r5 = [r3, cls]
r6 = load_address r5
r7 = PyObject_VectorcallMethod(r4, r6, 9223372036854775810, 0)
keep_alive r3, cls
return r7
def __new___NonNative_obj.__get__(__mypyc_self__, instance, owner):
__mypyc_self__, instance, owner, r0 :: object
r1 :: bit
r2 :: object
L0:
r0 = load_address _Py_NoneStruct
r1 = instance == r0
if r1 goto L1 else goto L2 :: bool
L1:
return __mypyc_self__
L2:
r2 = PyMethod_New(__mypyc_self__, instance)
return r2
def __new___NonNative_obj.__call__(__mypyc_self__, cls):
__mypyc_self__ :: __main__.__new___NonNative_obj
cls, r0 :: object
r1 :: str
r2 :: object[1]
r3 :: object_ptr
r4 :: object
L0:
r0 = load_address PyUnicode_Type
cls = r0
r1 = 'str'
r2 = [r1]
r3 = load_address r2
r4 = PyObject_Vectorcall(cls, r3, 1, 0)
keep_alive r1
return r4
def InheritsPython.__new__(cls):
cls, r0 :: object
r1 :: dict
r2 :: object[1]
r3 :: object_ptr
r4 :: object
L0:
r0 = load_address PyDict_Type
cls = r0
r1 = PyDict_New()
r2 = [r1]
r3 = load_address r2
r4 = PyObject_Vectorcall(cls, r3, 1, 0)
keep_alive r1
return r4
def ObjectNewOutsideDunderNew.__init__(self):
self :: __main__.ObjectNewOutsideDunderNew
r0 :: object
r1 :: str
r2, r3 :: object
r4 :: str
r5 :: object[2]
r6 :: object_ptr
r7 :: object
L0:
r0 = builtins :: module
r1 = 'object'
r2 = CPyObject_GetAttr(r0, r1)
r3 = __main__.ObjectNewOutsideDunderNew :: type
r4 = '__new__'
r5 = [r2, r3]
r6 = load_address r5
r7 = PyObject_VectorcallMethod(r4, r6, 9223372036854775810, 0)
keep_alive r2, r3
return 1
def object_new_outside_class():
r0 :: object
r1 :: str
r2, r3 :: object
r4 :: str
r5 :: object[2]
r6 :: object_ptr
r7 :: object
L0:
r0 = builtins :: module
r1 = 'object'
r2 = CPyObject_GetAttr(r0, r1)
r3 = __main__.Test :: type
r4 = '__new__'
r5 = [r2, r3]
r6 = load_address r5
r7 = PyObject_VectorcallMethod(r4, r6, 9223372036854775810, 0)
keep_alive r2, r3
return 1
[case testUnsupportedDunderNew]
from __future__ import annotations
from mypy_extensions import mypyc_attr
from typing import Any
@mypyc_attr(native_class=False)
class NonNative:
def __new__(cls) -> NonNative:
return super().__new__(cls) # E: "object.__new__()" not supported for non-extension classes
class InheritsPython(dict):
def __new__(cls) -> InheritsPython:
return super().__new__(cls) # E: "object.__new__()" not supported for classes inheriting from non-native classes
@mypyc_attr(native_class=False)
class NonNativeObjectNew:
def __new__(cls) -> NonNativeObjectNew:
return object.__new__(cls) # E: "object.__new__()" not supported for non-extension classes
class InheritsPythonObjectNew(dict):
def __new__(cls) -> InheritsPythonObjectNew:
return object.__new__(cls) # E: "object.__new__()" not supported for classes inheriting from non-native classes
class ClsAssignment:
def __new__(cls: Any) -> Any:
cls = str # E: Assignment to argument "cls" in "__new__" method unsupported
return super().__new__(cls)
class ClsTupleAssignment:
def __new__(class_i_want: Any, val: int) -> Any:
class_i_want, val = dict, 1 # E: Assignment to argument "class_i_want" in "__new__" method unsupported
return object.__new__(class_i_want)
class ClsListAssignment:
def __new__(cls: Any, val: str) -> Any:
[cls, val] = [object, "object"] # E: Assignment to argument "cls" in "__new__" method unsupported
return object.__new__(cls)
class ClsNestedAssignment:
def __new__(cls: Any, val1: str, val2: int) -> Any:
[val1, [val2, cls]] = ["val1", [2, int]] # E: Assignment to argument "cls" in "__new__" method unsupported
return object.__new__(cls)
class WrongNumberOfArgs:
def __new__(cls):
return super().__new__() # E: "object.__new__()" supported only with 1 argument, got 0
class WrongNumberOfArgsObjectNew:
def __new__(cls):
return object.__new__(cls, 1) # E: "object.__new__()" supported only with 1 argument, got 2
[case testClassWithFreeList]
from mypy_extensions import mypyc_attr, trait
@mypyc_attr(free_list_len=1)
class UsesFreeList:
pass
@mypyc_attr(free_list_len=None)
class NoFreeList:
pass
@mypyc_attr(free_list_len=2) # E: Unsupported value for "free_list_len": 2
class FreeListError:
pass
@trait
@mypyc_attr(free_list_len=1) # E: "free_list_len" can't be used with traits
class NonNative:
pass
@mypyc_attr(free_list_len=1, allow_interpreted_subclasses=True) # E: "free_list_len" can't be used in a class that allows interpreted subclasses
class InterpSub:
pass
[case testUnsupportedGetAttr]
from mypy_extensions import mypyc_attr
@mypyc_attr(allow_interpreted_subclasses=True)
class AllowsInterpreted:
def __getattr__(self, attr: str) -> object: # E: "__getattr__" not supported in class "AllowsInterpreted" because it allows interpreted subclasses
return 0
class InheritsInterpreted(dict):
def __getattr__(self, attr: str) -> object: # E: "__getattr__" not supported in class "InheritsInterpreted" because it inherits from a non-native class
return 0
@mypyc_attr(native_class=False)
class NonNative:
pass
class InheritsNonNative(NonNative):
def __getattr__(self, attr: str) -> object: # E: "__getattr__" not supported in class "InheritsNonNative" because it inherits from a non-native class
return 0
[case testGetAttr]
from typing import ClassVar
class GetAttr:
class_var = "x"
class_var_annotated: ClassVar[int] = 99
def __init__(self, regular_attr: int):
self.regular_attr = regular_attr
def __getattr__(self, attr: str) -> object:
return attr
def method(self) -> int:
return 0
def test_getattr() -> list[object]:
i = GetAttr(42)
one = i.one
two = i.regular_attr
three = i.class_var
four = i.class_var_annotated
five = i.method()
return [one, two, three, four, five]
[typing fixtures/typing-full.pyi]
[out]
def GetAttr.__init__(self, regular_attr):
self :: __main__.GetAttr
regular_attr :: int
L0:
self.regular_attr = regular_attr
return 1
def GetAttr.__getattr__(self, attr):
self :: __main__.GetAttr
attr :: str
L0:
return attr
def GetAttr.__getattr____wrapper(__mypyc_self__, attr):
__mypyc_self__ :: __main__.GetAttr
attr, r0 :: object
r1 :: bit
r2 :: str
r3 :: object
L0:
r0 = CPyObject_GenericGetAttr(__mypyc_self__, attr)
r1 = r0 != 0
if r1 goto L1 else goto L2 :: bool
L1:
return r0
L2:
r2 = cast(str, attr)
r3 = __mypyc_self__.__getattr__(r2)
return r3
def GetAttr.method(self):
self :: __main__.GetAttr
L0:
return 0
def GetAttr.__mypyc_defaults_setup(__mypyc_self__):
__mypyc_self__ :: __main__.GetAttr
r0 :: str
L0:
r0 = 'x'
__mypyc_self__.class_var = r0
return 1
def test_getattr():
r0, i :: __main__.GetAttr
r1 :: str
r2, one :: object
r3, two :: int
r4, three, r5 :: str
r6 :: object
r7, four, r8, five :: int
r9 :: list
r10, r11, r12 :: object
r13 :: ptr
L0:
r0 = GetAttr(84)
i = r0
r1 = 'one'
r2 = CPyObject_GetAttr(i, r1)
one = r2
r3 = i.regular_attr
two = r3
r4 = i.class_var
three = r4
r5 = 'class_var_annotated'
r6 = CPyObject_GetAttr(i, r5)
r7 = unbox(int, r6)
four = r7
r8 = i.method()
five = r8
r9 = PyList_New(5)
r10 = box(int, two)
r11 = box(int, four)
r12 = box(int, five)
r13 = list_items r9
buf_init_item r13, 0, one
buf_init_item r13, 1, r10
buf_init_item r13, 2, three
buf_init_item r13, 3, r11
buf_init_item r13, 4, r12
keep_alive r9
return r9
[case testUnsupportedSetAttr]
from mypy_extensions import mypyc_attr
@mypyc_attr(allow_interpreted_subclasses=True)
class AllowsInterpreted:
def __setattr__(self, attr: str, val: object) -> None: # E: "__setattr__" not supported in class "AllowsInterpreted" because it allows interpreted subclasses
pass
def __delattr__(self, attr: str) -> None:
pass
class InheritsInterpreted(dict):
def __setattr__(self, attr: str, val: object) -> None: # E: "__setattr__" not supported in class "InheritsInterpreted" because it inherits from a non-native class
pass
def __delattr__(self, attr: str) -> None:
pass
@mypyc_attr(native_class=False)
class NonNative:
def __setattr__(self, attr: str, val: object) -> None:
pass
class InheritsNonNative(NonNative):
def __setattr__(self, attr: str, val: object) -> None: # E: "__setattr__" not supported in class "InheritsNonNative" because it inherits from a non-native class
pass
def __delattr__(self, attr: str) -> None:
pass
[case testUnsupportedDelAttr]
class SetAttr:
def __setattr__(self, attr: str, val: object) -> None:
pass
class NoSetAttr:
def __delattr__(self, attr: str) -> None: # E: "__delattr__" supported only in classes that also override "__setattr__", or inherit from a native class that overrides it.
pass
class InheritedSetAttr(SetAttr):
def __delattr__(self, attr: str) -> None:
pass
[case testSetAttr]
from typing import ClassVar
class SetAttr:
_attributes: dict[str, object]
regular_attr: int
class_var: ClassVar[str] = "x"
def __init__(self, regular_attr: int, extra_attrs: dict[str, object], new_attr: str, new_val: object) -> None:
super().__setattr__("_attributes", extra_attrs)
object.__setattr__(self, "regular_attr", regular_attr)
super().__setattr__(new_attr, new_val)
object.__setattr__(self, new_attr, new_val)
def __setattr__(self, key: str, val: object) -> None:
if key == "regular_attr":
super().__setattr__("regular_attr", val)
elif key == "class_var":
raise AttributeError()
else:
self._attributes[key] = val
def test(attr: str, val: object) -> None:
i = SetAttr(99, {}, attr, val)
i.regular_attr = 100
i.new_attr = 101
object.__setattr__(i, "regular_attr", 11)
object.__setattr__(i, attr, val)
[typing fixtures/typing-full.pyi]
[out]
def SetAttr.__init__(self, regular_attr, extra_attrs, new_attr, new_val):
self :: __main__.SetAttr
regular_attr :: int
extra_attrs :: dict
new_attr :: str
new_val :: object
r0 :: i32
r1 :: bit
r2 :: i32
r3 :: bit
L0:
self._attributes = extra_attrs
self.regular_attr = regular_attr
r0 = CPyObject_GenericSetAttr(self, new_attr, new_val)
r1 = r0 >= 0 :: signed
r2 = CPyObject_GenericSetAttr(self, new_attr, new_val)
r3 = r2 >= 0 :: signed
return 1
def SetAttr.__setattr__(self, key, val):
self :: __main__.SetAttr
key :: str
val :: object
r0 :: str
r1 :: bool
r2 :: int
r3 :: bool
r4 :: str
r5 :: bool
r6 :: object
r7 :: str
r8, r9 :: object
r10 :: dict
r11 :: i32
r12 :: bit
L0:
r0 = 'regular_attr'
r1 = CPyStr_EqualLiteral(key, r0, 12)
if r1 goto L1 else goto L2 :: bool
L1:
r2 = unbox(int, val)
self.regular_attr = r2; r3 = is_error
goto L6
L2:
r4 = 'class_var'
r5 = CPyStr_EqualLiteral(key, r4, 9)
if r5 goto L3 else goto L4 :: bool
L3:
r6 = builtins :: module
r7 = 'AttributeError'
r8 = CPyObject_GetAttr(r6, r7)
r9 = PyObject_Vectorcall(r8, 0, 0, 0)
CPy_Raise(r9)
unreachable
L4:
r10 = self._attributes
r11 = CPyDict_SetItem(r10, key, val)
r12 = r11 >= 0 :: signed
L5:
L6:
return 1
def SetAttr.__setattr____wrapper(__mypyc_self__, attr, value):
__mypyc_self__ :: __main__.SetAttr
attr, value :: object
r0 :: bit
r1 :: i32
r2 :: bit
r3 :: str
r4 :: None
L0:
r0 = value == 0
if r0 goto L1 else goto L2 :: bool
L1:
r1 = CPyObject_GenericSetAttr(__mypyc_self__, attr, 0)
r2 = r1 >= 0 :: signed
return 0
L2:
r3 = cast(str, attr)
r4 = __mypyc_self__.__setattr__(r3, value)
return 0
def test(attr, val):
attr :: str
val :: object
r0 :: dict
r1, i :: __main__.SetAttr
r2 :: str
r3 :: object
r4 :: None
r5 :: str
r6 :: object
r7 :: i32
r8 :: bit
r9 :: str
r10 :: object
r11 :: i32
r12 :: bit
r13 :: i32
r14 :: bit
L0:
r0 = PyDict_New()
r1 = SetAttr(198, r0, attr, val)
i = r1
r2 = 'regular_attr'
r3 = object 100
r4 = i.__setattr__(r2, r3)
r5 = 'new_attr'
r6 = object 101
r7 = PyObject_SetAttr(i, r5, r6)
r8 = r7 >= 0 :: signed
r9 = 'regular_attr'
r10 = object 11
r11 = CPyObject_GenericSetAttr(i, r9, r10)
r12 = r11 >= 0 :: signed
r13 = CPyObject_GenericSetAttr(i, attr, val)
r14 = r13 >= 0 :: signed
return 1
[case testSetAttrAndDelAttr]
from typing import ClassVar
class SetAttr:
_attributes: dict[str, object]
regular_attr: int
class_var: ClassVar[str] = "x"
def __init__(self, regular_attr: int, extra_attrs: dict[str, object], new_attr: str, new_val: object) -> None:
super().__setattr__("_attributes", extra_attrs)
object.__setattr__(self, "regular_attr", regular_attr)
super().__setattr__(new_attr, new_val)
object.__setattr__(self, new_attr, new_val)
def __setattr__(self, key: str, val: object) -> None:
if key == "regular_attr":
super().__setattr__("regular_attr", val)
elif key == "class_var":
raise AttributeError()
else:
self._attributes[key] = val
def __delattr__(self, key: str) -> None:
del self._attributes[key]
[typing fixtures/typing-full.pyi]
[out]
def SetAttr.__init__(self, regular_attr, extra_attrs, new_attr, new_val):
self :: __main__.SetAttr
regular_attr :: int
extra_attrs :: dict
new_attr :: str
new_val :: object
r0 :: i32
r1 :: bit
r2 :: i32
r3 :: bit
L0:
self._attributes = extra_attrs
self.regular_attr = regular_attr
r0 = CPyObject_GenericSetAttr(self, new_attr, new_val)
r1 = r0 >= 0 :: signed
r2 = CPyObject_GenericSetAttr(self, new_attr, new_val)
r3 = r2 >= 0 :: signed
return 1
def SetAttr.__setattr__(self, key, val):
self :: __main__.SetAttr
key :: str
val :: object
r0 :: str
r1 :: bool
r2 :: int
r3 :: bool
r4 :: str
r5 :: bool
r6 :: object
r7 :: str
r8, r9 :: object
r10 :: dict
r11 :: i32
r12 :: bit
L0:
r0 = 'regular_attr'
r1 = CPyStr_EqualLiteral(key, r0, 12)
if r1 goto L1 else goto L2 :: bool
L1:
r2 = unbox(int, val)
self.regular_attr = r2; r3 = is_error
goto L6
L2:
r4 = 'class_var'
r5 = CPyStr_EqualLiteral(key, r4, 9)
if r5 goto L3 else goto L4 :: bool
L3:
r6 = builtins :: module
r7 = 'AttributeError'
r8 = CPyObject_GetAttr(r6, r7)
r9 = PyObject_Vectorcall(r8, 0, 0, 0)
CPy_Raise(r9)
unreachable
L4:
r10 = self._attributes
r11 = CPyDict_SetItem(r10, key, val)
r12 = r11 >= 0 :: signed
L5:
L6:
return 1
def SetAttr.__setattr____wrapper(__mypyc_self__, attr, value):
__mypyc_self__ :: __main__.SetAttr
attr, value :: object
r0 :: bit
r1 :: str
r2 :: None
r3 :: str
r4 :: None
L0:
r0 = value == 0
if r0 goto L1 else goto L2 :: bool
L1:
r1 = cast(str, attr)
r2 = __mypyc_self__.__delattr__(r1)
return 0
L2:
r3 = cast(str, attr)
r4 = __mypyc_self__.__setattr__(r3, value)
return 0
def SetAttr.__delattr__(self, key):
self :: __main__.SetAttr
key :: str
r0 :: dict
r1 :: i32
r2 :: bit
L0:
r0 = self._attributes
r1 = PyObject_DelItem(r0, key)
r2 = r1 >= 0 :: signed
return 1
[case testUntransformedSetAttr_64bit]
from mypy_extensions import mypyc_attr
class SetAttr:
def super_missing_args(self):
super().__setattr__()
super().__setattr__("attr")
def object_missing_args(self):
object.__setattr__()
object.__setattr__(self)
object.__setattr__(self, "attr")
@mypyc_attr(native_class=False)
class NonNative:
def super_setattr(self, key: str, val: object) -> None:
super().__setattr__(key, val)
def object_setattr(self, key: str, val: object) -> None:
object.__setattr__(self, key, val)
class InheritsPython(NonNative):
def super_setattr(self, key: str, val: object) -> None:
super().__setattr__(key, val)
def object_setattr(self, key: str, val: object) -> None:
object.__setattr__(self, key, val)
class BuiltInBase(dict):
def super_setattr(self, key: str, val: object) -> None:
super().__setattr__(key, val)
def object_setattr(self, key: str, val: object) -> None:
object.__setattr__(self, key, val)
[typing fixtures/typing-full.pyi]
[out]
def SetAttr.super_missing_args(self):
self :: __main__.SetAttr
r0 :: object
r1 :: str
r2, r3 :: object
r4 :: object[2]
r5 :: object_ptr
r6 :: object
r7 :: str
r8, r9, r10 :: object
r11 :: str
r12, r13 :: object
r14 :: object[2]
r15 :: object_ptr
r16 :: object
r17 :: str
r18 :: object
r19 :: str
r20 :: object[1]
r21 :: object_ptr
r22, r23 :: object
L0:
r0 = builtins :: module
r1 = 'super'
r2 = CPyObject_GetAttr(r0, r1)
r3 = __main__.SetAttr :: type
r4 = [r3, self]
r5 = load_address r4
r6 = PyObject_Vectorcall(r2, r5, 2, 0)
keep_alive r3, self
r7 = '__setattr__'
r8 = CPyObject_GetAttr(r6, r7)
r9 = PyObject_Vectorcall(r8, 0, 0, 0)
r10 = builtins :: module
r11 = 'super'
r12 = CPyObject_GetAttr(r10, r11)
r13 = __main__.SetAttr :: type
r14 = [r13, self]
r15 = load_address r14
r16 = PyObject_Vectorcall(r12, r15, 2, 0)
keep_alive r13, self
r17 = '__setattr__'
r18 = CPyObject_GetAttr(r16, r17)
r19 = 'attr'
r20 = [r19]
r21 = load_address r20
r22 = PyObject_Vectorcall(r18, r21, 1, 0)
keep_alive r19
r23 = box(None, 1)
return r23
def SetAttr.object_missing_args(self):
self :: __main__.SetAttr
r0 :: object
r1 :: str
r2 :: object
r3 :: str
r4 :: object[1]
r5 :: object_ptr
r6, r7 :: object
r8 :: str
r9 :: object
r10 :: str
r11 :: object[2]
r12 :: object_ptr
r13, r14 :: object
r15 :: str
r16 :: object
r17, r18 :: str
r19 :: object[3]
r20 :: object_ptr
r21, r22 :: object
L0:
r0 = builtins :: module
r1 = 'object'
r2 = CPyObject_GetAttr(r0, r1)
r3 = '__setattr__'
r4 = [r2]
r5 = load_address r4
r6 = PyObject_VectorcallMethod(r3, r5, 9223372036854775809, 0)
keep_alive r2
r7 = builtins :: module
r8 = 'object'
r9 = CPyObject_GetAttr(r7, r8)
r10 = '__setattr__'
r11 = [r9, self]
r12 = load_address r11
r13 = PyObject_VectorcallMethod(r10, r12, 9223372036854775810, 0)
keep_alive r9, self
r14 = builtins :: module
r15 = 'object'
r16 = CPyObject_GetAttr(r14, r15)
r17 = 'attr'
r18 = '__setattr__'
r19 = [r16, self, r17]
r20 = load_address r19
r21 = PyObject_VectorcallMethod(r18, r20, 9223372036854775811, 0)
keep_alive r16, self, r17
r22 = box(None, 1)
return r22
def super_setattr_NonNative_obj.__get__(__mypyc_self__, instance, owner):
__mypyc_self__, instance, owner, r0 :: object
r1 :: bit
r2 :: object
L0:
r0 = load_address _Py_NoneStruct
r1 = instance == r0
if r1 goto L1 else goto L2 :: bool
L1:
return __mypyc_self__
L2:
r2 = PyMethod_New(__mypyc_self__, instance)
return r2
def super_setattr_NonNative_obj.__call__(__mypyc_self__, self, key, val):
__mypyc_self__ :: __main__.super_setattr_NonNative_obj
self :: __main__.NonNative
key :: str
val, r0 :: object
r1 :: str
r2, r3 :: object
r4 :: object[2]
r5 :: object_ptr
r6 :: object
r7 :: str
r8 :: object
r9 :: object[2]
r10 :: object_ptr
r11 :: object
L0:
r0 = builtins :: module
r1 = 'super'
r2 = CPyObject_GetAttr(r0, r1)
r3 = __main__.NonNative :: type
r4 = [r3, self]
r5 = load_address r4
r6 = PyObject_Vectorcall(r2, r5, 2, 0)
keep_alive r3, self
r7 = '__setattr__'
r8 = CPyObject_GetAttr(r6, r7)
r9 = [key, val]
r10 = load_address r9
r11 = PyObject_Vectorcall(r8, r10, 2, 0)
keep_alive key, val
return 1
def object_setattr_NonNative_obj.__get__(__mypyc_self__, instance, owner):
__mypyc_self__, instance, owner, r0 :: object
r1 :: bit
r2 :: object
L0:
r0 = load_address _Py_NoneStruct
r1 = instance == r0
if r1 goto L1 else goto L2 :: bool
L1:
return __mypyc_self__
L2:
r2 = PyMethod_New(__mypyc_self__, instance)
return r2
def object_setattr_NonNative_obj.__call__(__mypyc_self__, self, key, val):
__mypyc_self__ :: __main__.object_setattr_NonNative_obj
self :: __main__.NonNative
key :: str
val, r0 :: object
r1 :: str
r2 :: object
r3 :: str
r4 :: object[4]
r5 :: object_ptr
r6 :: object
L0:
r0 = builtins :: module
r1 = 'object'
r2 = CPyObject_GetAttr(r0, r1)
r3 = '__setattr__'
r4 = [r2, self, key, val]
r5 = load_address r4
r6 = PyObject_VectorcallMethod(r3, r5, 9223372036854775812, 0)
keep_alive r2, self, key, val
return 1
def InheritsPython.super_setattr(self, key, val):
self :: __main__.InheritsPython
key :: str
val, r0 :: object
r1 :: str
r2, r3 :: object
r4 :: object[2]
r5 :: object_ptr
r6 :: object
r7 :: str
r8 :: object
r9 :: object[2]
r10 :: object_ptr
r11 :: object
L0:
r0 = builtins :: module
r1 = 'super'
r2 = CPyObject_GetAttr(r0, r1)
r3 = __main__.InheritsPython :: type
r4 = [r3, self]
r5 = load_address r4
r6 = PyObject_Vectorcall(r2, r5, 2, 0)
keep_alive r3, self
r7 = '__setattr__'
r8 = CPyObject_GetAttr(r6, r7)
r9 = [key, val]
r10 = load_address r9
r11 = PyObject_Vectorcall(r8, r10, 2, 0)
keep_alive key, val
return 1
def InheritsPython.object_setattr(self, key, val):
self :: __main__.InheritsPython
key :: str
val, r0 :: object
r1 :: str
r2 :: object
r3 :: str
r4 :: object[4]
r5 :: object_ptr
r6 :: object
L0:
r0 = builtins :: module
r1 = 'object'
r2 = CPyObject_GetAttr(r0, r1)
r3 = '__setattr__'
r4 = [r2, self, key, val]
r5 = load_address r4
r6 = PyObject_VectorcallMethod(r3, r5, 9223372036854775812, 0)
keep_alive r2, self, key, val
return 1
def BuiltInBase.super_setattr(self, key, val):
self :: dict
key :: str
val, r0 :: object
r1 :: str
r2, r3 :: object
r4 :: object[2]
r5 :: object_ptr
r6 :: object
r7 :: str
r8 :: object
r9 :: object[2]
r10 :: object_ptr
r11 :: object
L0:
r0 = builtins :: module
r1 = 'super'
r2 = CPyObject_GetAttr(r0, r1)
r3 = __main__.BuiltInBase :: type
r4 = [r3, self]
r5 = load_address r4
r6 = PyObject_Vectorcall(r2, r5, 2, 0)
keep_alive r3, self
r7 = '__setattr__'
r8 = CPyObject_GetAttr(r6, r7)
r9 = [key, val]
r10 = load_address r9
r11 = PyObject_Vectorcall(r8, r10, 2, 0)
keep_alive key, val
return 1
def BuiltInBase.object_setattr(self, key, val):
self :: dict
key :: str
val, r0 :: object
r1 :: str
r2 :: object
r3 :: str
r4 :: object[4]
r5 :: object_ptr
r6 :: object
L0:
r0 = builtins :: module
r1 = 'object'
r2 = CPyObject_GetAttr(r0, r1)
r3 = '__setattr__'
r4 = [r2, self, key, val]
r5 = load_address r4
r6 = PyObject_VectorcallMethod(r3, r5, 9223372036854775812, 0)
keep_alive r2, self, key, val
return 1
[case testInvalidMypycAttr]
from mypy_extensions import mypyc_attr
@mypyc_attr("allow_interpreted_subclasses", "invalid_arg") # E: "invalid_arg" is not a supported "mypyc_attr" \
# N: supported keys: "allow_interpreted_subclasses", "free_list_len", "native_class", "serializable"
class InvalidArg:
pass
@mypyc_attr(invalid_kwarg=True) # E: "invalid_kwarg" is not a supported "mypyc_attr" \
# N: supported keys: "allow_interpreted_subclasses", "free_list_len", "native_class", "serializable"
class InvalidKwarg:
pass
@mypyc_attr(str()) # E: All "mypyc_attr" positional arguments must be string literals.
class InvalidLiteral:
pass
|