File size: 118,310 Bytes
d0faac4 | 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 | diff --git a/Grammar/python.gram b/Grammar/python.gram
index 05d7837e3a..1c1c53c4b7 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -647,21 +647,25 @@ type_params[asdl_type_param_seq*]: '[' t=type_param_seq ']' {
type_param_seq[asdl_type_param_seq*]: a[asdl_type_param_seq*]=','.type_param+ [','] { a }
type_param[type_param_ty] (memo):
- | a=NAME b=[type_param_bound] { _PyAST_TypeVar(a->v.Name.id, b, EXTRA) }
+ | a=NAME b=[type_param_bound] c=[type_param_default] { _PyAST_TypeVar(a->v.Name.id, b, c, EXTRA) }
| '*' a=NAME colon=':' e=expression {
RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind
? "cannot use constraints with TypeVarTuple"
: "cannot use bound with TypeVarTuple")
}
- | '*' a=NAME { _PyAST_TypeVarTuple(a->v.Name.id, EXTRA) }
+ | '*' a=NAME b=[type_param_starred_default] { _PyAST_TypeVarTuple(a->v.Name.id, b, EXTRA) }
| '**' a=NAME colon=':' e=expression {
RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind
? "cannot use constraints with ParamSpec"
: "cannot use bound with ParamSpec")
}
- | '**' a=NAME { _PyAST_ParamSpec(a->v.Name.id, EXTRA) }
+ | '**' a=NAME b=[type_param_default] { _PyAST_ParamSpec(a->v.Name.id, b, EXTRA) }
type_param_bound[expr_ty]: ':' e=expression { e }
+type_param_default[expr_ty]: '=' e=expression {
+ CHECK_VERSION(expr_ty, 13, "Type parameter defaults are", e) }
+type_param_starred_default[expr_ty]: '=' e=star_expression {
+ CHECK_VERSION(expr_ty, 13, "Type parameter defaults are", e) }
# EXPRESSIONS
# -----------
diff --git a/Include/internal/pycore_ast.h b/Include/internal/pycore_ast.h
index f222d485e0..f5bf1205a8 100644
--- a/Include/internal/pycore_ast.h
+++ b/Include/internal/pycore_ast.h
@@ -657,14 +657,17 @@ struct _type_param {
struct {
identifier name;
expr_ty bound;
+ expr_ty default_value;
} TypeVar;
struct {
identifier name;
+ expr_ty default_value;
} ParamSpec;
struct {
identifier name;
+ expr_ty default_value;
} TypeVarTuple;
} v;
@@ -892,14 +895,15 @@ pattern_ty _PyAST_MatchOr(asdl_pattern_seq * patterns, int lineno, int
col_offset, int end_lineno, int end_col_offset,
PyArena *arena);
type_ignore_ty _PyAST_TypeIgnore(int lineno, string tag, PyArena *arena);
-type_param_ty _PyAST_TypeVar(identifier name, expr_ty bound, int lineno, int
- col_offset, int end_lineno, int end_col_offset,
- PyArena *arena);
-type_param_ty _PyAST_ParamSpec(identifier name, int lineno, int col_offset, int
- end_lineno, int end_col_offset, PyArena *arena);
-type_param_ty _PyAST_TypeVarTuple(identifier name, int lineno, int col_offset,
- int end_lineno, int end_col_offset, PyArena
- *arena);
+type_param_ty _PyAST_TypeVar(identifier name, expr_ty bound, expr_ty
+ default_value, int lineno, int col_offset, int
+ end_lineno, int end_col_offset, PyArena *arena);
+type_param_ty _PyAST_ParamSpec(identifier name, expr_ty default_value, int
+ lineno, int col_offset, int end_lineno, int
+ end_col_offset, PyArena *arena);
+type_param_ty _PyAST_TypeVarTuple(identifier name, expr_ty default_value, int
+ lineno, int col_offset, int end_lineno, int
+ end_col_offset, PyArena *arena);
PyObject* PyAST_mod2obj(mod_ty t);
diff --git a/Include/internal/pycore_ast_state.h b/Include/internal/pycore_ast_state.h
index f1b1786264..09ae954654 100644
--- a/Include/internal/pycore_ast_state.h
+++ b/Include/internal/pycore_ast_state.h
@@ -184,6 +184,7 @@ struct ast_state {
PyObject *conversion;
PyObject *ctx;
PyObject *decorator_list;
+ PyObject *default_value;
PyObject *defaults;
PyObject *elt;
PyObject *elts;
diff --git a/Include/internal/pycore_intrinsics.h b/Include/internal/pycore_intrinsics.h
index 8fa88ea3f7..39c2a30f6e 100644
--- a/Include/internal/pycore_intrinsics.h
+++ b/Include/internal/pycore_intrinsics.h
@@ -28,8 +28,9 @@
#define INTRINSIC_TYPEVAR_WITH_BOUND 2
#define INTRINSIC_TYPEVAR_WITH_CONSTRAINTS 3
#define INTRINSIC_SET_FUNCTION_TYPE_PARAMS 4
+#define INTRINSIC_SET_TYPEPARAM_DEFAULT 5
-#define MAX_INTRINSIC_2 4
+#define MAX_INTRINSIC_2 5
typedef PyObject *(*intrinsic_func1)(PyThreadState* tstate, PyObject *value);
typedef PyObject *(*intrinsic_func2)(PyThreadState* tstate, PyObject *value1, PyObject *value2);
diff --git a/Include/internal/pycore_typevarobject.h b/Include/internal/pycore_typevarobject.h
index c9fa97d682..80a2daf4ef 100644
--- a/Include/internal/pycore_typevarobject.h
+++ b/Include/internal/pycore_typevarobject.h
@@ -13,10 +13,12 @@ extern PyObject *_Py_make_paramspec(PyThreadState *, PyObject *);
extern PyObject *_Py_make_typevartuple(PyThreadState *, PyObject *);
extern PyObject *_Py_make_typealias(PyThreadState *, PyObject *);
extern PyObject *_Py_subscript_generic(PyThreadState *, PyObject *);
+extern PyObject *_Py_set_typeparam_default(PyThreadState *, PyObject *, PyObject *);
extern int _Py_initialize_generic(PyInterpreterState *);
extern void _Py_clear_generic_types(PyInterpreterState *);
extern PyTypeObject _PyTypeAlias_Type;
+extern PyObject _Py_NoDefaultStruct;
#ifdef __cplusplus
}
diff --git a/Lib/ast.py b/Lib/ast.py
index 9f38605165..d7e51aba59 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -1124,12 +1124,21 @@ def visit_TypeVar(self, node):
if node.bound:
self.write(": ")
self.traverse(node.bound)
+ if node.default_value:
+ self.write(" = ")
+ self.traverse(node.default_value)
def visit_TypeVarTuple(self, node):
self.write("*" + node.name)
+ if node.default_value:
+ self.write(" = ")
+ self.traverse(node.default_value)
def visit_ParamSpec(self, node):
self.write("**" + node.name)
+ if node.default_value:
+ self.write(" = ")
+ self.traverse(node.default_value)
def visit_TypeAlias(self, node):
self.fill("type ")
diff --git a/Lib/typing.py b/Lib/typing.py
index eff65cfb68..3f6ff491e7 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -38,6 +38,7 @@
ParamSpecKwargs,
TypeAliasType,
Generic,
+ NoDefault,
)
# Please keep __all__ alphabetized within each category.
@@ -138,6 +139,7 @@
'NewType',
'no_type_check',
'no_type_check_decorator',
+ 'NoDefault',
'NoReturn',
'NotRequired',
'overload',
@@ -266,6 +268,10 @@ def _collect_parameters(args):
>>> _collect_parameters((T, Callable[P, T]))
(~T, ~P)
"""
+ # required type parameter cannot appear after parameter with default
+ default_encountered = False
+ # or after TypeVarTuple
+ type_var_tuple_encountered = False
parameters = []
for t in args:
if isinstance(t, type):
@@ -280,27 +286,58 @@ def _collect_parameters(args):
parameters.append(collected)
elif hasattr(t, '__typing_subst__'):
if t not in parameters:
+ if type_var_tuple_encountered and t.has_default():
+ raise TypeError('Type parameter with a default'
+ ' follows TypeVarTuple')
+
+ if t.has_default():
+ default_encountered = True
+ elif default_encountered:
+ raise TypeError(f'Type parameter {t!r} without a default'
+ ' follows type parameter with a default')
+
parameters.append(t)
else:
+ if _is_unpacked_typevartuple(t):
+ type_var_tuple_encountered = True
for x in getattr(t, '__parameters__', ()):
if x not in parameters:
parameters.append(x)
return tuple(parameters)
-def _check_generic(cls, parameters, elen):
+def _check_generic_specialization(cls, arguments):
"""Check correct count for parameters of a generic cls (internal helper).
This gives a nice error message in case of count mismatch.
"""
- if not elen:
+ expected_len = len(cls.__parameters__)
+ if not expected_len:
raise TypeError(f"{cls} is not a generic class")
- alen = len(parameters)
- if alen != elen:
- raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments for {cls};"
- f" actual {alen}, expected {elen}")
+ actual_len = len(arguments)
+ if actual_len != expected_len:
+ # deal with defaults
+ if actual_len < expected_len:
+ # If the parameter at index `actual_len` in the parameters list
+ # has a default, then all parameters after it must also have
+ # one, because we validated as much in _collect_parameters().
+ # That means that no error needs to be raised here, despite
+ # the number of arguments being passed not matching the number
+ # of parameters: all parameters that aren't explicitly
+ # specialized in this call are parameters with default values.
+ if cls.__parameters__[actual_len].has_default():
+ return
+
+ expected_len -= sum(p.has_default() for p in cls.__parameters__)
+ expect_val = f"at least {expected_len}"
+ else:
+ expect_val = expected_len
+
+ raise TypeError(f"Too {'many' if actual_len > expected_len else 'few'} arguments"
+ f" for {cls}; actual {actual_len}, expected {expect_val}")
-def _unpack_args(args):
+
+def _unpack_args(*args):
newargs = []
for arg in args:
subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
@@ -1089,11 +1126,15 @@ def _typevartuple_prepare_subst(self, alias, args):
elif left + right > alen:
raise TypeError(f"Too few arguments for {alias};"
f" actual {alen}, expected at least {plen-1}")
+ if left == alen - right and self.has_default():
+ replacement = _unpack_args(self.__default__)
+ else:
+ replacement = args[left: alen - right]
return (
*args[:left],
*([fillarg]*(typevartuple_index - left)),
- tuple(args[left: alen - right]),
+ replacement,
*([fillarg]*(plen - right - left - typevartuple_index - 1)),
*args[alen - right:],
)
@@ -1111,6 +1152,8 @@ def _paramspec_subst(self, arg):
def _paramspec_prepare_subst(self, alias, args):
params = alias.__parameters__
i = params.index(self)
+ if i == len(args) and self.has_default():
+ args = [*args, self.__default__]
if i >= len(args):
raise TypeError(f"Too few arguments for {alias}")
# Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
@@ -1124,33 +1167,33 @@ def _paramspec_prepare_subst(self, alias, args):
@_tp_cache
-def _generic_class_getitem(cls, params):
+def _generic_class_getitem(cls, args):
"""Parameterizes a generic class.
At least, parameterizing a generic class is the *main* thing this method
does. For example, for some generic class `Foo`, this is called when we
- do `Foo[int]` - there, with `cls=Foo` and `params=int`.
+ do `Foo[int]` - there, with `cls=Foo` and `args=int`.
However, note that this method is also called when defining generic
classes in the first place with `class Foo(Generic[T]): ...`.
"""
- if not isinstance(params, tuple):
- params = (params,)
+ if not isinstance(args, tuple):
+ args = (args,)
- params = tuple(_type_convert(p) for p in params)
+ args = tuple(_type_convert(p) for p in args)
is_generic_or_protocol = cls in (Generic, Protocol)
if is_generic_or_protocol:
# Generic and Protocol can only be subscripted with unique type variables.
- if not params:
+ if not args:
raise TypeError(
f"Parameter list to {cls.__qualname__}[...] cannot be empty"
)
- if not all(_is_typevar_like(p) for p in params):
+ if not all(_is_typevar_like(p) for p in args):
raise TypeError(
f"Parameters to {cls.__name__}[...] must all be type variables "
f"or parameter specification variables.")
- if len(set(params)) != len(params):
+ if len(set(args)) != len(args):
raise TypeError(
f"Parameters to {cls.__name__}[...] must all be unique")
else:
@@ -1158,18 +1201,18 @@ def _generic_class_getitem(cls, params):
for param in cls.__parameters__:
prepare = getattr(param, '__typing_prepare_subst__', None)
if prepare is not None:
- params = prepare(cls, params)
- _check_generic(cls, params, len(cls.__parameters__))
+ args = prepare(cls, args)
+ _check_generic_specialization(cls, args)
new_args = []
- for param, new_arg in zip(cls.__parameters__, params):
+ for param, new_arg in zip(cls.__parameters__, args):
if isinstance(param, TypeVarTuple):
new_args.extend(new_arg)
else:
new_args.append(new_arg)
- params = tuple(new_args)
+ args = tuple(new_args)
- return _GenericAlias(cls, params)
+ return _GenericAlias(cls, args)
def _generic_init_subclass(cls, *args, **kwargs):
@@ -1390,8 +1433,7 @@ def __getitem__(self, args):
# Preprocess `args`.
if not isinstance(args, tuple):
args = (args,)
- args = tuple(_type_convert(p) for p in args)
- args = _unpack_args(args)
+ args = _unpack_args(*(_type_convert(p) for p in args))
new_args = self._determine_new_args(args)
r = self.copy_with(new_args)
return r
@@ -1552,7 +1594,12 @@ def __getitem__(self, params):
params = (params,)
msg = "Parameters to generic types must be types."
params = tuple(_type_check(p, msg) for p in params)
- _check_generic(self, params, self._nparams)
+ actual_len = len(params)
+ if actual_len != self._nparams:
+ if not self._nparams:
+ raise TypeError(f"{self} is not a generic class")
+ raise TypeError(f"Too {'many' if actual_len > self._nparams else 'few'} arguments for {self};"
+ f" actual {actual_len}, expected {self._nparams}")
return self.copy_with(params)
def copy_with(self, params):
diff --git a/Modules/_typingmodule.c b/Modules/_typingmodule.c
index 9ea72bf89c..180f3d7eb0 100644
--- a/Modules/_typingmodule.c
+++ b/Modules/_typingmodule.c
@@ -63,6 +63,9 @@ _typing_exec(PyObject *m)
if (PyModule_AddObjectRef(m, "TypeAliasType", (PyObject *)&_PyTypeAlias_Type) < 0) {
return -1;
}
+ if (PyModule_AddObjectRef(m, "NoDefault", (PyObject *)&_Py_NoDefaultStruct) < 0) {
+ return -1;
+ }
return 0;
}
diff --git a/Objects/clinic/typevarobject.c.h b/Objects/clinic/typevarobject.c.h
index 2bb0a98a2e..0ba4ff48bc 100644
--- a/Objects/clinic/typevarobject.c.h
+++ b/Objects/clinic/typevarobject.c.h
@@ -9,16 +9,16 @@ preserve
#include "pycore_modsupport.h" // _PyArg_UnpackKeywordsWithVararg()
PyDoc_STRVAR(typevar_new__doc__,
-"typevar(name, *constraints, bound=None, covariant=False,\n"
-" contravariant=False, infer_variance=False)\n"
+"typevar(name, *constraints, bound=None, default=typing.NoDefault,\n"
+" covariant=False, contravariant=False, infer_variance=False)\n"
"--\n"
"\n"
"Create a TypeVar.");
static PyObject *
typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
- PyObject *bound, int covariant, int contravariant,
- int infer_variance);
+ PyObject *bound, PyObject *default_value, int covariant,
+ int contravariant, int infer_variance);
static PyObject *
typevar_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
@@ -26,14 +26,14 @@ typevar_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
- #define NUM_KEYWORDS 5
+ #define NUM_KEYWORDS 6
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
- .ob_item = { &_Py_ID(name), &_Py_ID(bound), &_Py_ID(covariant), &_Py_ID(contravariant), &_Py_ID(infer_variance), },
+ .ob_item = { &_Py_ID(name), &_Py_ID(bound), &_Py_ID(default), &_Py_ID(covariant), &_Py_ID(contravariant), &_Py_ID(infer_variance), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
@@ -42,20 +42,21 @@ typevar_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
- static const char * const _keywords[] = {"name", "bound", "covariant", "contravariant", "infer_variance", NULL};
+ static const char * const _keywords[] = {"name", "bound", "default", "covariant", "contravariant", "infer_variance", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "typevar",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
- PyObject *argsbuf[6];
+ PyObject *argsbuf[7];
PyObject * const *fastargs;
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Py_ssize_t noptargs = Py_MIN(nargs, 1) + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
PyObject *name;
PyObject *constraints = NULL;
PyObject *bound = Py_None;
+ PyObject *default_value = &_Py_NoDefaultStruct;
int covariant = 0;
int contravariant = 0;
int infer_variance = 0;
@@ -80,7 +81,13 @@ typevar_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[3]) {
- covariant = PyObject_IsTrue(fastargs[3]);
+ default_value = fastargs[3];
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ if (fastargs[4]) {
+ covariant = PyObject_IsTrue(fastargs[4]);
if (covariant < 0) {
goto exit;
}
@@ -88,8 +95,8 @@ typevar_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto skip_optional_kwonly;
}
}
- if (fastargs[4]) {
- contravariant = PyObject_IsTrue(fastargs[4]);
+ if (fastargs[5]) {
+ contravariant = PyObject_IsTrue(fastargs[5]);
if (contravariant < 0) {
goto exit;
}
@@ -97,12 +104,12 @@ typevar_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto skip_optional_kwonly;
}
}
- infer_variance = PyObject_IsTrue(fastargs[5]);
+ infer_variance = PyObject_IsTrue(fastargs[6]);
if (infer_variance < 0) {
goto exit;
}
skip_optional_kwonly:
- return_value = typevar_new_impl(type, name, constraints, bound, covariant, contravariant, infer_variance);
+ return_value = typevar_new_impl(type, name, constraints, bound, default_value, covariant, contravariant, infer_variance);
exit:
Py_XDECREF(constraints);
@@ -117,6 +124,36 @@ PyDoc_STRVAR(typevar_typing_subst__doc__,
#define TYPEVAR_TYPING_SUBST_METHODDEF \
{"__typing_subst__", (PyCFunction)typevar_typing_subst, METH_O, typevar_typing_subst__doc__},
+PyDoc_STRVAR(typevar_typing_prepare_subst__doc__,
+"__typing_prepare_subst__($self, alias, args, /)\n"
+"--\n"
+"\n");
+
+#define TYPEVAR_TYPING_PREPARE_SUBST_METHODDEF \
+ {"__typing_prepare_subst__", _PyCFunction_CAST(typevar_typing_prepare_subst), METH_FASTCALL, typevar_typing_prepare_subst__doc__},
+
+static PyObject *
+typevar_typing_prepare_subst_impl(typevarobject *self, PyObject *alias,
+ PyObject *args);
+
+static PyObject *
+typevar_typing_prepare_subst(typevarobject *self, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *alias;
+ PyObject *__clinic_args;
+
+ if (!_PyArg_CheckPositional("__typing_prepare_subst__", nargs, 2, 2)) {
+ goto exit;
+ }
+ alias = args[0];
+ __clinic_args = args[1];
+ return_value = typevar_typing_prepare_subst_impl(self, alias, __clinic_args);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(typevar_reduce__doc__,
"__reduce__($self, /)\n"
"--\n"
@@ -134,6 +171,23 @@ typevar_reduce(typevarobject *self, PyObject *Py_UNUSED(ignored))
return typevar_reduce_impl(self);
}
+PyDoc_STRVAR(typevar_has_default__doc__,
+"has_default($self, /)\n"
+"--\n"
+"\n");
+
+#define TYPEVAR_HAS_DEFAULT_METHODDEF \
+ {"has_default", (PyCFunction)typevar_has_default, METH_NOARGS, typevar_has_default__doc__},
+
+static PyObject *
+typevar_has_default_impl(typevarobject *self);
+
+static PyObject *
+typevar_has_default(typevarobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return typevar_has_default_impl(self);
+}
+
PyDoc_STRVAR(paramspecargs_new__doc__,
"paramspecargs(origin)\n"
"--\n"
@@ -243,15 +297,16 @@ exit:
}
PyDoc_STRVAR(paramspec_new__doc__,
-"paramspec(name, *, bound=None, covariant=False, contravariant=False,\n"
-" infer_variance=False)\n"
+"paramspec(name, *, bound=None, default=typing.NoDefault,\n"
+" covariant=False, contravariant=False, infer_variance=False)\n"
"--\n"
"\n"
"Create a ParamSpec object.");
static PyObject *
paramspec_new_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
- int covariant, int contravariant, int infer_variance);
+ PyObject *default_value, int covariant, int contravariant,
+ int infer_variance);
static PyObject *
paramspec_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
@@ -259,14 +314,14 @@ paramspec_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
- #define NUM_KEYWORDS 5
+ #define NUM_KEYWORDS 6
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
- .ob_item = { &_Py_ID(name), &_Py_ID(bound), &_Py_ID(covariant), &_Py_ID(contravariant), &_Py_ID(infer_variance), },
+ .ob_item = { &_Py_ID(name), &_Py_ID(bound), &_Py_ID(default), &_Py_ID(covariant), &_Py_ID(contravariant), &_Py_ID(infer_variance), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
@@ -275,19 +330,20 @@ paramspec_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
- static const char * const _keywords[] = {"name", "bound", "covariant", "contravariant", "infer_variance", NULL};
+ static const char * const _keywords[] = {"name", "bound", "default", "covariant", "contravariant", "infer_variance", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "paramspec",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
- PyObject *argsbuf[5];
+ PyObject *argsbuf[6];
PyObject * const *fastargs;
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
PyObject *name;
PyObject *bound = Py_None;
+ PyObject *default_value = &_Py_NoDefaultStruct;
int covariant = 0;
int contravariant = 0;
int infer_variance = 0;
@@ -311,7 +367,13 @@ paramspec_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[2]) {
- covariant = PyObject_IsTrue(fastargs[2]);
+ default_value = fastargs[2];
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ if (fastargs[3]) {
+ covariant = PyObject_IsTrue(fastargs[3]);
if (covariant < 0) {
goto exit;
}
@@ -319,8 +381,8 @@ paramspec_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto skip_optional_kwonly;
}
}
- if (fastargs[3]) {
- contravariant = PyObject_IsTrue(fastargs[3]);
+ if (fastargs[4]) {
+ contravariant = PyObject_IsTrue(fastargs[4]);
if (contravariant < 0) {
goto exit;
}
@@ -328,12 +390,12 @@ paramspec_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto skip_optional_kwonly;
}
}
- infer_variance = PyObject_IsTrue(fastargs[4]);
+ infer_variance = PyObject_IsTrue(fastargs[5]);
if (infer_variance < 0) {
goto exit;
}
skip_optional_kwonly:
- return_value = paramspec_new_impl(type, name, bound, covariant, contravariant, infer_variance);
+ return_value = paramspec_new_impl(type, name, bound, default_value, covariant, contravariant, infer_variance);
exit:
return return_value;
@@ -394,14 +456,32 @@ paramspec_reduce(paramspecobject *self, PyObject *Py_UNUSED(ignored))
return paramspec_reduce_impl(self);
}
+PyDoc_STRVAR(paramspec_has_default__doc__,
+"has_default($self, /)\n"
+"--\n"
+"\n");
+
+#define PARAMSPEC_HAS_DEFAULT_METHODDEF \
+ {"has_default", (PyCFunction)paramspec_has_default, METH_NOARGS, paramspec_has_default__doc__},
+
+static PyObject *
+paramspec_has_default_impl(paramspecobject *self);
+
+static PyObject *
+paramspec_has_default(paramspecobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return paramspec_has_default_impl(self);
+}
+
PyDoc_STRVAR(typevartuple__doc__,
-"typevartuple(name)\n"
+"typevartuple(name, *, default=typing.NoDefault)\n"
"--\n"
"\n"
"Create a new TypeVarTuple with the given name.");
static PyObject *
-typevartuple_impl(PyTypeObject *type, PyObject *name);
+typevartuple_impl(PyTypeObject *type, PyObject *name,
+ PyObject *default_value);
static PyObject *
typevartuple(PyTypeObject *type, PyObject *args, PyObject *kwargs)
@@ -409,14 +489,14 @@ typevartuple(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
- #define NUM_KEYWORDS 1
+ #define NUM_KEYWORDS 2
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
- .ob_item = { &_Py_ID(name), },
+ .ob_item = { &_Py_ID(name), &_Py_ID(default), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
@@ -425,17 +505,19 @@ typevartuple(PyTypeObject *type, PyObject *args, PyObject *kwargs)
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
- static const char * const _keywords[] = {"name", NULL};
+ static const char * const _keywords[] = {"name", "default", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "typevartuple",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
- PyObject *argsbuf[1];
+ PyObject *argsbuf[2];
PyObject * const *fastargs;
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
PyObject *name;
+ PyObject *default_value = &_Py_NoDefaultStruct;
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
if (!fastargs) {
@@ -446,7 +528,12 @@ typevartuple(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto exit;
}
name = fastargs[0];
- return_value = typevartuple_impl(type, name);
+ if (!noptargs) {
+ goto skip_optional_kwonly;
+ }
+ default_value = fastargs[1];
+skip_optional_kwonly:
+ return_value = typevartuple_impl(type, name, default_value);
exit:
return return_value;
@@ -507,6 +594,23 @@ typevartuple_reduce(typevartupleobject *self, PyObject *Py_UNUSED(ignored))
return typevartuple_reduce_impl(self);
}
+PyDoc_STRVAR(typevartuple_has_default__doc__,
+"has_default($self, /)\n"
+"--\n"
+"\n");
+
+#define TYPEVARTUPLE_HAS_DEFAULT_METHODDEF \
+ {"has_default", (PyCFunction)typevartuple_has_default, METH_NOARGS, typevartuple_has_default__doc__},
+
+static PyObject *
+typevartuple_has_default_impl(typevartupleobject *self);
+
+static PyObject *
+typevartuple_has_default(typevartupleobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return typevartuple_has_default_impl(self);
+}
+
PyDoc_STRVAR(typealias_reduce__doc__,
"__reduce__($self, /)\n"
"--\n"
@@ -591,4 +695,4 @@ skip_optional_kwonly:
exit:
return return_value;
}
-/*[clinic end generated code: output=5a582d9d89ad787b input=a9049054013a1b77]*/
+/*[clinic end generated code: output=73b39e550e4e336c input=a9049054013a1b77]*/
diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c
index 7f80c9c61b..cc91604526 100644
--- a/Objects/typevarobject.c
+++ b/Objects/typevarobject.c
@@ -23,6 +23,8 @@ typedef struct {
PyObject *evaluate_bound;
PyObject *constraints;
PyObject *evaluate_constraints;
+ PyObject *default_value;
+ PyObject *evaluate_default;
bool covariant;
bool contravariant;
bool infer_variance;
@@ -31,12 +33,16 @@ typedef struct {
typedef struct {
PyObject_HEAD
PyObject *name;
+ PyObject *default_value;
+ PyObject *evaluate_default;
} typevartupleobject;
typedef struct {
PyObject_HEAD
PyObject *name;
PyObject *bound;
+ PyObject *default_value;
+ PyObject *evaluate_default;
bool covariant;
bool contravariant;
bool infer_variance;
@@ -53,6 +59,64 @@ typedef struct {
#include "clinic/typevarobject.c.h"
+/* NoDefault is a marker object to indicate that a parameter has no default. */
+
+static PyObject *
+NoDefault_repr(PyObject *op)
+{
+ return PyUnicode_FromString("typing.NoDefault");
+}
+
+static PyObject *
+NoDefault_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
+{
+ return PyUnicode_FromString("NoDefault");
+}
+
+static PyMethodDef notimplemented_methods[] = {
+ {"__reduce__", NoDefault_reduce, METH_NOARGS, NULL},
+ {NULL, NULL}
+};
+
+static PyObject *
+nodefault_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+ if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
+ PyErr_SetString(PyExc_TypeError, "NoDefaultType takes no arguments");
+ return NULL;
+ }
+ return &_Py_NoDefaultStruct;
+}
+
+static void
+nodefault_dealloc(PyObject *nodefault)
+{
+ /* This should never get called, but we also don't want to SEGV if
+ * we accidentally decref NoDefault out of existence. Instead,
+ * since NoDefault is an immortal object, re-set the reference count.
+ */
+ _Py_SetImmortal(nodefault);
+}
+
+PyDoc_STRVAR(notimplemented_doc,
+"NoDefaultType()\n"
+"--\n\n"
+"The type of the NoDefault singleton.");
+
+PyTypeObject _PyNoDefault_Type = {
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "NoDefaultType",
+ .tp_dealloc = nodefault_dealloc,
+ .tp_repr = NoDefault_repr,
+ .tp_flags = Py_TPFLAGS_DEFAULT,
+ .tp_doc = notimplemented_doc,
+ .tp_methods = notimplemented_methods,
+ .tp_new = nodefault_new,
+};
+
+PyObject _Py_NoDefaultStruct = _PyObject_HEAD_INIT(&_PyNoDefault_Type);
+
+
static PyObject *
call_typing_func_object(const char *name, PyObject **args, size_t nargs)
{
@@ -200,6 +264,8 @@ typevar_dealloc(PyObject *self)
Py_XDECREF(tv->evaluate_bound);
Py_XDECREF(tv->constraints);
Py_XDECREF(tv->evaluate_constraints);
+ Py_XDECREF(tv->default_value);
+ Py_XDECREF(tv->evaluate_default);
PyObject_ClearManagedDict(self);
PyObject_ClearWeakRefs(self);
@@ -216,6 +282,8 @@ typevar_traverse(PyObject *self, visitproc visit, void *arg)
Py_VISIT(tv->evaluate_bound);
Py_VISIT(tv->constraints);
Py_VISIT(tv->evaluate_constraints);
+ Py_VISIT(tv->default_value);
+ Py_VISIT(tv->evaluate_default);
PyObject_VisitManagedDict(self, visit, arg);
return 0;
}
@@ -227,6 +295,8 @@ typevar_clear(typevarobject *self)
Py_CLEAR(self->evaluate_bound);
Py_CLEAR(self->constraints);
Py_CLEAR(self->evaluate_constraints);
+ Py_CLEAR(self->default_value);
+ Py_CLEAR(self->evaluate_default);
PyObject_ClearManagedDict((PyObject *)self);
return 0;
}
@@ -266,6 +336,20 @@ typevar_bound(typevarobject *self, void *Py_UNUSED(ignored))
return bound;
}
+static PyObject *
+typevar_default(typevarobject *self, void *unused)
+{
+ if (self->default_value != NULL) {
+ return Py_NewRef(self->default_value);
+ }
+ if (self->evaluate_default == NULL) {
+ return &_Py_NoDefaultStruct;
+ }
+ PyObject *default_value = PyObject_CallNoArgs(self->evaluate_default);
+ self->default_value = Py_XNewRef(default_value);
+ return default_value;
+}
+
static PyObject *
typevar_constraints(typevarobject *self, void *Py_UNUSED(ignored))
{
@@ -283,12 +367,14 @@ typevar_constraints(typevarobject *self, void *Py_UNUSED(ignored))
static PyGetSetDef typevar_getset[] = {
{"__bound__", (getter)typevar_bound, NULL, NULL, NULL},
{"__constraints__", (getter)typevar_constraints, NULL, NULL, NULL},
+ {"__default__", (getter)typevar_default, NULL, NULL, NULL},
{0}
};
static typevarobject *
typevar_alloc(PyObject *name, PyObject *bound, PyObject *evaluate_bound,
PyObject *constraints, PyObject *evaluate_constraints,
+ PyObject *default_value,
bool covariant, bool contravariant, bool infer_variance,
PyObject *module)
{
@@ -305,6 +391,8 @@ typevar_alloc(PyObject *name, PyObject *bound, PyObject *evaluate_bound,
tv->evaluate_bound = Py_XNewRef(evaluate_bound);
tv->constraints = Py_XNewRef(constraints);
tv->evaluate_constraints = Py_XNewRef(evaluate_constraints);
+ tv->default_value = Py_XNewRef(default_value);
+ tv->evaluate_default = NULL;
tv->covariant = covariant;
tv->contravariant = contravariant;
@@ -328,6 +416,7 @@ typevar.__new__ as typevar_new
name: object(subclass_of="&PyUnicode_Type")
*constraints: object
bound: object = None
+ default as default_value: object(c_default="&_Py_NoDefaultStruct") = typing.NoDefault
covariant: bool = False
contravariant: bool = False
infer_variance: bool = False
@@ -337,9 +426,9 @@ Create a TypeVar.
static PyObject *
typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
- PyObject *bound, int covariant, int contravariant,
- int infer_variance)
-/*[clinic end generated code: output=1d200450ee99226d input=41ae33a916bfe76f]*/
+ PyObject *bound, PyObject *default_value, int covariant,
+ int contravariant, int infer_variance)
+/*[clinic end generated code: output=d2b248ff074eaab6 input=836f97f631d7293a]*/
{
if (covariant && contravariant) {
PyErr_SetString(PyExc_ValueError,
@@ -386,6 +475,7 @@ typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
PyObject *tv = (PyObject *)typevar_alloc(name, bound, NULL,
constraints, NULL,
+ default_value,
covariant, contravariant,
infer_variance, module);
Py_XDECREF(bound);
@@ -410,6 +500,66 @@ typevar_typing_subst(typevarobject *self, PyObject *arg)
return result;
}
+/*[clinic input]
+typevar.__typing_prepare_subst__ as typevar_typing_prepare_subst
+
+ alias: object
+ args: object
+ /
+
+[clinic start generated code]*/
+
+static PyObject *
+typevar_typing_prepare_subst_impl(typevarobject *self, PyObject *alias,
+ PyObject *args)
+/*[clinic end generated code: output=82c3f4691e0ded22 input=201a750415d14ffb]*/
+{
+ PyObject *params = PyObject_GetAttrString(alias, "__parameters__");
+ if (params == NULL) {
+ return NULL;
+ }
+ Py_ssize_t i = PySequence_Index(params, (PyObject *)self);
+ if (i == -1) {
+ Py_DECREF(params);
+ return NULL;
+ }
+ Py_ssize_t args_len = PySequence_Length(args);
+ if (args_len == -1) {
+ Py_DECREF(params);
+ return NULL;
+ }
+ if (i < args_len) {
+ // We already have a value for our TypeVar
+ Py_DECREF(params);
+ return Py_NewRef(args);
+ }
+ else if (i == args_len) {
+ // If the TypeVar has a default, use it.
+ PyObject *dflt = typevar_default(self, NULL);
+ if (dflt == NULL) {
+ Py_DECREF(params);
+ return NULL;
+ }
+ if (dflt != &_Py_NoDefaultStruct) {
+ PyObject *new_args = PyTuple_Pack(1, dflt);
+ Py_DECREF(dflt);
+ if (new_args == NULL) {
+ Py_DECREF(params);
+ return NULL;
+ }
+ PyObject *result = PySequence_Concat(args, new_args);
+ Py_DECREF(params);
+ Py_DECREF(new_args);
+ return result;
+ }
+ }
+ Py_DECREF(params);
+ PyErr_Format(PyExc_TypeError,
+ "Too few arguments for %S; actual %d, expected at least %d",
+ alias, args_len, i + 1);
+ return NULL;
+}
+
/*[clinic input]
typevar.__reduce__ as typevar_reduce
@@ -422,6 +572,23 @@ typevar_reduce_impl(typevarobject *self)
return Py_NewRef(self->name);
}
+
+/*[clinic input]
+typevar.has_default as typevar_has_default
+
+[clinic start generated code]*/
+
+static PyObject *
+typevar_has_default_impl(typevarobject *self)
+/*[clinic end generated code: output=76bf0b8dc98b97dd input=31024aa030761cf6]*/
+{
+ if (self->evaluate_default != NULL ||
+ (self->default_value != &_Py_NoDefaultStruct && self->default_value != NULL)) {
+ Py_RETURN_TRUE;
+ }
+ Py_RETURN_FALSE;
+}
+
static PyObject *
typevar_mro_entries(PyObject *self, PyObject *args)
{
@@ -432,7 +599,9 @@ typevar_mro_entries(PyObject *self, PyObject *args)
static PyMethodDef typevar_methods[] = {
TYPEVAR_TYPING_SUBST_METHODDEF
+ TYPEVAR_TYPING_PREPARE_SUBST_METHODDEF
TYPEVAR_REDUCE_METHODDEF
+ TYPEVAR_HAS_DEFAULT_METHODDEF
{"__mro_entries__", typevar_mro_entries, METH_O},
{0}
};
@@ -457,12 +626,18 @@ variables::\n\
class StrOrBytesSequence[A: (str, bytes)]:\n\
...\n\
\n\
+Type variables can also have defaults:\n\
+\n\
+ class IntDefault[T = int]:\n\
+ ...\n\
+\n\
However, if desired, reusable type variables can also be constructed\n\
manually, like so::\n\
\n\
T = TypeVar('T') # Can be anything\n\
S = TypeVar('S', bound=str) # Can be any subtype of str\n\
A = TypeVar('A', str, bytes) # Must be exactly str or bytes\n\
+ D = TypeVar('D', default=int) # Defaults to int\n\
\n\
Type variables exist primarily for the benefit of static type\n\
checkers. They serve as the parameters for generic types as well\n\
@@ -739,6 +914,8 @@ paramspec_dealloc(PyObject *self)
Py_DECREF(ps->name);
Py_XDECREF(ps->bound);
+ Py_XDECREF(ps->default_value);
+ Py_XDECREF(ps->evaluate_default);
PyObject_ClearManagedDict(self);
PyObject_ClearWeakRefs(self);
@@ -752,6 +929,8 @@ paramspec_traverse(PyObject *self, visitproc visit, void *arg)
Py_VISIT(Py_TYPE(self));
paramspecobject *ps = (paramspecobject *)self;
Py_VISIT(ps->bound);
+ Py_VISIT(ps->default_value);
+ Py_VISIT(ps->evaluate_default);
PyObject_VisitManagedDict(self, visit, arg);
return 0;
}
@@ -760,6 +939,8 @@ static int
paramspec_clear(paramspecobject *self)
{
Py_CLEAR(self->bound);
+ Py_CLEAR(self->default_value);
+ Py_CLEAR(self->evaluate_default);
PyObject_ClearManagedDict((PyObject *)self);
return 0;
}
@@ -800,14 +981,29 @@ paramspec_kwargs(PyObject *self, void *unused)
return (PyObject *)paramspecattr_new(tp, self);
}
+static PyObject *
+paramspec_default(paramspecobject *self, void *unused)
+{
+ if (self->default_value != NULL) {
+ return Py_NewRef(self->default_value);
+ }
+ if (self->evaluate_default == NULL) {
+ return &_Py_NoDefaultStruct;
+ }
+ PyObject *default_value = PyObject_CallNoArgs(self->evaluate_default);
+ self->default_value = Py_XNewRef(default_value);
+ return default_value;
+}
+
static PyGetSetDef paramspec_getset[] = {
{"args", (getter)paramspec_args, NULL, PyDoc_STR("Represents positional arguments."), NULL},
{"kwargs", (getter)paramspec_kwargs, NULL, PyDoc_STR("Represents keyword arguments."), NULL},
+ {"__default__", (getter)paramspec_default, NULL, "The default value for this ParamSpec.", NULL},
{0},
};
static paramspecobject *
-paramspec_alloc(PyObject *name, PyObject *bound, bool covariant,
+paramspec_alloc(PyObject *name, PyObject *bound, PyObject *default_value, bool covariant,
bool contravariant, bool infer_variance, PyObject *module)
{
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.paramspec_type;
@@ -820,6 +1016,8 @@ paramspec_alloc(PyObject *name, PyObject *bound, bool covariant,
ps->covariant = covariant;
ps->contravariant = contravariant;
ps->infer_variance = infer_variance;
+ ps->default_value = Py_XNewRef(default_value);
+ ps->evaluate_default = NULL;
_PyObject_GC_TRACK(ps);
if (module != NULL) {
if (PyObject_SetAttrString((PyObject *)ps, "__module__", module) < 0) {
@@ -837,6 +1035,7 @@ paramspec.__new__ as paramspec_new
name: object(subclass_of="&PyUnicode_Type")
*
bound: object = None
+ default as default_value: object(c_default="&_Py_NoDefaultStruct") = typing.NoDefault
covariant: bool = False
contravariant: bool = False
infer_variance: bool = False
@@ -846,8 +1045,9 @@ Create a ParamSpec object.
static PyObject *
paramspec_new_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
- int covariant, int contravariant, int infer_variance)
-/*[clinic end generated code: output=fd2daab79cba62da input=57c49c581979b952]*/
+ PyObject *default_value, int covariant, int contravariant,
+ int infer_variance)
+/*[clinic end generated code: output=47ca9d63fa5a094d input=495e1565bc067ab9]*/
{
if (covariant && contravariant) {
PyErr_SetString(PyExc_ValueError, "Bivariant types are not supported.");
@@ -869,7 +1069,7 @@ paramspec_new_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
return NULL;
}
PyObject *ps = (PyObject *)paramspec_alloc(
- name, bound, covariant, contravariant, infer_variance, module);
+ name, bound, default_value, covariant, contravariant, infer_variance, module);
Py_XDECREF(bound);
Py_DECREF(module);
return ps;
@@ -925,6 +1125,22 @@ paramspec_reduce_impl(paramspecobject *self)
return Py_NewRef(self->name);
}
+/*[clinic input]
+paramspec.has_default as paramspec_has_default
+
+[clinic start generated code]*/
+
+static PyObject *
+paramspec_has_default_impl(paramspecobject *self)
+/*[clinic end generated code: output=daaae7467a6a4368 input=2112e97eeb76cd59]*/
+{
+ if (self->evaluate_default != NULL ||
+ (self->default_value != &_Py_NoDefaultStruct && self->default_value != NULL)) {
+ Py_RETURN_TRUE;
+ }
+ Py_RETURN_FALSE;
+}
+
static PyObject *
paramspec_mro_entries(PyObject *self, PyObject *args)
{
@@ -936,6 +1152,7 @@ paramspec_mro_entries(PyObject *self, PyObject *args)
static PyMethodDef paramspec_methods[] = {
PARAMSPEC_TYPING_SUBST_METHODDEF
PARAMSPEC_TYPING_PREPARE_SUBST_METHODDEF
+ PARAMSPEC_HAS_DEFAULT_METHODDEF
PARAMSPEC_REDUCE_METHODDEF
{"__mro_entries__", paramspec_mro_entries, METH_O},
{0}
@@ -950,10 +1167,17 @@ where the use of '**' creates a parameter specification::\n\
\n\
type IntFunc[**P] = Callable[P, int]\n\
\n\
+The following syntax creates a parameter specification that defaults\n\
+to a callable accepting two positional-only arguments of types int\n\
+and str:\n\
+\n\
+ type IntFuncDefault[**P = (int, str)] = Callable[P, int]\n\
+\n\
For compatibility with Python 3.11 and earlier, ParamSpec objects\n\
can also be created as follows::\n\
\n\
P = ParamSpec('P')\n\
+ DefaultP = ParamSpec('DefaultP', default=(int, str))\n\
\n\
Parameter specification variables exist primarily for the benefit of\n\
static type checkers. They are used to forward the parameter types of\n\
@@ -1021,6 +1245,8 @@ typevartuple_dealloc(PyObject *self)
typevartupleobject *tvt = (typevartupleobject *)self;
Py_DECREF(tvt->name);
+ Py_XDECREF(tvt->default_value);
+ Py_XDECREF(tvt->evaluate_default);
PyObject_ClearManagedDict(self);
PyObject_ClearWeakRefs(self);
@@ -1060,7 +1286,7 @@ static PyMemberDef typevartuple_members[] = {
};
static typevartupleobject *
-typevartuple_alloc(PyObject *name, PyObject *module)
+typevartuple_alloc(PyObject *name, PyObject *module, PyObject *default_value)
{
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.typevartuple_type;
typevartupleobject *tvt = PyObject_GC_New(typevartupleobject, tp);
@@ -1068,6 +1294,8 @@ typevartuple_alloc(PyObject *name, PyObject *module)
return NULL;
}
tvt->name = Py_NewRef(name);
+ tvt->default_value = Py_XNewRef(default_value);
+ tvt->evaluate_default = NULL;
_PyObject_GC_TRACK(tvt);
if (module != NULL) {
if (PyObject_SetAttrString((PyObject *)tvt, "__module__", module) < 0) {
@@ -1083,19 +1311,22 @@ typevartuple_alloc(PyObject *name, PyObject *module)
typevartuple.__new__
name: object(subclass_of="&PyUnicode_Type")
+ *
+ default as default_value: object(c_default="&_Py_NoDefaultStruct") = typing.NoDefault
Create a new TypeVarTuple with the given name.
[clinic start generated code]*/
static PyObject *
-typevartuple_impl(PyTypeObject *type, PyObject *name)
-/*[clinic end generated code: output=09d417a28f976202 input=00d28abcf1fc96bb]*/
+typevartuple_impl(PyTypeObject *type, PyObject *name,
+ PyObject *default_value)
+/*[clinic end generated code: output=9d6b76dfe95aae51 input=e149739929a866d0]*/
{
PyObject *module = caller();
if (module == NULL) {
return NULL;
}
- PyObject *result = (PyObject *)typevartuple_alloc(name, module);
+ PyObject *result = (PyObject *)typevartuple_alloc(name, module, default_value);
Py_DECREF(module);
return result;
}
@@ -1148,6 +1379,23 @@ typevartuple_reduce_impl(typevartupleobject *self)
return Py_NewRef(self->name);
}
+
+/*[clinic input]
+typevartuple.has_default as typevartuple_has_default
+
+[clinic start generated code]*/
+
+static PyObject *
+typevartuple_has_default_impl(typevartupleobject *self)
+/*[clinic end generated code: output=4895f602f56a5e29 input=9ef3250ddb2c1851]*/
+{
+ if (self->evaluate_default != NULL ||
+ (self->default_value != &_Py_NoDefaultStruct && self->default_value != NULL)) {
+ Py_RETURN_TRUE;
+ }
+ Py_RETURN_FALSE;
+}
+
static PyObject *
typevartuple_mro_entries(PyObject *self, PyObject *args)
{
@@ -1160,6 +1408,8 @@ static int
typevartuple_traverse(PyObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
+ Py_VISIT(((typevartupleobject *)self)->default_value);
+ Py_VISIT(((typevartupleobject *)self)->evaluate_default);
PyObject_VisitManagedDict(self, visit, arg);
return 0;
}
@@ -1167,14 +1417,36 @@ typevartuple_traverse(PyObject *self, visitproc visit, void *arg)
static int
typevartuple_clear(PyObject *self)
{
+ Py_CLEAR(((typevartupleobject *)self)->default_value);
+ Py_CLEAR(((typevartupleobject *)self)->evaluate_default);
PyObject_ClearManagedDict(self);
return 0;
}
+static PyObject *
+typevartuple_default(typevartupleobject *self, void *unused)
+{
+ if (self->default_value != NULL) {
+ return Py_NewRef(self->default_value);
+ }
+ if (self->evaluate_default == NULL) {
+ return &_Py_NoDefaultStruct;
+ }
+ PyObject *default_value = PyObject_CallNoArgs(self->evaluate_default);
+ self->default_value = Py_XNewRef(default_value);
+ return default_value;
+}
+
+static PyGetSetDef typevartuple_getset[] = {
+ {"__default__", (getter)typevartuple_default, NULL, "The default value for this TypeVarTuple.", NULL},
+ {0},
+};
+
static PyMethodDef typevartuple_methods[] = {
TYPEVARTUPLE_TYPING_SUBST_METHODDEF
TYPEVARTUPLE_TYPING_PREPARE_SUBST_METHODDEF
TYPEVARTUPLE_REDUCE_METHODDEF
+ TYPEVARTUPLE_HAS_DEFAULT_METHODDEF
{"__mro_entries__", typevartuple_mro_entries, METH_O},
{0}
};
@@ -1190,10 +1462,15 @@ where a single '*' indicates a type variable tuple::\n\
def move_first_element_to_last[T, *Ts](tup: tuple[T, *Ts]) -> tuple[*Ts, T]:\n\
return (*tup[1:], tup[0])\n\
\n\
+Type variables tuples can have default values:\n\
+\n\
+ type AliasWithDefault[*Ts = (str, int)] = tuple[*Ts]\n\
+\n\
For compatibility with Python 3.11 and earlier, TypeVarTuple objects\n\
can also be created as follows::\n\
\n\
Ts = TypeVarTuple('Ts') # Can be given any name\n\
+ DefaultTs = TypeVarTuple('Ts', default=(str, int))\n\
\n\
Just as a TypeVar (type variable) is a placeholder for a single type,\n\
a TypeVarTuple is a placeholder for an *arbitrary* number of types. For\n\
@@ -1218,6 +1495,7 @@ PyType_Slot typevartuple_slots[] = {
{Py_tp_doc, (void *)typevartuple_doc},
{Py_tp_members, typevartuple_members},
{Py_tp_methods, typevartuple_methods},
+ {Py_tp_getset, typevartuple_getset},
{Py_tp_new, typevartuple},
{Py_tp_iter, typevartuple_iter},
{Py_tp_repr, typevartuple_repr},
@@ -1241,21 +1519,21 @@ PyObject *
_Py_make_typevar(PyObject *name, PyObject *evaluate_bound, PyObject *evaluate_constraints)
{
return (PyObject *)typevar_alloc(name, NULL, evaluate_bound, NULL, evaluate_constraints,
- false, false, true, NULL);
+ NULL, false, false, true, NULL);
}
PyObject *
_Py_make_paramspec(PyThreadState *Py_UNUSED(ignored), PyObject *v)
{
assert(PyUnicode_Check(v));
- return (PyObject *)paramspec_alloc(v, NULL, false, false, true, NULL);
+ return (PyObject *)paramspec_alloc(v, NULL, NULL, false, false, true, NULL);
}
PyObject *
_Py_make_typevartuple(PyThreadState *Py_UNUSED(ignored), PyObject *v)
{
assert(PyUnicode_Check(v));
- return (PyObject *)typevartuple_alloc(v, NULL);
+ return (PyObject *)typevartuple_alloc(v, NULL, NULL);
}
static void
@@ -1687,3 +1965,24 @@ void _Py_clear_generic_types(PyInterpreterState *interp)
Py_CLEAR(interp->cached_objects.paramspecargs_type);
Py_CLEAR(interp->cached_objects.paramspeckwargs_type);
}
+
+PyObject *
+_Py_set_typeparam_default(PyThreadState *ts, PyObject *typeparam, PyObject *evaluate_default)
+{
+ if (Py_IS_TYPE(typeparam, ts->interp->cached_objects.typevar_type)) {
+ Py_XSETREF(((typevarobject *)typeparam)->evaluate_default, Py_NewRef(evaluate_default));
+ return Py_NewRef(typeparam);
+ }
+ else if (Py_IS_TYPE(typeparam, ts->interp->cached_objects.paramspec_type)) {
+ Py_XSETREF(((paramspecobject *)typeparam)->evaluate_default, Py_NewRef(evaluate_default));
+ return Py_NewRef(typeparam);
+ }
+ else if (Py_IS_TYPE(typeparam, ts->interp->cached_objects.typevartuple_type)) {
+ Py_XSETREF(((typevartupleobject *)typeparam)->evaluate_default, Py_NewRef(evaluate_default));
+ return Py_NewRef(typeparam);
+ }
+ else {
+ PyErr_Format(PyExc_TypeError, "Expected a type param, got %R", typeparam);
+ return NULL;
+ }
+}
diff --git a/Parser/Python.asdl b/Parser/Python.asdl
index 0d15486727..80776ffe44 100644
--- a/Parser/Python.asdl
+++ b/Parser/Python.asdl
@@ -145,8 +145,8 @@ module Python
type_ignore = TypeIgnore(int lineno, string tag)
- type_param = TypeVar(identifier name, expr? bound)
- | ParamSpec(identifier name)
- | TypeVarTuple(identifier name)
+ type_param = TypeVar(identifier name, expr? bound, expr? default_value)
+ | ParamSpec(identifier name, expr? default_value)
+ | TypeVarTuple(identifier name, expr? default_value)
attributes (int lineno, int col_offset, int end_lineno, int end_col_offset)
}
diff --git a/Parser/parser.c b/Parser/parser.c
index 0715e9775a..e34fcada15 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -186,431 +186,433 @@ static char *soft_keywords[] = {
#define type_param_seq_type 1099
#define type_param_type 1100
#define type_param_bound_type 1101
-#define expressions_type 1102
-#define expression_type 1103
-#define yield_expr_type 1104
-#define star_expressions_type 1105
-#define star_expression_type 1106
-#define star_named_expressions_type 1107
-#define star_named_expression_type 1108
-#define assignment_expression_type 1109
-#define named_expression_type 1110
-#define disjunction_type 1111
-#define conjunction_type 1112
-#define inversion_type 1113
-#define comparison_type 1114
-#define compare_op_bitwise_or_pair_type 1115
-#define eq_bitwise_or_type 1116
-#define noteq_bitwise_or_type 1117
-#define lte_bitwise_or_type 1118
-#define lt_bitwise_or_type 1119
-#define gte_bitwise_or_type 1120
-#define gt_bitwise_or_type 1121
-#define notin_bitwise_or_type 1122
-#define in_bitwise_or_type 1123
-#define isnot_bitwise_or_type 1124
-#define is_bitwise_or_type 1125
-#define bitwise_or_type 1126 // Left-recursive
-#define bitwise_xor_type 1127 // Left-recursive
-#define bitwise_and_type 1128 // Left-recursive
-#define shift_expr_type 1129 // Left-recursive
-#define sum_type 1130 // Left-recursive
-#define term_type 1131 // Left-recursive
-#define factor_type 1132
-#define power_type 1133
-#define await_primary_type 1134
-#define primary_type 1135 // Left-recursive
-#define slices_type 1136
-#define slice_type 1137
-#define atom_type 1138
-#define group_type 1139
-#define lambdef_type 1140
-#define lambda_params_type 1141
-#define lambda_parameters_type 1142
-#define lambda_slash_no_default_type 1143
-#define lambda_slash_with_default_type 1144
-#define lambda_star_etc_type 1145
-#define lambda_kwds_type 1146
-#define lambda_param_no_default_type 1147
-#define lambda_param_with_default_type 1148
-#define lambda_param_maybe_default_type 1149
-#define lambda_param_type 1150
-#define fstring_middle_type 1151
-#define fstring_replacement_field_type 1152
-#define fstring_conversion_type 1153
-#define fstring_full_format_spec_type 1154
-#define fstring_format_spec_type 1155
-#define fstring_type 1156
-#define string_type 1157
-#define strings_type 1158
-#define list_type 1159
-#define tuple_type 1160
-#define set_type 1161
-#define dict_type 1162
-#define double_starred_kvpairs_type 1163
-#define double_starred_kvpair_type 1164
-#define kvpair_type 1165
-#define for_if_clauses_type 1166
-#define for_if_clause_type 1167
-#define listcomp_type 1168
-#define setcomp_type 1169
-#define genexp_type 1170
-#define dictcomp_type 1171
-#define arguments_type 1172
-#define args_type 1173
-#define kwargs_type 1174
-#define starred_expression_type 1175
-#define kwarg_or_starred_type 1176
-#define kwarg_or_double_starred_type 1177
-#define star_targets_type 1178
-#define star_targets_list_seq_type 1179
-#define star_targets_tuple_seq_type 1180
-#define star_target_type 1181
-#define target_with_star_atom_type 1182
-#define star_atom_type 1183
-#define single_target_type 1184
-#define single_subscript_attribute_target_type 1185
-#define t_primary_type 1186 // Left-recursive
-#define t_lookahead_type 1187
-#define del_targets_type 1188
-#define del_target_type 1189
-#define del_t_atom_type 1190
-#define type_expressions_type 1191
-#define func_type_comment_type 1192
-#define invalid_arguments_type 1193
-#define invalid_kwarg_type 1194
-#define expression_without_invalid_type 1195
-#define invalid_legacy_expression_type 1196
-#define invalid_expression_type 1197
-#define invalid_named_expression_type 1198
-#define invalid_assignment_type 1199
-#define invalid_ann_assign_target_type 1200
-#define invalid_del_stmt_type 1201
-#define invalid_block_type 1202
-#define invalid_comprehension_type 1203
-#define invalid_dict_comprehension_type 1204
-#define invalid_parameters_type 1205
-#define invalid_default_type 1206
-#define invalid_star_etc_type 1207
-#define invalid_kwds_type 1208
-#define invalid_parameters_helper_type 1209
-#define invalid_lambda_parameters_type 1210
-#define invalid_lambda_parameters_helper_type 1211
-#define invalid_lambda_star_etc_type 1212
-#define invalid_lambda_kwds_type 1213
-#define invalid_double_type_comments_type 1214
-#define invalid_with_item_type 1215
-#define invalid_for_target_type 1216
-#define invalid_group_type 1217
-#define invalid_import_type 1218
-#define invalid_import_from_targets_type 1219
-#define invalid_compound_stmt_type 1220
-#define invalid_with_stmt_type 1221
-#define invalid_with_stmt_indent_type 1222
-#define invalid_try_stmt_type 1223
-#define invalid_except_stmt_type 1224
-#define invalid_finally_stmt_type 1225
-#define invalid_except_stmt_indent_type 1226
-#define invalid_except_star_stmt_indent_type 1227
-#define invalid_match_stmt_type 1228
-#define invalid_case_block_type 1229
-#define invalid_as_pattern_type 1230
-#define invalid_class_pattern_type 1231
-#define invalid_class_argument_pattern_type 1232
-#define invalid_if_stmt_type 1233
-#define invalid_elif_stmt_type 1234
-#define invalid_else_stmt_type 1235
-#define invalid_while_stmt_type 1236
-#define invalid_for_stmt_type 1237
-#define invalid_def_raw_type 1238
-#define invalid_class_def_raw_type 1239
-#define invalid_double_starred_kvpairs_type 1240
-#define invalid_kvpair_type 1241
-#define invalid_starred_expression_type 1242
-#define invalid_replacement_field_type 1243
-#define invalid_conversion_character_type 1244
-#define invalid_arithmetic_type 1245
-#define invalid_factor_type 1246
-#define _loop0_1_type 1247
-#define _loop0_2_type 1248
-#define _loop1_3_type 1249
-#define _loop0_5_type 1250
-#define _gather_4_type 1251
-#define _tmp_6_type 1252
-#define _tmp_7_type 1253
-#define _tmp_8_type 1254
-#define _tmp_9_type 1255
-#define _tmp_10_type 1256
-#define _tmp_11_type 1257
-#define _tmp_12_type 1258
-#define _tmp_13_type 1259
-#define _loop1_14_type 1260
-#define _tmp_15_type 1261
-#define _tmp_16_type 1262
-#define _tmp_17_type 1263
-#define _loop0_19_type 1264
-#define _gather_18_type 1265
-#define _loop0_21_type 1266
-#define _gather_20_type 1267
-#define _tmp_22_type 1268
-#define _tmp_23_type 1269
-#define _loop0_24_type 1270
-#define _loop1_25_type 1271
-#define _loop0_27_type 1272
-#define _gather_26_type 1273
-#define _tmp_28_type 1274
-#define _loop0_30_type 1275
-#define _gather_29_type 1276
-#define _tmp_31_type 1277
-#define _loop1_32_type 1278
-#define _tmp_33_type 1279
-#define _tmp_34_type 1280
-#define _tmp_35_type 1281
-#define _loop0_36_type 1282
-#define _loop0_37_type 1283
-#define _loop0_38_type 1284
-#define _loop1_39_type 1285
-#define _loop0_40_type 1286
-#define _loop1_41_type 1287
-#define _loop1_42_type 1288
-#define _loop1_43_type 1289
-#define _loop0_44_type 1290
-#define _loop1_45_type 1291
-#define _loop0_46_type 1292
-#define _loop1_47_type 1293
-#define _loop0_48_type 1294
-#define _loop0_49_type 1295
-#define _loop1_50_type 1296
-#define _loop0_52_type 1297
-#define _gather_51_type 1298
-#define _loop0_54_type 1299
-#define _gather_53_type 1300
-#define _loop0_56_type 1301
-#define _gather_55_type 1302
-#define _loop0_58_type 1303
-#define _gather_57_type 1304
-#define _tmp_59_type 1305
-#define _loop1_60_type 1306
-#define _loop1_61_type 1307
-#define _tmp_62_type 1308
-#define _tmp_63_type 1309
-#define _loop1_64_type 1310
-#define _loop0_66_type 1311
-#define _gather_65_type 1312
-#define _tmp_67_type 1313
-#define _tmp_68_type 1314
-#define _tmp_69_type 1315
-#define _tmp_70_type 1316
-#define _loop0_72_type 1317
-#define _gather_71_type 1318
-#define _loop0_74_type 1319
-#define _gather_73_type 1320
-#define _tmp_75_type 1321
-#define _loop0_77_type 1322
-#define _gather_76_type 1323
-#define _loop0_79_type 1324
-#define _gather_78_type 1325
-#define _loop0_81_type 1326
-#define _gather_80_type 1327
-#define _loop1_82_type 1328
-#define _loop1_83_type 1329
-#define _loop0_85_type 1330
-#define _gather_84_type 1331
-#define _loop1_86_type 1332
-#define _loop1_87_type 1333
-#define _loop1_88_type 1334
-#define _tmp_89_type 1335
-#define _loop0_91_type 1336
-#define _gather_90_type 1337
-#define _tmp_92_type 1338
-#define _tmp_93_type 1339
-#define _tmp_94_type 1340
-#define _tmp_95_type 1341
-#define _tmp_96_type 1342
-#define _tmp_97_type 1343
-#define _loop0_98_type 1344
-#define _loop0_99_type 1345
-#define _loop0_100_type 1346
-#define _loop1_101_type 1347
-#define _loop0_102_type 1348
-#define _loop1_103_type 1349
-#define _loop1_104_type 1350
-#define _loop1_105_type 1351
-#define _loop0_106_type 1352
-#define _loop1_107_type 1353
-#define _loop0_108_type 1354
-#define _loop1_109_type 1355
-#define _loop0_110_type 1356
-#define _loop1_111_type 1357
-#define _loop0_112_type 1358
-#define _loop0_113_type 1359
-#define _loop1_114_type 1360
-#define _tmp_115_type 1361
-#define _loop0_117_type 1362
-#define _gather_116_type 1363
-#define _loop1_118_type 1364
-#define _loop0_119_type 1365
-#define _loop0_120_type 1366
-#define _tmp_121_type 1367
-#define _tmp_122_type 1368
-#define _loop0_124_type 1369
-#define _gather_123_type 1370
-#define _tmp_125_type 1371
-#define _loop0_127_type 1372
-#define _gather_126_type 1373
-#define _loop0_129_type 1374
-#define _gather_128_type 1375
-#define _loop0_131_type 1376
-#define _gather_130_type 1377
-#define _loop0_133_type 1378
-#define _gather_132_type 1379
-#define _loop0_134_type 1380
-#define _loop0_136_type 1381
-#define _gather_135_type 1382
-#define _loop1_137_type 1383
-#define _tmp_138_type 1384
-#define _loop0_140_type 1385
-#define _gather_139_type 1386
-#define _loop0_142_type 1387
-#define _gather_141_type 1388
-#define _loop0_144_type 1389
-#define _gather_143_type 1390
-#define _loop0_146_type 1391
-#define _gather_145_type 1392
-#define _loop0_148_type 1393
-#define _gather_147_type 1394
-#define _tmp_149_type 1395
-#define _tmp_150_type 1396
-#define _loop0_152_type 1397
-#define _gather_151_type 1398
-#define _tmp_153_type 1399
-#define _tmp_154_type 1400
-#define _tmp_155_type 1401
-#define _tmp_156_type 1402
-#define _tmp_157_type 1403
-#define _tmp_158_type 1404
-#define _tmp_159_type 1405
-#define _tmp_160_type 1406
-#define _tmp_161_type 1407
-#define _tmp_162_type 1408
-#define _loop0_163_type 1409
-#define _loop0_164_type 1410
-#define _loop0_165_type 1411
-#define _tmp_166_type 1412
-#define _tmp_167_type 1413
-#define _tmp_168_type 1414
-#define _tmp_169_type 1415
-#define _loop0_170_type 1416
-#define _loop0_171_type 1417
-#define _loop0_172_type 1418
-#define _loop1_173_type 1419
-#define _tmp_174_type 1420
-#define _loop0_175_type 1421
-#define _tmp_176_type 1422
-#define _loop0_177_type 1423
-#define _loop1_178_type 1424
-#define _tmp_179_type 1425
-#define _tmp_180_type 1426
-#define _tmp_181_type 1427
-#define _loop0_182_type 1428
-#define _tmp_183_type 1429
-#define _tmp_184_type 1430
-#define _loop1_185_type 1431
-#define _tmp_186_type 1432
-#define _loop0_187_type 1433
-#define _loop0_188_type 1434
-#define _loop0_189_type 1435
-#define _loop0_191_type 1436
-#define _gather_190_type 1437
-#define _tmp_192_type 1438
-#define _loop0_193_type 1439
-#define _tmp_194_type 1440
-#define _loop0_195_type 1441
-#define _loop1_196_type 1442
-#define _loop1_197_type 1443
-#define _tmp_198_type 1444
-#define _tmp_199_type 1445
-#define _loop0_200_type 1446
-#define _tmp_201_type 1447
-#define _tmp_202_type 1448
-#define _tmp_203_type 1449
-#define _loop0_205_type 1450
-#define _gather_204_type 1451
-#define _loop0_207_type 1452
-#define _gather_206_type 1453
-#define _loop0_209_type 1454
-#define _gather_208_type 1455
-#define _loop0_211_type 1456
-#define _gather_210_type 1457
-#define _loop0_213_type 1458
-#define _gather_212_type 1459
-#define _tmp_214_type 1460
-#define _loop0_215_type 1461
-#define _loop1_216_type 1462
-#define _tmp_217_type 1463
-#define _loop0_218_type 1464
-#define _loop1_219_type 1465
-#define _tmp_220_type 1466
-#define _tmp_221_type 1467
-#define _tmp_222_type 1468
-#define _tmp_223_type 1469
-#define _tmp_224_type 1470
-#define _tmp_225_type 1471
-#define _tmp_226_type 1472
-#define _tmp_227_type 1473
-#define _tmp_228_type 1474
-#define _tmp_229_type 1475
-#define _loop0_231_type 1476
-#define _gather_230_type 1477
-#define _tmp_232_type 1478
-#define _tmp_233_type 1479
-#define _tmp_234_type 1480
-#define _tmp_235_type 1481
-#define _tmp_236_type 1482
-#define _tmp_237_type 1483
-#define _tmp_238_type 1484
-#define _loop0_239_type 1485
-#define _tmp_240_type 1486
-#define _tmp_241_type 1487
-#define _tmp_242_type 1488
-#define _tmp_243_type 1489
-#define _tmp_244_type 1490
-#define _tmp_245_type 1491
-#define _tmp_246_type 1492
-#define _tmp_247_type 1493
-#define _tmp_248_type 1494
-#define _tmp_249_type 1495
-#define _tmp_250_type 1496
-#define _tmp_251_type 1497
-#define _tmp_252_type 1498
-#define _tmp_253_type 1499
-#define _tmp_254_type 1500
-#define _tmp_255_type 1501
-#define _loop0_256_type 1502
-#define _tmp_257_type 1503
-#define _tmp_258_type 1504
-#define _tmp_259_type 1505
-#define _tmp_260_type 1506
-#define _tmp_261_type 1507
-#define _tmp_262_type 1508
-#define _tmp_263_type 1509
-#define _tmp_264_type 1510
-#define _tmp_265_type 1511
-#define _tmp_266_type 1512
-#define _tmp_267_type 1513
-#define _tmp_268_type 1514
-#define _tmp_269_type 1515
-#define _tmp_270_type 1516
-#define _tmp_271_type 1517
-#define _tmp_272_type 1518
-#define _loop0_274_type 1519
-#define _gather_273_type 1520
-#define _tmp_275_type 1521
-#define _tmp_276_type 1522
-#define _tmp_277_type 1523
-#define _tmp_278_type 1524
-#define _tmp_279_type 1525
-#define _tmp_280_type 1526
+#define type_param_default_type 1102
+#define type_param_starred_default_type 1103
+#define expressions_type 1104
+#define expression_type 1105
+#define yield_expr_type 1106
+#define star_expressions_type 1107
+#define star_expression_type 1108
+#define star_named_expressions_type 1109
+#define star_named_expression_type 1110
+#define assignment_expression_type 1111
+#define named_expression_type 1112
+#define disjunction_type 1113
+#define conjunction_type 1114
+#define inversion_type 1115
+#define comparison_type 1116
+#define compare_op_bitwise_or_pair_type 1117
+#define eq_bitwise_or_type 1118
+#define noteq_bitwise_or_type 1119
+#define lte_bitwise_or_type 1120
+#define lt_bitwise_or_type 1121
+#define gte_bitwise_or_type 1122
+#define gt_bitwise_or_type 1123
+#define notin_bitwise_or_type 1124
+#define in_bitwise_or_type 1125
+#define isnot_bitwise_or_type 1126
+#define is_bitwise_or_type 1127
+#define bitwise_or_type 1128 // Left-recursive
+#define bitwise_xor_type 1129 // Left-recursive
+#define bitwise_and_type 1130 // Left-recursive
+#define shift_expr_type 1131 // Left-recursive
+#define sum_type 1132 // Left-recursive
+#define term_type 1133 // Left-recursive
+#define factor_type 1134
+#define power_type 1135
+#define await_primary_type 1136
+#define primary_type 1137 // Left-recursive
+#define slices_type 1138
+#define slice_type 1139
+#define atom_type 1140
+#define group_type 1141
+#define lambdef_type 1142
+#define lambda_params_type 1143
+#define lambda_parameters_type 1144
+#define lambda_slash_no_default_type 1145
+#define lambda_slash_with_default_type 1146
+#define lambda_star_etc_type 1147
+#define lambda_kwds_type 1148
+#define lambda_param_no_default_type 1149
+#define lambda_param_with_default_type 1150
+#define lambda_param_maybe_default_type 1151
+#define lambda_param_type 1152
+#define fstring_middle_type 1153
+#define fstring_replacement_field_type 1154
+#define fstring_conversion_type 1155
+#define fstring_full_format_spec_type 1156
+#define fstring_format_spec_type 1157
+#define fstring_type 1158
+#define string_type 1159
+#define strings_type 1160
+#define list_type 1161
+#define tuple_type 1162
+#define set_type 1163
+#define dict_type 1164
+#define double_starred_kvpairs_type 1165
+#define double_starred_kvpair_type 1166
+#define kvpair_type 1167
+#define for_if_clauses_type 1168
+#define for_if_clause_type 1169
+#define listcomp_type 1170
+#define setcomp_type 1171
+#define genexp_type 1172
+#define dictcomp_type 1173
+#define arguments_type 1174
+#define args_type 1175
+#define kwargs_type 1176
+#define starred_expression_type 1177
+#define kwarg_or_starred_type 1178
+#define kwarg_or_double_starred_type 1179
+#define star_targets_type 1180
+#define star_targets_list_seq_type 1181
+#define star_targets_tuple_seq_type 1182
+#define star_target_type 1183
+#define target_with_star_atom_type 1184
+#define star_atom_type 1185
+#define single_target_type 1186
+#define single_subscript_attribute_target_type 1187
+#define t_primary_type 1188 // Left-recursive
+#define t_lookahead_type 1189
+#define del_targets_type 1190
+#define del_target_type 1191
+#define del_t_atom_type 1192
+#define type_expressions_type 1193
+#define func_type_comment_type 1194
+#define invalid_arguments_type 1195
+#define invalid_kwarg_type 1196
+#define expression_without_invalid_type 1197
+#define invalid_legacy_expression_type 1198
+#define invalid_expression_type 1199
+#define invalid_named_expression_type 1200
+#define invalid_assignment_type 1201
+#define invalid_ann_assign_target_type 1202
+#define invalid_del_stmt_type 1203
+#define invalid_block_type 1204
+#define invalid_comprehension_type 1205
+#define invalid_dict_comprehension_type 1206
+#define invalid_parameters_type 1207
+#define invalid_default_type 1208
+#define invalid_star_etc_type 1209
+#define invalid_kwds_type 1210
+#define invalid_parameters_helper_type 1211
+#define invalid_lambda_parameters_type 1212
+#define invalid_lambda_parameters_helper_type 1213
+#define invalid_lambda_star_etc_type 1214
+#define invalid_lambda_kwds_type 1215
+#define invalid_double_type_comments_type 1216
+#define invalid_with_item_type 1217
+#define invalid_for_target_type 1218
+#define invalid_group_type 1219
+#define invalid_import_type 1220
+#define invalid_import_from_targets_type 1221
+#define invalid_compound_stmt_type 1222
+#define invalid_with_stmt_type 1223
+#define invalid_with_stmt_indent_type 1224
+#define invalid_try_stmt_type 1225
+#define invalid_except_stmt_type 1226
+#define invalid_finally_stmt_type 1227
+#define invalid_except_stmt_indent_type 1228
+#define invalid_except_star_stmt_indent_type 1229
+#define invalid_match_stmt_type 1230
+#define invalid_case_block_type 1231
+#define invalid_as_pattern_type 1232
+#define invalid_class_pattern_type 1233
+#define invalid_class_argument_pattern_type 1234
+#define invalid_if_stmt_type 1235
+#define invalid_elif_stmt_type 1236
+#define invalid_else_stmt_type 1237
+#define invalid_while_stmt_type 1238
+#define invalid_for_stmt_type 1239
+#define invalid_def_raw_type 1240
+#define invalid_class_def_raw_type 1241
+#define invalid_double_starred_kvpairs_type 1242
+#define invalid_kvpair_type 1243
+#define invalid_starred_expression_type 1244
+#define invalid_replacement_field_type 1245
+#define invalid_conversion_character_type 1246
+#define invalid_arithmetic_type 1247
+#define invalid_factor_type 1248
+#define _loop0_1_type 1249
+#define _loop0_2_type 1250
+#define _loop1_3_type 1251
+#define _loop0_5_type 1252
+#define _gather_4_type 1253
+#define _tmp_6_type 1254
+#define _tmp_7_type 1255
+#define _tmp_8_type 1256
+#define _tmp_9_type 1257
+#define _tmp_10_type 1258
+#define _tmp_11_type 1259
+#define _tmp_12_type 1260
+#define _tmp_13_type 1261
+#define _loop1_14_type 1262
+#define _tmp_15_type 1263
+#define _tmp_16_type 1264
+#define _tmp_17_type 1265
+#define _loop0_19_type 1266
+#define _gather_18_type 1267
+#define _loop0_21_type 1268
+#define _gather_20_type 1269
+#define _tmp_22_type 1270
+#define _tmp_23_type 1271
+#define _loop0_24_type 1272
+#define _loop1_25_type 1273
+#define _loop0_27_type 1274
+#define _gather_26_type 1275
+#define _tmp_28_type 1276
+#define _loop0_30_type 1277
+#define _gather_29_type 1278
+#define _tmp_31_type 1279
+#define _loop1_32_type 1280
+#define _tmp_33_type 1281
+#define _tmp_34_type 1282
+#define _tmp_35_type 1283
+#define _loop0_36_type 1284
+#define _loop0_37_type 1285
+#define _loop0_38_type 1286
+#define _loop1_39_type 1287
+#define _loop0_40_type 1288
+#define _loop1_41_type 1289
+#define _loop1_42_type 1290
+#define _loop1_43_type 1291
+#define _loop0_44_type 1292
+#define _loop1_45_type 1293
+#define _loop0_46_type 1294
+#define _loop1_47_type 1295
+#define _loop0_48_type 1296
+#define _loop0_49_type 1297
+#define _loop1_50_type 1298
+#define _loop0_52_type 1299
+#define _gather_51_type 1300
+#define _loop0_54_type 1301
+#define _gather_53_type 1302
+#define _loop0_56_type 1303
+#define _gather_55_type 1304
+#define _loop0_58_type 1305
+#define _gather_57_type 1306
+#define _tmp_59_type 1307
+#define _loop1_60_type 1308
+#define _loop1_61_type 1309
+#define _tmp_62_type 1310
+#define _tmp_63_type 1311
+#define _loop1_64_type 1312
+#define _loop0_66_type 1313
+#define _gather_65_type 1314
+#define _tmp_67_type 1315
+#define _tmp_68_type 1316
+#define _tmp_69_type 1317
+#define _tmp_70_type 1318
+#define _loop0_72_type 1319
+#define _gather_71_type 1320
+#define _loop0_74_type 1321
+#define _gather_73_type 1322
+#define _tmp_75_type 1323
+#define _loop0_77_type 1324
+#define _gather_76_type 1325
+#define _loop0_79_type 1326
+#define _gather_78_type 1327
+#define _loop0_81_type 1328
+#define _gather_80_type 1329
+#define _loop1_82_type 1330
+#define _loop1_83_type 1331
+#define _loop0_85_type 1332
+#define _gather_84_type 1333
+#define _loop1_86_type 1334
+#define _loop1_87_type 1335
+#define _loop1_88_type 1336
+#define _tmp_89_type 1337
+#define _loop0_91_type 1338
+#define _gather_90_type 1339
+#define _tmp_92_type 1340
+#define _tmp_93_type 1341
+#define _tmp_94_type 1342
+#define _tmp_95_type 1343
+#define _tmp_96_type 1344
+#define _tmp_97_type 1345
+#define _loop0_98_type 1346
+#define _loop0_99_type 1347
+#define _loop0_100_type 1348
+#define _loop1_101_type 1349
+#define _loop0_102_type 1350
+#define _loop1_103_type 1351
+#define _loop1_104_type 1352
+#define _loop1_105_type 1353
+#define _loop0_106_type 1354
+#define _loop1_107_type 1355
+#define _loop0_108_type 1356
+#define _loop1_109_type 1357
+#define _loop0_110_type 1358
+#define _loop1_111_type 1359
+#define _loop0_112_type 1360
+#define _loop0_113_type 1361
+#define _loop1_114_type 1362
+#define _tmp_115_type 1363
+#define _loop0_117_type 1364
+#define _gather_116_type 1365
+#define _loop1_118_type 1366
+#define _loop0_119_type 1367
+#define _loop0_120_type 1368
+#define _tmp_121_type 1369
+#define _tmp_122_type 1370
+#define _loop0_124_type 1371
+#define _gather_123_type 1372
+#define _tmp_125_type 1373
+#define _loop0_127_type 1374
+#define _gather_126_type 1375
+#define _loop0_129_type 1376
+#define _gather_128_type 1377
+#define _loop0_131_type 1378
+#define _gather_130_type 1379
+#define _loop0_133_type 1380
+#define _gather_132_type 1381
+#define _loop0_134_type 1382
+#define _loop0_136_type 1383
+#define _gather_135_type 1384
+#define _loop1_137_type 1385
+#define _tmp_138_type 1386
+#define _loop0_140_type 1387
+#define _gather_139_type 1388
+#define _loop0_142_type 1389
+#define _gather_141_type 1390
+#define _loop0_144_type 1391
+#define _gather_143_type 1392
+#define _loop0_146_type 1393
+#define _gather_145_type 1394
+#define _loop0_148_type 1395
+#define _gather_147_type 1396
+#define _tmp_149_type 1397
+#define _tmp_150_type 1398
+#define _loop0_152_type 1399
+#define _gather_151_type 1400
+#define _tmp_153_type 1401
+#define _tmp_154_type 1402
+#define _tmp_155_type 1403
+#define _tmp_156_type 1404
+#define _tmp_157_type 1405
+#define _tmp_158_type 1406
+#define _tmp_159_type 1407
+#define _tmp_160_type 1408
+#define _tmp_161_type 1409
+#define _tmp_162_type 1410
+#define _loop0_163_type 1411
+#define _loop0_164_type 1412
+#define _loop0_165_type 1413
+#define _tmp_166_type 1414
+#define _tmp_167_type 1415
+#define _tmp_168_type 1416
+#define _tmp_169_type 1417
+#define _loop0_170_type 1418
+#define _loop0_171_type 1419
+#define _loop0_172_type 1420
+#define _loop1_173_type 1421
+#define _tmp_174_type 1422
+#define _loop0_175_type 1423
+#define _tmp_176_type 1424
+#define _loop0_177_type 1425
+#define _loop1_178_type 1426
+#define _tmp_179_type 1427
+#define _tmp_180_type 1428
+#define _tmp_181_type 1429
+#define _loop0_182_type 1430
+#define _tmp_183_type 1431
+#define _tmp_184_type 1432
+#define _loop1_185_type 1433
+#define _tmp_186_type 1434
+#define _loop0_187_type 1435
+#define _loop0_188_type 1436
+#define _loop0_189_type 1437
+#define _loop0_191_type 1438
+#define _gather_190_type 1439
+#define _tmp_192_type 1440
+#define _loop0_193_type 1441
+#define _tmp_194_type 1442
+#define _loop0_195_type 1443
+#define _loop1_196_type 1444
+#define _loop1_197_type 1445
+#define _tmp_198_type 1446
+#define _tmp_199_type 1447
+#define _loop0_200_type 1448
+#define _tmp_201_type 1449
+#define _tmp_202_type 1450
+#define _tmp_203_type 1451
+#define _loop0_205_type 1452
+#define _gather_204_type 1453
+#define _loop0_207_type 1454
+#define _gather_206_type 1455
+#define _loop0_209_type 1456
+#define _gather_208_type 1457
+#define _loop0_211_type 1458
+#define _gather_210_type 1459
+#define _loop0_213_type 1460
+#define _gather_212_type 1461
+#define _tmp_214_type 1462
+#define _loop0_215_type 1463
+#define _loop1_216_type 1464
+#define _tmp_217_type 1465
+#define _loop0_218_type 1466
+#define _loop1_219_type 1467
+#define _tmp_220_type 1468
+#define _tmp_221_type 1469
+#define _tmp_222_type 1470
+#define _tmp_223_type 1471
+#define _tmp_224_type 1472
+#define _tmp_225_type 1473
+#define _tmp_226_type 1474
+#define _tmp_227_type 1475
+#define _tmp_228_type 1476
+#define _tmp_229_type 1477
+#define _loop0_231_type 1478
+#define _gather_230_type 1479
+#define _tmp_232_type 1480
+#define _tmp_233_type 1481
+#define _tmp_234_type 1482
+#define _tmp_235_type 1483
+#define _tmp_236_type 1484
+#define _tmp_237_type 1485
+#define _tmp_238_type 1486
+#define _loop0_239_type 1487
+#define _tmp_240_type 1488
+#define _tmp_241_type 1489
+#define _tmp_242_type 1490
+#define _tmp_243_type 1491
+#define _tmp_244_type 1492
+#define _tmp_245_type 1493
+#define _tmp_246_type 1494
+#define _tmp_247_type 1495
+#define _tmp_248_type 1496
+#define _tmp_249_type 1497
+#define _tmp_250_type 1498
+#define _tmp_251_type 1499
+#define _tmp_252_type 1500
+#define _tmp_253_type 1501
+#define _tmp_254_type 1502
+#define _tmp_255_type 1503
+#define _loop0_256_type 1504
+#define _tmp_257_type 1505
+#define _tmp_258_type 1506
+#define _tmp_259_type 1507
+#define _tmp_260_type 1508
+#define _tmp_261_type 1509
+#define _tmp_262_type 1510
+#define _tmp_263_type 1511
+#define _tmp_264_type 1512
+#define _tmp_265_type 1513
+#define _tmp_266_type 1514
+#define _tmp_267_type 1515
+#define _tmp_268_type 1516
+#define _tmp_269_type 1517
+#define _tmp_270_type 1518
+#define _tmp_271_type 1519
+#define _tmp_272_type 1520
+#define _loop0_274_type 1521
+#define _gather_273_type 1522
+#define _tmp_275_type 1523
+#define _tmp_276_type 1524
+#define _tmp_277_type 1525
+#define _tmp_278_type 1526
+#define _tmp_279_type 1527
+#define _tmp_280_type 1528
static mod_ty file_rule(Parser *p);
static mod_ty interactive_rule(Parser *p);
@@ -714,6 +716,8 @@ static asdl_type_param_seq* type_params_rule(Parser *p);
static asdl_type_param_seq* type_param_seq_rule(Parser *p);
static type_param_ty type_param_rule(Parser *p);
static expr_ty type_param_bound_rule(Parser *p);
+static expr_ty type_param_default_rule(Parser *p);
+static expr_ty type_param_starred_default_rule(Parser *p);
static expr_ty expressions_rule(Parser *p);
static expr_ty expression_rule(Parser *p);
static expr_ty yield_expr_rule(Parser *p);
@@ -10646,11 +10650,11 @@ type_param_seq_rule(Parser *p)
}
// type_param:
-// | NAME type_param_bound?
+// | NAME type_param_bound? type_param_default?
// | '*' NAME ':' expression
-// | '*' NAME
+// | '*' NAME type_param_starred_default?
// | '**' NAME ':' expression
-// | '**' NAME
+// | '**' NAME type_param_default?
static type_param_ty
type_param_rule(Parser *p)
{
@@ -10676,21 +10680,24 @@ type_param_rule(Parser *p)
UNUSED(_start_lineno); // Only used by EXTRA macro
int _start_col_offset = p->tokens[_mark]->col_offset;
UNUSED(_start_col_offset); // Only used by EXTRA macro
- { // NAME type_param_bound?
+ { // NAME type_param_bound? type_param_default?
if (p->error_indicator) {
p->level--;
return NULL;
}
- D(fprintf(stderr, "%*c> type_param[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "NAME type_param_bound?"));
+ D(fprintf(stderr, "%*c> type_param[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "NAME type_param_bound? type_param_default?"));
expr_ty a;
void *b;
+ void *c;
if (
(a = _PyPegen_name_token(p)) // NAME
&&
(b = type_param_bound_rule(p), !p->error_indicator) // type_param_bound?
+ &&
+ (c = type_param_default_rule(p), !p->error_indicator) // type_param_default?
)
{
- D(fprintf(stderr, "%*c+ type_param[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "NAME type_param_bound?"));
+ D(fprintf(stderr, "%*c+ type_param[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "NAME type_param_bound? type_param_default?"));
Token *_token = _PyPegen_get_last_nonnwhitespace_token(p);
if (_token == NULL) {
p->level--;
@@ -10700,7 +10707,7 @@ type_param_rule(Parser *p)
UNUSED(_end_lineno); // Only used by EXTRA macro
int _end_col_offset = _token->end_col_offset;
UNUSED(_end_col_offset); // Only used by EXTRA macro
- _res = _PyAST_TypeVar ( a -> v . Name . id , b , EXTRA );
+ _res = _PyAST_TypeVar ( a -> v . Name . id , b , c , EXTRA );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
p->level--;
@@ -10710,7 +10717,7 @@ type_param_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s type_param[%d-%d]: %s failed!\n", p->level, ' ',
- p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "NAME type_param_bound?"));
+ p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "NAME type_param_bound? type_param_default?"));
}
{ // '*' NAME ':' expression
if (p->error_indicator) {
@@ -10745,21 +10752,24 @@ type_param_rule(Parser *p)
D(fprintf(stderr, "%*c%s type_param[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'*' NAME ':' expression"));
}
- { // '*' NAME
+ { // '*' NAME type_param_starred_default?
if (p->error_indicator) {
p->level--;
return NULL;
}
- D(fprintf(stderr, "%*c> type_param[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'*' NAME"));
+ D(fprintf(stderr, "%*c> type_param[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'*' NAME type_param_starred_default?"));
Token * _literal;
expr_ty a;
+ void *b;
if (
(_literal = _PyPegen_expect_token(p, 16)) // token='*'
&&
(a = _PyPegen_name_token(p)) // NAME
+ &&
+ (b = type_param_starred_default_rule(p), !p->error_indicator) // type_param_starred_default?
)
{
- D(fprintf(stderr, "%*c+ type_param[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'*' NAME"));
+ D(fprintf(stderr, "%*c+ type_param[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'*' NAME type_param_starred_default?"));
Token *_token = _PyPegen_get_last_nonnwhitespace_token(p);
if (_token == NULL) {
p->level--;
@@ -10769,7 +10779,7 @@ type_param_rule(Parser *p)
UNUSED(_end_lineno); // Only used by EXTRA macro
int _end_col_offset = _token->end_col_offset;
UNUSED(_end_col_offset); // Only used by EXTRA macro
- _res = _PyAST_TypeVarTuple ( a -> v . Name . id , EXTRA );
+ _res = _PyAST_TypeVarTuple ( a -> v . Name . id , b , EXTRA );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
p->level--;
@@ -10779,7 +10789,7 @@ type_param_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s type_param[%d-%d]: %s failed!\n", p->level, ' ',
- p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'*' NAME"));
+ p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'*' NAME type_param_starred_default?"));
}
{ // '**' NAME ':' expression
if (p->error_indicator) {
@@ -10814,21 +10824,24 @@ type_param_rule(Parser *p)
D(fprintf(stderr, "%*c%s type_param[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'**' NAME ':' expression"));
}
- { // '**' NAME
+ { // '**' NAME type_param_default?
if (p->error_indicator) {
p->level--;
return NULL;
}
- D(fprintf(stderr, "%*c> type_param[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'**' NAME"));
+ D(fprintf(stderr, "%*c> type_param[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'**' NAME type_param_default?"));
Token * _literal;
expr_ty a;
+ void *b;
if (
(_literal = _PyPegen_expect_token(p, 35)) // token='**'
&&
(a = _PyPegen_name_token(p)) // NAME
+ &&
+ (b = type_param_default_rule(p), !p->error_indicator) // type_param_default?
)
{
- D(fprintf(stderr, "%*c+ type_param[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'**' NAME"));
+ D(fprintf(stderr, "%*c+ type_param[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'**' NAME type_param_default?"));
Token *_token = _PyPegen_get_last_nonnwhitespace_token(p);
if (_token == NULL) {
p->level--;
@@ -10838,7 +10851,7 @@ type_param_rule(Parser *p)
UNUSED(_end_lineno); // Only used by EXTRA macro
int _end_col_offset = _token->end_col_offset;
UNUSED(_end_col_offset); // Only used by EXTRA macro
- _res = _PyAST_ParamSpec ( a -> v . Name . id , EXTRA );
+ _res = _PyAST_ParamSpec ( a -> v . Name . id , b , EXTRA );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
p->level--;
@@ -10848,7 +10861,7 @@ type_param_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s type_param[%d-%d]: %s failed!\n", p->level, ' ',
- p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'**' NAME"));
+ p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'**' NAME type_param_default?"));
}
_res = NULL;
done:
@@ -10903,6 +10916,98 @@ type_param_bound_rule(Parser *p)
return _res;
}
+// type_param_default: '=' expression
+static expr_ty
+type_param_default_rule(Parser *p)
+{
+ if (p->level++ == MAXSTACK) {
+ _Pypegen_stack_overflow(p);
+ }
+ if (p->error_indicator) {
+ p->level--;
+ return NULL;
+ }
+ expr_ty _res = NULL;
+ int _mark = p->mark;
+ { // '=' expression
+ if (p->error_indicator) {
+ p->level--;
+ return NULL;
+ }
+ D(fprintf(stderr, "%*c> type_param_default[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'=' expression"));
+ Token * _literal;
+ expr_ty e;
+ if (
+ (_literal = _PyPegen_expect_token(p, 22)) // token='='
+ &&
+ (e = expression_rule(p)) // expression
+ )
+ {
+ D(fprintf(stderr, "%*c+ type_param_default[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'=' expression"));
+ _res = CHECK_VERSION ( expr_ty , 13 , "Type parameter defaults are" , e );
+ if (_res == NULL && PyErr_Occurred()) {
+ p->error_indicator = 1;
+ p->level--;
+ return NULL;
+ }
+ goto done;
+ }
+ p->mark = _mark;
+ D(fprintf(stderr, "%*c%s type_param_default[%d-%d]: %s failed!\n", p->level, ' ',
+ p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'=' expression"));
+ }
+ _res = NULL;
+ done:
+ p->level--;
+ return _res;
+}
+
+// type_param_starred_default: '=' star_expression
+static expr_ty
+type_param_starred_default_rule(Parser *p)
+{
+ if (p->level++ == MAXSTACK) {
+ _Pypegen_stack_overflow(p);
+ }
+ if (p->error_indicator) {
+ p->level--;
+ return NULL;
+ }
+ expr_ty _res = NULL;
+ int _mark = p->mark;
+ { // '=' star_expression
+ if (p->error_indicator) {
+ p->level--;
+ return NULL;
+ }
+ D(fprintf(stderr, "%*c> type_param_starred_default[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'=' star_expression"));
+ Token * _literal;
+ expr_ty e;
+ if (
+ (_literal = _PyPegen_expect_token(p, 22)) // token='='
+ &&
+ (e = star_expression_rule(p)) // star_expression
+ )
+ {
+ D(fprintf(stderr, "%*c+ type_param_starred_default[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'=' star_expression"));
+ _res = CHECK_VERSION ( expr_ty , 13 , "Type parameter defaults are" , e );
+ if (_res == NULL && PyErr_Occurred()) {
+ p->error_indicator = 1;
+ p->level--;
+ return NULL;
+ }
+ goto done;
+ }
+ p->mark = _mark;
+ D(fprintf(stderr, "%*c%s type_param_starred_default[%d-%d]: %s failed!\n", p->level, ' ',
+ p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'=' star_expression"));
+ }
+ _res = NULL;
+ done:
+ p->level--;
+ return _res;
+}
+
// expressions: expression ((',' expression))+ ','? | expression ',' | expression
static expr_ty
expressions_rule(Parser *p)
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 60b46263a0..cc7734e0db 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -203,6 +203,7 @@ void _PyAST_Fini(PyInterpreterState *interp)
Py_CLEAR(state->conversion);
Py_CLEAR(state->ctx);
Py_CLEAR(state->decorator_list);
+ Py_CLEAR(state->default_value);
Py_CLEAR(state->defaults);
Py_CLEAR(state->elt);
Py_CLEAR(state->elts);
@@ -311,6 +312,7 @@ static int init_identifiers(struct ast_state *state)
if ((state->conversion = PyUnicode_InternFromString("conversion")) == NULL) return -1;
if ((state->ctx = PyUnicode_InternFromString("ctx")) == NULL) return -1;
if ((state->decorator_list = PyUnicode_InternFromString("decorator_list")) == NULL) return -1;
+ if ((state->default_value = PyUnicode_InternFromString("default_value")) == NULL) return -1;
if ((state->defaults = PyUnicode_InternFromString("defaults")) == NULL) return -1;
if ((state->elt = PyUnicode_InternFromString("elt")) == NULL) return -1;
if ((state->elts = PyUnicode_InternFromString("elts")) == NULL) return -1;
@@ -809,12 +811,15 @@ static PyObject* ast2obj_type_param(struct ast_state *state, struct validator
static const char * const TypeVar_fields[]={
"name",
"bound",
+ "default_value",
};
static const char * const ParamSpec_fields[]={
"name",
+ "default_value",
};
static const char * const TypeVarTuple_fields[]={
"name",
+ "default_value",
};
@@ -4913,6 +4918,22 @@ add_ast_annotations(struct ast_state *state)
return 0;
}
}
+ {
+ PyObject *type = state->expr_type;
+ type = _Py_union_type_or(type, Py_None);
+ cond = type != NULL;
+ if (!cond) {
+ Py_DECREF(TypeVar_annotations);
+ return 0;
+ }
+ cond = PyDict_SetItemString(TypeVar_annotations, "default_value", type)
+ == 0;
+ Py_DECREF(type);
+ if (!cond) {
+ Py_DECREF(TypeVar_annotations);
+ return 0;
+ }
+ }
cond = PyObject_SetAttrString(state->TypeVar_type, "_field_types",
TypeVar_annotations) == 0;
if (!cond) {
@@ -4938,6 +4959,22 @@ add_ast_annotations(struct ast_state *state)
return 0;
}
}
+ {
+ PyObject *type = state->expr_type;
+ type = _Py_union_type_or(type, Py_None);
+ cond = type != NULL;
+ if (!cond) {
+ Py_DECREF(ParamSpec_annotations);
+ return 0;
+ }
+ cond = PyDict_SetItemString(ParamSpec_annotations, "default_value",
+ type) == 0;
+ Py_DECREF(type);
+ if (!cond) {
+ Py_DECREF(ParamSpec_annotations);
+ return 0;
+ }
+ }
cond = PyObject_SetAttrString(state->ParamSpec_type, "_field_types",
ParamSpec_annotations) == 0;
if (!cond) {
@@ -4964,6 +5001,22 @@ add_ast_annotations(struct ast_state *state)
return 0;
}
}
+ {
+ PyObject *type = state->expr_type;
+ type = _Py_union_type_or(type, Py_None);
+ cond = type != NULL;
+ if (!cond) {
+ Py_DECREF(TypeVarTuple_annotations);
+ return 0;
+ }
+ cond = PyDict_SetItemString(TypeVarTuple_annotations, "default_value",
+ type) == 0;
+ Py_DECREF(type);
+ if (!cond) {
+ Py_DECREF(TypeVarTuple_annotations);
+ return 0;
+ }
+ }
cond = PyObject_SetAttrString(state->TypeVarTuple_type, "_field_types",
TypeVarTuple_annotations) == 0;
if (!cond) {
@@ -6243,28 +6296,37 @@ init_types(struct ast_state *state)
if (!state->TypeIgnore_type) return -1;
state->type_param_type = make_type(state, "type_param", state->AST_type,
NULL, 0,
- "type_param = TypeVar(identifier name, expr? bound)\n"
- " | ParamSpec(identifier name)\n"
- " | TypeVarTuple(identifier name)");
+ "type_param = TypeVar(identifier name, expr? bound, expr? default_value)\n"
+ " | ParamSpec(identifier name, expr? default_value)\n"
+ " | TypeVarTuple(identifier name, expr? default_value)");
if (!state->type_param_type) return -1;
if (add_attributes(state, state->type_param_type, type_param_attributes, 4)
< 0) return -1;
state->TypeVar_type = make_type(state, "TypeVar", state->type_param_type,
- TypeVar_fields, 2,
- "TypeVar(identifier name, expr? bound)");
+ TypeVar_fields, 3,
+ "TypeVar(identifier name, expr? bound, expr? default_value)");
if (!state->TypeVar_type) return -1;
if (PyObject_SetAttr(state->TypeVar_type, state->bound, Py_None) == -1)
return -1;
+ if (PyObject_SetAttr(state->TypeVar_type, state->default_value, Py_None) ==
+ -1)
+ return -1;
state->ParamSpec_type = make_type(state, "ParamSpec",
state->type_param_type, ParamSpec_fields,
- 1,
- "ParamSpec(identifier name)");
+ 2,
+ "ParamSpec(identifier name, expr? default_value)");
if (!state->ParamSpec_type) return -1;
+ if (PyObject_SetAttr(state->ParamSpec_type, state->default_value, Py_None)
+ == -1)
+ return -1;
state->TypeVarTuple_type = make_type(state, "TypeVarTuple",
state->type_param_type,
- TypeVarTuple_fields, 1,
- "TypeVarTuple(identifier name)");
+ TypeVarTuple_fields, 2,
+ "TypeVarTuple(identifier name, expr? default_value)");
if (!state->TypeVarTuple_type) return -1;
+ if (PyObject_SetAttr(state->TypeVarTuple_type, state->default_value,
+ Py_None) == -1)
+ return -1;
if (!add_ast_annotations(state)) {
return -1;
@@ -8055,8 +8117,9 @@ _PyAST_TypeIgnore(int lineno, string tag, PyArena *arena)
}
type_param_ty
-_PyAST_TypeVar(identifier name, expr_ty bound, int lineno, int col_offset, int
- end_lineno, int end_col_offset, PyArena *arena)
+_PyAST_TypeVar(identifier name, expr_ty bound, expr_ty default_value, int
+ lineno, int col_offset, int end_lineno, int end_col_offset,
+ PyArena *arena)
{
type_param_ty p;
if (!name) {
@@ -8070,6 +8133,7 @@ _PyAST_TypeVar(identifier name, expr_ty bound, int lineno, int col_offset, int
p->kind = TypeVar_kind;
p->v.TypeVar.name = name;
p->v.TypeVar.bound = bound;
+ p->v.TypeVar.default_value = default_value;
p->lineno = lineno;
p->col_offset = col_offset;
p->end_lineno = end_lineno;
@@ -8078,8 +8142,8 @@ _PyAST_TypeVar(identifier name, expr_ty bound, int lineno, int col_offset, int
}
type_param_ty
-_PyAST_ParamSpec(identifier name, int lineno, int col_offset, int end_lineno,
- int end_col_offset, PyArena *arena)
+_PyAST_ParamSpec(identifier name, expr_ty default_value, int lineno, int
+ col_offset, int end_lineno, int end_col_offset, PyArena *arena)
{
type_param_ty p;
if (!name) {
@@ -8092,6 +8156,7 @@ _PyAST_ParamSpec(identifier name, int lineno, int col_offset, int end_lineno,
return NULL;
p->kind = ParamSpec_kind;
p->v.ParamSpec.name = name;
+ p->v.ParamSpec.default_value = default_value;
p->lineno = lineno;
p->col_offset = col_offset;
p->end_lineno = end_lineno;
@@ -8100,8 +8165,9 @@ _PyAST_ParamSpec(identifier name, int lineno, int col_offset, int end_lineno,
}
type_param_ty
-_PyAST_TypeVarTuple(identifier name, int lineno, int col_offset, int
- end_lineno, int end_col_offset, PyArena *arena)
+_PyAST_TypeVarTuple(identifier name, expr_ty default_value, int lineno, int
+ col_offset, int end_lineno, int end_col_offset, PyArena
+ *arena)
{
type_param_ty p;
if (!name) {
@@ -8114,6 +8180,7 @@ _PyAST_TypeVarTuple(identifier name, int lineno, int col_offset, int
return NULL;
p->kind = TypeVarTuple_kind;
p->v.TypeVarTuple.name = name;
+ p->v.TypeVarTuple.default_value = default_value;
p->lineno = lineno;
p->col_offset = col_offset;
p->end_lineno = end_lineno;
@@ -10079,6 +10146,11 @@ ast2obj_type_param(struct ast_state *state, struct validator *vstate, void* _o)
if (PyObject_SetAttr(result, state->bound, value) == -1)
goto failed;
Py_DECREF(value);
+ value = ast2obj_expr(state, vstate, o->v.TypeVar.default_value);
+ if (!value) goto failed;
+ if (PyObject_SetAttr(result, state->default_value, value) == -1)
+ goto failed;
+ Py_DECREF(value);
break;
case ParamSpec_kind:
tp = (PyTypeObject *)state->ParamSpec_type;
@@ -10089,6 +10161,11 @@ ast2obj_type_param(struct ast_state *state, struct validator *vstate, void* _o)
if (PyObject_SetAttr(result, state->name, value) == -1)
goto failed;
Py_DECREF(value);
+ value = ast2obj_expr(state, vstate, o->v.ParamSpec.default_value);
+ if (!value) goto failed;
+ if (PyObject_SetAttr(result, state->default_value, value) == -1)
+ goto failed;
+ Py_DECREF(value);
break;
case TypeVarTuple_kind:
tp = (PyTypeObject *)state->TypeVarTuple_type;
@@ -10099,6 +10176,11 @@ ast2obj_type_param(struct ast_state *state, struct validator *vstate, void* _o)
if (PyObject_SetAttr(result, state->name, value) == -1)
goto failed;
Py_DECREF(value);
+ value = ast2obj_expr(state, vstate, o->v.TypeVarTuple.default_value);
+ if (!value) goto failed;
+ if (PyObject_SetAttr(result, state->default_value, value) == -1)
+ goto failed;
+ Py_DECREF(value);
break;
}
value = ast2obj_int(state, vstate, o->lineno);
@@ -16935,6 +17017,7 @@ obj2ast_type_param(struct ast_state *state, PyObject* obj, type_param_ty* out,
if (isinstance) {
identifier name;
expr_ty bound;
+ expr_ty default_value;
if (PyObject_GetOptionalAttr(obj, state->name, &tmp) < 0) {
return -1;
@@ -16970,8 +17053,25 @@ obj2ast_type_param(struct ast_state *state, PyObject* obj, type_param_ty* out,
if (res != 0) goto failed;
Py_CLEAR(tmp);
}
- *out = _PyAST_TypeVar(name, bound, lineno, col_offset, end_lineno,
- end_col_offset, arena);
+ if (PyObject_GetOptionalAttr(obj, state->default_value, &tmp) < 0) {
+ return -1;
+ }
+ if (tmp == NULL || tmp == Py_None) {
+ Py_CLEAR(tmp);
+ default_value = NULL;
+ }
+ else {
+ int res;
+ if (_Py_EnterRecursiveCall(" while traversing 'TypeVar' node")) {
+ goto failed;
+ }
+ res = obj2ast_expr(state, tmp, &default_value, arena);
+ _Py_LeaveRecursiveCall();
+ if (res != 0) goto failed;
+ Py_CLEAR(tmp);
+ }
+ *out = _PyAST_TypeVar(name, bound, default_value, lineno, col_offset,
+ end_lineno, end_col_offset, arena);
if (*out == NULL) goto failed;
return 0;
}
@@ -16982,6 +17082,7 @@ obj2ast_type_param(struct ast_state *state, PyObject* obj, type_param_ty* out,
}
if (isinstance) {
identifier name;
+ expr_ty default_value;
if (PyObject_GetOptionalAttr(obj, state->name, &tmp) < 0) {
return -1;
@@ -17000,8 +17101,25 @@ obj2ast_type_param(struct ast_state *state, PyObject* obj, type_param_ty* out,
if (res != 0) goto failed;
Py_CLEAR(tmp);
}
- *out = _PyAST_ParamSpec(name, lineno, col_offset, end_lineno,
- end_col_offset, arena);
+ if (PyObject_GetOptionalAttr(obj, state->default_value, &tmp) < 0) {
+ return -1;
+ }
+ if (tmp == NULL || tmp == Py_None) {
+ Py_CLEAR(tmp);
+ default_value = NULL;
+ }
+ else {
+ int res;
+ if (_Py_EnterRecursiveCall(" while traversing 'ParamSpec' node")) {
+ goto failed;
+ }
+ res = obj2ast_expr(state, tmp, &default_value, arena);
+ _Py_LeaveRecursiveCall();
+ if (res != 0) goto failed;
+ Py_CLEAR(tmp);
+ }
+ *out = _PyAST_ParamSpec(name, default_value, lineno, col_offset,
+ end_lineno, end_col_offset, arena);
if (*out == NULL) goto failed;
return 0;
}
@@ -17012,6 +17130,7 @@ obj2ast_type_param(struct ast_state *state, PyObject* obj, type_param_ty* out,
}
if (isinstance) {
identifier name;
+ expr_ty default_value;
if (PyObject_GetOptionalAttr(obj, state->name, &tmp) < 0) {
return -1;
@@ -17030,8 +17149,25 @@ obj2ast_type_param(struct ast_state *state, PyObject* obj, type_param_ty* out,
if (res != 0) goto failed;
Py_CLEAR(tmp);
}
- *out = _PyAST_TypeVarTuple(name, lineno, col_offset, end_lineno,
- end_col_offset, arena);
+ if (PyObject_GetOptionalAttr(obj, state->default_value, &tmp) < 0) {
+ return -1;
+ }
+ if (tmp == NULL || tmp == Py_None) {
+ Py_CLEAR(tmp);
+ default_value = NULL;
+ }
+ else {
+ int res;
+ if (_Py_EnterRecursiveCall(" while traversing 'TypeVarTuple' node")) {
+ goto failed;
+ }
+ res = obj2ast_expr(state, tmp, &default_value, arena);
+ _Py_LeaveRecursiveCall();
+ if (res != 0) goto failed;
+ Py_CLEAR(tmp);
+ }
+ *out = _PyAST_TypeVarTuple(name, default_value, lineno, col_offset,
+ end_lineno, end_col_offset, arena);
if (*out == NULL) goto failed;
return 0;
}
diff --git a/Python/ast.c b/Python/ast.c
index 71b09d889f..1d1a48ec88 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -1011,13 +1011,19 @@ validate_typeparam(struct validator *state, type_param_ty tp)
case TypeVar_kind:
ret = validate_name(tp->v.TypeVar.name) &&
(!tp->v.TypeVar.bound ||
- validate_expr(state, tp->v.TypeVar.bound, Load));
+ validate_expr(state, tp->v.TypeVar.bound, Load)) &&
+ (!tp->v.TypeVar.default_value ||
+ validate_expr(state, tp->v.TypeVar.default_value, Load));
break;
case ParamSpec_kind:
- ret = validate_name(tp->v.ParamSpec.name);
+ ret = validate_name(tp->v.ParamSpec.name) &&
+ (!tp->v.ParamSpec.default_value ||
+ validate_expr(state, tp->v.ParamSpec.default_value, Load));
break;
case TypeVarTuple_kind:
- ret = validate_name(tp->v.TypeVarTuple.name);
+ ret = validate_name(tp->v.TypeVarTuple.name) &&
+ (!tp->v.TypeVarTuple.default_value ||
+ validate_expr(state, tp->v.TypeVarTuple.default_value, Load));
break;
}
return ret;
diff --git a/Python/compile.c b/Python/compile.c
index feedd98883..ec47af151f 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -2116,6 +2116,36 @@ wrap_in_stopiteration_handler(struct compiler *c)
return SUCCESS;
}
+static int
+compiler_type_param_bound_or_default(struct compiler *c, expr_ty e,
+ identifier name, void *key,
+ bool allow_starred)
+{
+ if (compiler_enter_scope(c, name, COMPILER_SCOPE_TYPEPARAMS,
+ key, e->lineno) == -1) {
+ return ERROR;
+ }
+ if (allow_starred && e->kind == Starred_kind) {
+ VISIT(c, expr, e->v.Starred.value);
+ ADDOP_I(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
+ }
+ else {
+ VISIT(c, expr, e);
+ }
+ ADDOP_IN_SCOPE(c, LOC(e), RETURN_VALUE);
+ PyCodeObject *co = optimize_and_assemble(c, 1);
+ compiler_exit_scope(c);
+ if (co == NULL) {
+ return ERROR;
+ }
+ if (compiler_make_closure(c, LOC(e), co, 0) < 0) {
+ Py_DECREF(co);
+ return ERROR;
+ }
+ Py_DECREF(co);
+ return SUCCESS;
+}
+
static int
compiler_type_params(struct compiler *c, asdl_type_param_seq *type_params)
{
@@ -2123,6 +2153,7 @@ compiler_type_params(struct compiler *c, asdl_type_param_seq *type_params)
return SUCCESS;
}
Py_ssize_t n = asdl_seq_LEN(type_params);
+ bool seen_default = false;
for (Py_ssize_t i = 0; i < n; i++) {
type_param_ty typeparam = asdl_seq_GET(type_params, i);
@@ -2132,22 +2163,10 @@ compiler_type_params(struct compiler *c, asdl_type_param_seq *type_params)
ADDOP_LOAD_CONST(c, loc, typeparam->v.TypeVar.name);
if (typeparam->v.TypeVar.bound) {
expr_ty bound = typeparam->v.TypeVar.bound;
- if (compiler_enter_scope(c, typeparam->v.TypeVar.name, COMPILER_SCOPE_TYPEPARAMS,
- (void *)typeparam, bound->lineno) == -1) {
+ if (compiler_type_param_bound_or_default(c, bound, typeparam->v.TypeVar.name,
+ (void *)typeparam, false) < 0) {
return ERROR;
}
- VISIT_IN_SCOPE(c, expr, bound);
- ADDOP_IN_SCOPE(c, loc, RETURN_VALUE);
- PyCodeObject *co = optimize_and_assemble(c, 1);
- compiler_exit_scope(c);
- if (co == NULL) {
- return ERROR;
- }
- if (compiler_make_closure(c, loc, co, 0) < 0) {
- Py_DECREF(co);
- return ERROR;
- }
- Py_DECREF(co);
int intrinsic = bound->kind == Tuple_kind
? INTRINSIC_TYPEVAR_WITH_CONSTRAINTS
@@ -2157,18 +2176,60 @@ compiler_type_params(struct compiler *c, asdl_type_param_seq *type_params)
else {
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_TYPEVAR);
}
+ if (typeparam->v.TypeVar.default_value) {
+ seen_default = true;
+ expr_ty default_ = typeparam->v.TypeVar.default_value;
+ if (compiler_type_param_bound_or_default(c, default_, typeparam->v.TypeVar.name,
+ (void *)((uintptr_t)typeparam + 1), false) < 0) {
+ return ERROR;
+ }
+ ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
+ }
+ else if (seen_default) {
+ return compiler_error(c, loc, "non-default type parameter '%U' "
+ "follows default type parameter",
+ typeparam->v.TypeVar.name);
+ }
ADDOP_I(c, loc, COPY, 1);
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVar.name, Store));
break;
case TypeVarTuple_kind:
ADDOP_LOAD_CONST(c, loc, typeparam->v.TypeVarTuple.name);
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_TYPEVARTUPLE);
+ if (typeparam->v.TypeVarTuple.default_value) {
+ expr_ty default_ = typeparam->v.TypeVarTuple.default_value;
+ if (compiler_type_param_bound_or_default(c, default_, typeparam->v.TypeVarTuple.name,
+ (void *)typeparam, true) < 0) {
+ return ERROR;
+ }
+ ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
+ seen_default = true;
+ }
+ else if (seen_default) {
+ return compiler_error(c, loc, "non-default type parameter '%U' "
+ "follows default type parameter",
+ typeparam->v.TypeVarTuple.name);
+ }
ADDOP_I(c, loc, COPY, 1);
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVarTuple.name, Store));
break;
case ParamSpec_kind:
ADDOP_LOAD_CONST(c, loc, typeparam->v.ParamSpec.name);
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_PARAMSPEC);
+ if (typeparam->v.ParamSpec.default_value) {
+ expr_ty default_ = typeparam->v.ParamSpec.default_value;
+ if (compiler_type_param_bound_or_default(c, default_, typeparam->v.ParamSpec.name,
+ (void *)typeparam, false) < 0) {
+ return ERROR;
+ }
+ ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
+ seen_default = true;
+ }
+ else if (seen_default) {
+ return compiler_error(c, loc, "non-default type parameter '%U' "
+ "follows default type parameter",
+ typeparam->v.ParamSpec.name);
+ }
ADDOP_I(c, loc, COPY, 1);
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.ParamSpec.name, Store));
break;
diff --git a/Python/intrinsics.c b/Python/intrinsics.c
index d3146973b7..5b10c3ce7d 100644
--- a/Python/intrinsics.c
+++ b/Python/intrinsics.c
@@ -265,6 +265,7 @@ _PyIntrinsics_BinaryFunctions[] = {
INTRINSIC_FUNC_ENTRY(INTRINSIC_TYPEVAR_WITH_BOUND, make_typevar_with_bound)
INTRINSIC_FUNC_ENTRY(INTRINSIC_TYPEVAR_WITH_CONSTRAINTS, make_typevar_with_constraints)
INTRINSIC_FUNC_ENTRY(INTRINSIC_SET_FUNCTION_TYPE_PARAMS, _Py_set_function_type_params)
+ INTRINSIC_FUNC_ENTRY(INTRINSIC_SET_TYPEPARAM_DEFAULT, _Py_set_typeparam_default)
};
#undef INTRINSIC_FUNC_ENTRY
diff --git a/Python/symtable.c b/Python/symtable.c
index eecd159b2c..2ec21a2d37 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -2275,6 +2275,24 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
VISIT_QUIT(st, 1);
}
+static int
+symtable_visit_type_param_bound_or_default(struct symtable *st, expr_ty e, identifier name, void *key)
+{
+ if (e) {
+ int is_in_class = st->st_cur->ste_can_see_class_scope;
+ if (!symtable_enter_block(st, name, TypeVarBoundBlock, key, LOCATION(e)))
+ return 0;
+ st->st_cur->ste_can_see_class_scope = is_in_class;
+ if (is_in_class && !symtable_add_def(st, &_Py_ID(__classdict__), USE, LOCATION(e))) {
+ VISIT_QUIT(st, 0);
+ }
+ VISIT(st, expr, e);
+ if (!symtable_exit_block(st))
+ return 0;
+ }
+ return 1;
+}
+
static int
symtable_visit_type_param(struct symtable *st, type_param_ty tp)
{
@@ -2287,28 +2305,39 @@ symtable_visit_type_param(struct symtable *st, type_param_ty tp)
case TypeVar_kind:
if (!symtable_add_def(st, tp->v.TypeVar.name, DEF_TYPE_PARAM | DEF_LOCAL, LOCATION(tp)))
VISIT_QUIT(st, 0);
- if (tp->v.TypeVar.bound) {
- int is_in_class = st->st_cur->ste_can_see_class_scope;
- if (!symtable_enter_block(st, tp->v.TypeVar.name,
- TypeVarBoundBlock, (void *)tp,
- LOCATION(tp)))
- VISIT_QUIT(st, 0);
- st->st_cur->ste_can_see_class_scope = is_in_class;
- if (is_in_class && !symtable_add_def(st, &_Py_ID(__classdict__), USE, LOCATION(tp->v.TypeVar.bound))) {
- VISIT_QUIT(st, 0);
- }
- VISIT(st, expr, tp->v.TypeVar.bound);
- if (!symtable_exit_block(st))
- VISIT_QUIT(st, 0);
+
+ // We must use a different key for the bound and default. The obvious choice would be to
+ // use the .bound and .default_value pointers, but that fails when the expression immediately
+ // inside the bound or default is a comprehension: we would reuse the same key for
+ // the comprehension scope. Therefore, use the address + 1 as the second key.
+ // The only requirement for the key is that it is unique and it matches the logic in
+ // compile.c where the scope is retrieved.
+ if (!symtable_visit_type_param_bound_or_default(st, tp->v.TypeVar.bound, tp->v.TypeVar.name,
+ (void *)tp)) {
+ VISIT_QUIT(st, 0);
+ }
+ if (!symtable_visit_type_param_bound_or_default(st, tp->v.TypeVar.default_value, tp->v.TypeVar.name,
+ (void *)((uintptr_t)tp + 1))) {
+ VISIT_QUIT(st, 0);
}
break;
case TypeVarTuple_kind:
- if (!symtable_add_def(st, tp->v.TypeVarTuple.name, DEF_TYPE_PARAM | DEF_LOCAL, LOCATION(tp)))
+ if (!symtable_add_def(st, tp->v.TypeVarTuple.name, DEF_TYPE_PARAM | DEF_LOCAL, LOCATION(tp))) {
VISIT_QUIT(st, 0);
+ }
+ if (!symtable_visit_type_param_bound_or_default(st, tp->v.TypeVarTuple.default_value, tp->v.TypeVarTuple.name,
+ (void *)tp)) {
+ VISIT_QUIT(st, 0);
+ }
break;
case ParamSpec_kind:
- if (!symtable_add_def(st, tp->v.ParamSpec.name, DEF_TYPE_PARAM | DEF_LOCAL, LOCATION(tp)))
+ if (!symtable_add_def(st, tp->v.ParamSpec.name, DEF_TYPE_PARAM | DEF_LOCAL, LOCATION(tp))) {
VISIT_QUIT(st, 0);
+ }
+ if (!symtable_visit_type_param_bound_or_default(st, tp->v.ParamSpec.default_value, tp->v.ParamSpec.name,
+ (void *)tp)) {
+ VISIT_QUIT(st, 0);
+ }
break;
}
VISIT_QUIT(st, 1);
|