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
|
[case testBasicParamSpec]
from typing_extensions import ParamSpec
P = ParamSpec('P')
[builtins fixtures/tuple.pyi]
[case testInvalidParamSpecDefinitions]
from typing import ParamSpec
P1 = ParamSpec("P1", covariant=True) # E: The variance and bound arguments to ParamSpec do not have defined semantics yet
P2 = ParamSpec("P2", contravariant=True) # E: The variance and bound arguments to ParamSpec do not have defined semantics yet
P3 = ParamSpec("P3", bound=int) # E: The variance and bound arguments to ParamSpec do not have defined semantics yet
P4 = ParamSpec("P4", int, str) # E: Too many positional arguments for "ParamSpec"
P5 = ParamSpec("P5", covariant=True, bound=int) # E: The variance and bound arguments to ParamSpec do not have defined semantics yet
[builtins fixtures/paramspec.pyi]
[case testParamSpecLocations]
from typing import Any, Callable, List, Type
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec('P')
x: P # E: ParamSpec "P" is unbound
def foo1(x: Callable[P, int]) -> Callable[P, str]: ...
def foo2(x: P) -> P: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
def foo3(x: Concatenate[int, P]) -> int: ... # E: Invalid location for Concatenate \
# N: You can use Concatenate as the first argument to Callable
def foo4(x: List[P]) -> None: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
def foo5(x: Callable[[int, str], P]) -> None: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
def foo6(x: Callable[[P], int]) -> None: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
def foo7(
*args: P.args, **kwargs: P.kwargs # E: ParamSpec "P" is unbound
) -> Callable[[Callable[P, T]], Type[T]]:
...
def wrapper(f: Callable[P, int]) -> None:
def inner(*args: P.args, **kwargs: P.kwargs) -> None: ... # OK
def extra_args_left(x: int, *args: P.args, **kwargs: P.kwargs) -> None: ... # OK
def extra_args_between(*args: P.args, x: int, **kwargs: P.kwargs) -> None: ... # E: Arguments not allowed after ParamSpec.args
def swapped(*args: P.kwargs, **kwargs: P.args) -> None: ... # E: Use "P.args" for variadic "*" parameter \
# E: Use "P.kwargs" for variadic "**" parameter
def bad_kwargs(*args: P.args, **kwargs: P.args) -> None: ... # E: Use "P.kwargs" for variadic "**" parameter
def bad_args(*args: P.kwargs, **kwargs: P.kwargs) -> None: ... # E: Use "P.args" for variadic "*" parameter
def misplaced(x: P.args) -> None: ... # E: ParamSpec components are not allowed here
def bad_kwargs_any(*args: P.args, **kwargs: Any) -> None: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
[builtins fixtures/paramspec.pyi]
[case testParamSpecImports]
import lib
from lib import Base
class C(Base[[int]]):
def test(self, x: int): ...
class D(lib.Base[[int]]):
def test(self, x: int): ...
class E(lib.Base[...]): ...
reveal_type(E().test) # N: Revealed type is "def (*Any, **Any)"
[file lib.py]
from typing import Generic
from typing_extensions import ParamSpec
P = ParamSpec("P")
class Base(Generic[P]):
def test(self, *args: P.args, **kwargs: P.kwargs) -> None:
...
[builtins fixtures/paramspec.pyi]
[case testParamSpecEllipsisInAliases]
from typing import Any, Callable, Generic, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
R = TypeVar('R')
Alias = Callable[P, R]
class B(Generic[P]): ...
Other = B[P]
T = TypeVar('T', bound=Alias[..., Any])
Alias[..., Any] # E: Type application is only supported for generic classes
B[...]
Other[...]
[builtins fixtures/paramspec.pyi]
[case testParamSpecEllipsisInConcatenate]
from typing import Any, Callable, Generic, TypeVar
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec('P')
R = TypeVar('R')
Alias = Callable[P, R]
IntFun = Callable[Concatenate[int, ...], None]
f: IntFun
reveal_type(f) # N: Revealed type is "def (builtins.int, *Any, **Any)"
g: Callable[Concatenate[int, ...], None]
reveal_type(g) # N: Revealed type is "def (builtins.int, *Any, **Any)"
class B(Generic[P]):
def test(self, *args: P.args, **kwargs: P.kwargs) -> None:
...
x: B[Concatenate[int, ...]]
reveal_type(x.test) # N: Revealed type is "def (builtins.int, *Any, **Any)"
Bad = Callable[Concatenate[int, [int, str]], None] # E: The last parameter to Concatenate needs to be a ParamSpec \
# E: Bracketed expression "[...]" is not valid as a type
def bad(fn: Callable[Concatenate[P, int], None]): # E: The last parameter to Concatenate needs to be a ParamSpec
...
[builtins fixtures/paramspec.pyi]
[case testParamSpecContextManagerLike]
from typing import Callable, List, Iterator, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
def tmpcontextmanagerlike(x: Callable[P, Iterator[T]]) -> Callable[P, List[T]]: ...
@tmpcontextmanagerlike
def whatever(x: int) -> Iterator[int]:
yield x
reveal_type(whatever) # N: Revealed type is "def (x: builtins.int) -> builtins.list[builtins.int]"
reveal_type(whatever(217)) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/paramspec.pyi]
[case testInvalidParamSpecType]
# flags: --python-version 3.10
from typing import ParamSpec
P = ParamSpec("P")
class MyFunction(P): # E: Invalid base class "P"
...
[case testParamSpecRevealType]
from typing import Callable
from typing_extensions import ParamSpec
P = ParamSpec('P')
def f(x: Callable[P, int]) -> None: ...
reveal_type(f) # N: Revealed type is "def [P] (x: def (*P.args, **P.kwargs) -> builtins.int)"
[builtins fixtures/paramspec.pyi]
[case testParamSpecSimpleFunction]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
def changes_return_type_to_str(x: Callable[P, int]) -> Callable[P, str]: ...
def returns_int(a: str, b: bool) -> int: ...
reveal_type(changes_return_type_to_str(returns_int)) # N: Revealed type is "def (a: builtins.str, b: builtins.bool) -> builtins.str"
[builtins fixtures/paramspec.pyi]
[case testParamSpecSimpleClass]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, *args: P.args, **kwargs: P.kwargs) -> int:
return 1
def f(x: int, y: str) -> None: ...
reveal_type(C(f)) # N: Revealed type is "__main__.C[[x: builtins.int, y: builtins.str]]"
reveal_type(C(f).m) # N: Revealed type is "def (x: builtins.int, y: builtins.str) -> builtins.int"
[builtins fixtures/dict.pyi]
[case testParamSpecClassWithPrefixArgument]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, a: str, *args: P.args, **kwargs: P.kwargs) -> int:
return 1
def f(x: int, y: str) -> None: ...
reveal_type(C(f).m) # N: Revealed type is "def (a: builtins.str, x: builtins.int, y: builtins.str) -> builtins.int"
reveal_type(C(f).m('', 1, '')) # N: Revealed type is "builtins.int"
[builtins fixtures/dict.pyi]
[case testParamSpecDecorator]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
R = TypeVar('R')
class W(Generic[P, R]):
f: Callable[P, R]
x: int
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
reveal_type(self.f(*args, **kwargs)) # N: Revealed type is "R`2"
return self.f(*args, **kwargs)
def dec() -> Callable[[Callable[P, R]], W[P, R]]:
pass
@dec()
def f(a: int, b: str) -> None: ...
reveal_type(f) # N: Revealed type is "__main__.W[[a: builtins.int, b: builtins.str], None]"
reveal_type(f(1, '')) # N: Revealed type is "None"
reveal_type(f.x) # N: Revealed type is "builtins.int"
## TODO: How should this work?
#
# class C:
# @dec()
# def m(self, x: int) -> str: ...
#
# reveal_type(C().m(x=1))
[builtins fixtures/dict.pyi]
[case testParamSpecFunction]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
R = TypeVar('R')
def f(x: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
return x(*args, **kwargs)
def g(x: int, y: str) -> None: ...
reveal_type(f(g, 1, y='x')) # N: Revealed type is "None"
f(g, 'x', y='x') # E: Argument 2 to "f" has incompatible type "str"; expected "int"
f(g, 1, y=1) # E: Argument "y" to "f" has incompatible type "int"; expected "str"
f(g) # E: Missing positional arguments "x", "y" in call to "f"
[builtins fixtures/dict.pyi]
[case testParamSpecSpecialCase]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
def register(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> Callable[P, T]: ...
def f(x: int, y: str, z: int, a: str) -> None: ...
x = register(f, 1, '', 1, '')
[builtins fixtures/dict.pyi]
[case testParamSpecInferredFromAny]
from typing import Callable, Any
from typing_extensions import ParamSpec
P = ParamSpec('P')
def f(x: Callable[P, int]) -> Callable[P, str]: ...
g: Any
reveal_type(f(g)) # N: Revealed type is "def (*Any, **Any) -> builtins.str"
f(g)(1, 3, x=1, y=2)
[builtins fixtures/paramspec.pyi]
[case testParamSpecDecoratorImplementation]
from typing import Callable, Any, TypeVar, List
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
def dec(f: Callable[P, T]) -> Callable[P, List[T]]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> List[T]:
return [f(*args, **kwargs)]
return wrapper
@dec
def g(x: int, y: str = '') -> int: ...
reveal_type(g) # N: Revealed type is "def (x: builtins.int, y: builtins.str =) -> builtins.list[builtins.int]"
[builtins fixtures/dict.pyi]
[case testParamSpecArgsAndKwargsTypes]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, *args: P.args, **kwargs: P.kwargs) -> None:
reveal_type(args) # N: Revealed type is "P.args`1"
reveal_type(kwargs) # N: Revealed type is "P.kwargs`1"
[builtins fixtures/dict.pyi]
[case testParamSpecSubtypeChecking1]
from typing import Callable, TypeVar, Generic, Any
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, *args: P.args, **kwargs: P.kwargs) -> None:
args = args
kwargs = kwargs
o: object
o = args
o = kwargs
o2: object
args = o2 # E: Incompatible types in assignment (expression has type "object", variable has type "P.args")
kwargs = o2 # E: Incompatible types in assignment (expression has type "object", variable has type "P.kwargs")
a: Any
a = args
a = kwargs
args = kwargs # E: Incompatible types in assignment (expression has type "P.kwargs", variable has type "P.args")
kwargs = args # E: Incompatible types in assignment (expression has type "P.args", variable has type "P.kwargs")
a1: Any
args = a1
kwargs = a1
[builtins fixtures/dict.pyi]
[case testParamSpecSubtypeChecking2]
from typing import Callable, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
P2 = ParamSpec('P2')
class C(Generic[P]):
pass
def f(c1: C[P], c2: C[P2]) -> None:
c1 = c1
c2 = c2
c1 = c2 # E: Incompatible types in assignment (expression has type "C[P2]", variable has type "C[P]")
c2 = c1 # E: Incompatible types in assignment (expression has type "C[P]", variable has type "C[P2]")
def g(f: Callable[P, None], g: Callable[P2, None]) -> None:
f = f
g = g
f = g # E: Incompatible types in assignment (expression has type "Callable[P2, None]", variable has type "Callable[P, None]")
g = f # E: Incompatible types in assignment (expression has type "Callable[P, None]", variable has type "Callable[P2, None]")
[builtins fixtures/dict.pyi]
[case testParamSpecJoin]
from typing import Callable, Generic, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
P2 = ParamSpec('P2')
P3 = ParamSpec('P3')
T = TypeVar('T')
def join(x: T, y: T) -> T: ...
class C(Generic[P, P2]):
def m(self, f: Callable[P, None], g: Callable[P2, None]) -> None:
reveal_type(join(f, f)) # N: Revealed type is "def (*P.args, **P.kwargs)"
reveal_type(join(f, g)) # N: Revealed type is "builtins.function"
def m2(self, *args: P.args, **kwargs: P.kwargs) -> None:
reveal_type(join(args, args)) # N: Revealed type is "P.args`1"
reveal_type(join(kwargs, kwargs)) # N: Revealed type is "P.kwargs`1"
reveal_type(join(args, kwargs)) # N: Revealed type is "builtins.object"
def f(*args2: P2.args, **kwargs2: P2.kwargs) -> None:
reveal_type(join(args, args2)) # N: Revealed type is "builtins.object"
reveal_type(join(kwargs, kwargs2)) # N: Revealed type is "builtins.object"
def m3(self, c: C[P, P3]) -> None:
reveal_type(join(c, c)) # N: Revealed type is "__main__.C[P`1, P3`-1]"
reveal_type(join(self, c)) # N: Revealed type is "builtins.object"
[builtins fixtures/dict.pyi]
[case testParamSpecClassWithAny]
from typing import Callable, Generic, Any
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, *args: P.args, **kwargs: P.kwargs) -> int:
return 1
c: C[Any]
reveal_type(c) # N: Revealed type is "__main__.C[Any]"
reveal_type(c.m) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> builtins.int"
c.m(4, 6, y='x')
c = c
def f() -> None: pass
c2 = C(f)
c2 = c
c3 = C(f)
c = c3
[builtins fixtures/dict.pyi]
[case testParamSpecInferredFromLambda]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
# Similar to atexit.register
def register(f: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> Callable[P, T]: ...
def f(x: int) -> None: pass
def g(x: int, y: str) -> None: pass
reveal_type(register(lambda: f(1))) # N: Revealed type is "def ()"
reveal_type(register(lambda x: f(x), x=1)) # N: Revealed type is "def (x: Literal[1]?)"
register(lambda x: f(x)) # E: Cannot infer type of lambda \
# E: Argument 1 to "register" has incompatible type "Callable[[Any], None]"; expected "Callable[[], None]"
register(lambda x: f(x), y=1) # E: Argument 1 to "register" has incompatible type "def (x: int) -> None"; expected "def (y: int) -> None"
reveal_type(register(lambda x: f(x), 1)) # N: Revealed type is "def (Literal[1]?)"
reveal_type(register(lambda x, y: g(x, y), 1, "a")) # N: Revealed type is "def (Literal[1]?, Literal['a']?)"
reveal_type(register(lambda x, y: g(x, y), 1, y="a")) # N: Revealed type is "def (Literal[1]?, y: Literal['a']?)"
[builtins fixtures/dict.pyi]
[case testParamSpecInvalidCalls]
from typing import Callable, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
P2 = ParamSpec('P2')
class C(Generic[P, P2]):
def m1(self, *args: P.args, **kwargs: P.kwargs) -> None:
self.m1(*args, **kwargs)
self.m2(*args, **kwargs) # E: Argument 1 to "m2" of "C" has incompatible type "*P.args"; expected "P2.args" \
# E: Argument 2 to "m2" of "C" has incompatible type "**P.kwargs"; expected "P2.kwargs"
self.m1(*kwargs, **args) # E: Argument 1 to "m1" of "C" has incompatible type "*P.kwargs"; expected "P.args" \
# E: Argument 2 to "m1" of "C" has incompatible type "**P.args"; expected "P.kwargs"
self.m3(*args, **kwargs) # E: Argument 1 to "m3" of "C" has incompatible type "*P.args"; expected "int" \
# E: Argument 2 to "m3" of "C" has incompatible type "**P.kwargs"; expected "int"
self.m4(*args, **kwargs) # E: Argument 1 to "m4" of "C" has incompatible type "*P.args"; expected "int" \
# E: Argument 2 to "m4" of "C" has incompatible type "**P.kwargs"; expected "int"
self.m1(*args, **args) # E: Argument 2 to "m1" of "C" has incompatible type "**P.args"; expected "P.kwargs"
self.m1(*kwargs, **kwargs) # E: Argument 1 to "m1" of "C" has incompatible type "*P.kwargs"; expected "P.args"
def m2(self, *args: P2.args, **kwargs: P2.kwargs) -> None:
pass
def m3(self, *args: int, **kwargs: int) -> None:
pass
def m4(self, x: int) -> None:
pass
[builtins fixtures/dict.pyi]
[case testParamSpecOverUnannotatedDecorator]
from typing import Callable, Iterator, TypeVar, ContextManager, Any
from typing_extensions import ParamSpec
from nonexistent import deco2 # type: ignore
T = TypeVar("T")
P = ParamSpec("P")
T_co = TypeVar("T_co", covariant=True)
class CM(ContextManager[T_co]):
def __call__(self, func: T) -> T: ...
def deco1(
func: Callable[P, Iterator[T]]) -> Callable[P, CM[T]]: ...
@deco1
@deco2
def f():
pass
reveal_type(f) # N: Revealed type is "def (*Any, **Any) -> __main__.CM[Any]"
with f() as x:
pass
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
[case testParamSpecLiterals]
from typing_extensions import ParamSpec, TypeAlias
from typing import Generic, TypeVar
P = ParamSpec("P")
T = TypeVar("T")
class Z(Generic[P]): ...
# literals can be applied
n: Z[[int]]
nt1 = Z[[int]]
nt2: TypeAlias = Z[[int]]
unt1: nt1
unt2: nt2
# literals actually keep types
reveal_type(n) # N: Revealed type is "__main__.Z[[builtins.int]]"
reveal_type(unt1) # N: Revealed type is "__main__.Z[[builtins.int]]"
reveal_type(unt2) # N: Revealed type is "__main__.Z[[builtins.int]]"
# passing into a function keeps the type
def fT(a: T) -> T: ...
def fP(a: Z[P]) -> Z[P]: ...
reveal_type(fT(n)) # N: Revealed type is "__main__.Z[[builtins.int]]"
reveal_type(fP(n)) # N: Revealed type is "__main__.Z[[builtins.int]]"
# literals can be in function args and return type
def k(a: Z[[int]]) -> Z[[str]]: ...
# functions work
reveal_type(k(n)) # N: Revealed type is "__main__.Z[[builtins.str]]"
# literals can be matched in arguments
def kb(a: Z[[bytes]]) -> Z[[str]]: ...
reveal_type(kb(n)) # N: Revealed type is "__main__.Z[[builtins.str]]" \
# E: Argument 1 to "kb" has incompatible type "Z[[int]]"; expected "Z[[bytes]]"
n2: Z[bytes]
reveal_type(kb(n2)) # N: Revealed type is "__main__.Z[[builtins.str]]"
[builtins fixtures/tuple.pyi]
[case testParamSpecConcatenateFromPep]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar, Generic
P = ParamSpec("P")
R = TypeVar("R")
# CASE 1
class Request:
...
def with_request(f: Callable[Concatenate[Request, P], R]) -> Callable[P, R]:
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
return f(Request(), *args, **kwargs)
return inner
@with_request
def takes_int_str(request: Request, x: int, y: str) -> int:
# use request
return x + 7
reveal_type(takes_int_str) # N: Revealed type is "def (x: builtins.int, y: builtins.str) -> builtins.int"
takes_int_str(1, "A") # Accepted
takes_int_str("B", 2) # E: Argument 1 to "takes_int_str" has incompatible type "str"; expected "int" \
# E: Argument 2 to "takes_int_str" has incompatible type "int"; expected "str"
# CASE 2
T = TypeVar("T")
P_2 = ParamSpec("P_2")
class X(Generic[T, P]):
f: Callable[P, int]
x: T
def f1(x: X[int, P_2]) -> str: ... # Accepted
def f2(x: X[int, Concatenate[int, P_2]]) -> str: ... # Accepted
def f3(x: X[int, [int, bool]]) -> str: ... # Accepted
# ellipsis only show up here, but I can assume it works like Callable[..., R]
def f4(x: X[int, ...]) -> str: ... # Accepted
def f5(x: X[int, int]) -> str: ... # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
# CASE 3
def bar(x: int, *args: bool) -> int: ...
def add(x: Callable[P, int]) -> Callable[Concatenate[str, P], bool]: ...
reveal_type(add(bar)) # N: Revealed type is "def (builtins.str, x: builtins.int, *args: builtins.bool) -> builtins.bool"
def remove(x: Callable[Concatenate[int, P], int]) -> Callable[P, bool]: ...
reveal_type(remove(bar)) # N: Revealed type is "def (*args: builtins.bool) -> builtins.bool"
def transform(
x: Callable[Concatenate[int, P], int]
) -> Callable[Concatenate[str, P], bool]: ...
# In the PEP, "__a" appears. What is that? Autogenerated names? To what spec?
reveal_type(transform(bar)) # N: Revealed type is "def (builtins.str, *args: builtins.bool) -> builtins.bool"
# CASE 4
def expects_int_first(x: Callable[Concatenate[int, P], int]) -> None: ...
@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "Callable[[str], int]"; expected "Callable[[int], int]" \
# N: This is likely because "one" has named arguments: "x". Consider marking them positional-only
def one(x: str) -> int: ...
@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "def two(*, x: int) -> int"; expected "def (int, /, *, x: int) -> int"
def two(*, x: int) -> int: ...
@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "def three(**kwargs: int) -> int"; expected "def (int, /, **kwargs: int) -> int"
def three(**kwargs: int) -> int: ...
@expects_int_first # Accepted
def four(*args: int) -> int: ...
[builtins fixtures/dict.pyi]
[case testParamSpecTwiceSolving]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def f(one: Callable[Concatenate[int, P], R], two: Callable[Concatenate[str, P], R]) -> Callable[P, R]: ...
a: Callable[[int, bytes], str]
b: Callable[[str, bytes], str]
reveal_type(f(a, b)) # N: Revealed type is "def (builtins.bytes) -> builtins.str"
[builtins fixtures/paramspec.pyi]
[case testParamSpecConcatenateInReturn]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, Protocol
P = ParamSpec("P")
def f(i: Callable[Concatenate[int, P], str]) -> Callable[Concatenate[int, P], str]: ...
n: Callable[[int, bytes], str]
reveal_type(f(n)) # N: Revealed type is "def (builtins.int, builtins.bytes) -> builtins.str"
[builtins fixtures/paramspec.pyi]
[case testParamSpecConcatenateNamedArgs]
# flags: --extra-checks
# this is one noticeable deviation from PEP but I believe it is for the better
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def f1(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
def result(x: int, /, *args: P.args, **kwargs: P.kwargs) -> R: ...
return result # Accepted
def f2(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
def result(x: int, *args: P.args, **kwargs: P.kwargs) -> R: ...
return result # Rejected
# reason for rejection:
f2(lambda x: 42)(42, x=42)
[builtins fixtures/paramspec.pyi]
[out]
main:17: error: Incompatible return value type (got "Callable[[Arg(int, 'x'), **P], R]", expected "Callable[[int, **P], R]")
main:17: note: This is likely because "result" has named arguments: "x". Consider marking them positional-only
[case testNonStrictParamSpecConcatenateNamedArgs]
# this is one noticeable deviation from PEP but I believe it is for the better
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def f1(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
def result(x: int, /, *args: P.args, **kwargs: P.kwargs) -> R: ...
return result # Accepted
def f2(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
def result(x: int, *args: P.args, **kwargs: P.kwargs) -> R: ...
return result # Rejected -> Accepted
# reason for rejection:
f2(lambda x: 42)(42, x=42)
[builtins fixtures/paramspec.pyi]
[case testParamSpecConcatenateWithTypeVar]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
S = TypeVar("S")
def f(c: Callable[Concatenate[S, P], R]) -> Callable[Concatenate[S, P], R]: ...
def a(n: int) -> None: ...
n = f(a)
reveal_type(n) # N: Revealed type is "def (builtins.int)"
reveal_type(n(42)) # N: Revealed type is "None"
[builtins fixtures/paramspec.pyi]
[case testCallablesAsParameters]
# credits to https://github.com/microsoft/pyright/issues/2705
from typing_extensions import ParamSpec, Concatenate
from typing import Generic, Callable, Any
P = ParamSpec("P")
class Foo(Generic[P]):
def __init__(self, func: Callable[P, Any]) -> None: ...
def bar(baz: Foo[Concatenate[int, P]]) -> Foo[P]: ...
def test(a: int, /, b: str) -> str: ...
abc = Foo(test)
reveal_type(abc)
bar(abc)
[builtins fixtures/paramspec.pyi]
[out]
main:14: note: Revealed type is "__main__.Foo[[builtins.int, b: builtins.str]]"
[case testSolveParamSpecWithSelfType]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, Generic
P = ParamSpec("P")
class Foo(Generic[P]):
def foo(self: 'Foo[P]', other: Callable[P, None]) -> None: ...
n: Foo[[int]]
def f(x: int) -> None: ...
n.foo(f)
[builtins fixtures/paramspec.pyi]
[case testParamSpecLiteralsTypeApplication]
from typing_extensions import ParamSpec
from typing import Generic, Callable
P = ParamSpec("P")
class Z(Generic[P]):
def __init__(self, c: Callable[P, None]) -> None:
...
# it allows valid functions
reveal_type(Z[[int]](lambda x: None)) # N: Revealed type is "__main__.Z[[builtins.int]]"
reveal_type(Z[[]](lambda: None)) # N: Revealed type is "__main__.Z[[]]"
reveal_type(Z[bytes, str](lambda b, s: None)) # N: Revealed type is "__main__.Z[[builtins.bytes, builtins.str]]"
# it disallows invalid functions
def f1(n: str) -> None: ...
def f2(b: bytes, i: int) -> None: ...
Z[[int]](lambda one, two: None) # E: Cannot infer type of lambda \
# E: Argument 1 to "Z" has incompatible type "Callable[[Any, Any], None]"; expected "Callable[[int], None]"
Z[[int]](f1) # E: Argument 1 to "Z" has incompatible type "Callable[[str], None]"; expected "Callable[[int], None]"
Z[[]](lambda one: None) # E: Cannot infer type of lambda \
# E: Argument 1 to "Z" has incompatible type "Callable[[Any], None]"; expected "Callable[[], None]"
Z[bytes, str](lambda one: None) # E: Cannot infer type of lambda \
# E: Argument 1 to "Z" has incompatible type "Callable[[Any], None]"; expected "Callable[[bytes, str], None]"
Z[bytes, str](f2) # E: Argument 1 to "Z" has incompatible type "Callable[[bytes, int], None]"; expected "Callable[[bytes, str], None]"
[builtins fixtures/paramspec.pyi]
[case testParamSpecLiteralEllipsis]
from typing_extensions import ParamSpec
from typing import Generic, Callable
P = ParamSpec("P")
class Z(Generic[P]):
def __init__(self: 'Z[P]', c: Callable[P, None]) -> None:
...
def f1() -> None: ...
def f2(*args: int) -> None: ...
def f3(a: int, *, b: bytes) -> None: ...
def f4(b: bytes) -> None: ...
argh: Callable[..., None] = f4
# check it works
Z[...](f1)
Z[...](f2)
Z[...](f3)
# check subtyping works
n: Z[...]
n = Z(f1)
n = Z(f2)
n = Z(f3)
[builtins fixtures/paramspec.pyi]
[case testParamSpecApplyConcatenateTwice]
from typing_extensions import ParamSpec, Concatenate
from typing import Generic, Callable, Optional
P = ParamSpec("P")
class C(Generic[P]):
# think PhantomData<T> from rust
phantom: Optional[Callable[P, None]]
def add_str(self) -> C[Concatenate[str, P]]:
return C[Concatenate[str, P]]()
def add_int(self) -> C[Concatenate[int, P]]:
return C[Concatenate[int, P]]()
def f(c: C[P]) -> None:
reveal_type(c) # N: Revealed type is "__main__.C[P`-1]"
n1 = c.add_str()
reveal_type(n1) # N: Revealed type is "__main__.C[[builtins.str, **P`-1]]"
n2 = n1.add_int()
reveal_type(n2) # N: Revealed type is "__main__.C[[builtins.int, builtins.str, **P`-1]]"
p1 = c.add_int()
reveal_type(p1) # N: Revealed type is "__main__.C[[builtins.int, **P`-1]]"
p2 = p1.add_str()
reveal_type(p2) # N: Revealed type is "__main__.C[[builtins.str, builtins.int, **P`-1]]"
[builtins fixtures/paramspec.pyi]
[case testParamSpecLiteralJoin]
from typing import Generic, Callable, Union
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
class Job(Generic[_P]):
def __init__(self, target: Callable[_P, None]) -> None:
self.target = target
def func(
action: Union[Job[int], Callable[[int], None]],
) -> None:
job = action if isinstance(action, Job) else Job(action)
reveal_type(job) # N: Revealed type is "__main__.Job[[builtins.int]]"
[builtins fixtures/paramspec.pyi]
[case testApplyParamSpecToParamSpecLiterals]
from typing import TypeVar, Generic, Callable
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
_R_co = TypeVar("_R_co", covariant=True)
class Job(Generic[_P, _R_co]):
def __init__(self, target: Callable[_P, _R_co]) -> None:
self.target = target
def run_job(job: Job[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> None: # N: "run_job" defined here
...
def func(job: Job[[int, str], None]) -> None:
run_job(job, 42, "Hello")
run_job(job, "Hello", 42) # E: Argument 2 to "run_job" has incompatible type "str"; expected "int" \
# E: Argument 3 to "run_job" has incompatible type "int"; expected "str"
run_job(job, 42, msg="Hello") # E: Unexpected keyword argument "msg" for "run_job"
run_job(job, "Hello") # E: Too few arguments for "run_job" \
# E: Argument 2 to "run_job" has incompatible type "str"; expected "int"
def func2(job: Job[..., None]) -> None:
run_job(job, 42, "Hello")
run_job(job, "Hello", 42)
run_job(job, 42, msg="Hello")
run_job(job, x=42, msg="Hello")
[builtins fixtures/paramspec.pyi]
[case testExpandNonBareParamSpecAgainstCallable]
from typing import Callable, TypeVar, Any
from typing_extensions import ParamSpec
CallableT = TypeVar("CallableT", bound=Callable[..., Any])
_P = ParamSpec("_P")
_R = TypeVar("_R")
def simple_decorator(callable: CallableT) -> CallableT:
# set some attribute on 'callable'
return callable
class A:
@simple_decorator
def func(self, action: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R:
...
reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`4, *_P.args, **_P.kwargs) -> _R`4"
reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`8, *_P.args, **_P.kwargs) -> _R`8"
def f(x: int) -> int:
...
reveal_type(A().func(f, 42)) # N: Revealed type is "builtins.int"
reveal_type(A().func(lambda x: x + x, 42)) # N: Revealed type is "builtins.int"
[builtins fixtures/paramspec.pyi]
[case testParamSpecConstraintOnOtherParamSpec]
from typing import Callable, TypeVar, Any, Generic
from typing_extensions import ParamSpec
CallableT = TypeVar("CallableT", bound=Callable[..., Any])
_P = ParamSpec("_P")
_R_co = TypeVar("_R_co", covariant=True)
def simple_decorator(callable: CallableT) -> CallableT:
...
class Job(Generic[_P, _R_co]):
def __init__(self, target: Callable[_P, _R_co]) -> None:
...
class A:
@simple_decorator
def func(self, action: Job[_P, None]) -> Job[_P, None]:
...
reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`3, None]) -> __main__.Job[_P`3, None]"
reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`5, None]) -> __main__.Job[_P`5, None]"
reveal_type(A().func(Job(lambda x: x))) # N: Revealed type is "__main__.Job[[x: Any], None]"
def f(x: int, y: int) -> None: ...
reveal_type(A().func(Job(f))) # N: Revealed type is "__main__.Job[[x: builtins.int, y: builtins.int], None]"
[builtins fixtures/paramspec.pyi]
[case testConstraintBetweenParamSpecFunctions1]
from typing import Callable, TypeVar, Any, Generic
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
_R_co = TypeVar("_R_co", covariant=True)
def simple_decorator(callable: Callable[_P, _R_co]) -> Callable[_P, _R_co]: ...
class Job(Generic[_P]): ...
@simple_decorator
def func(__action: Job[_P]) -> Callable[_P, None]:
...
reveal_type(func) # N: Revealed type is "def [_P] (__main__.Job[_P`-1]) -> def (*_P.args, **_P.kwargs)"
[builtins fixtures/paramspec.pyi]
[case testConstraintBetweenParamSpecFunctions2]
from typing import Callable, TypeVar, Any, Generic
from typing_extensions import ParamSpec
CallableT = TypeVar("CallableT", bound=Callable[..., Any])
_P = ParamSpec("_P")
def simple_decorator(callable: CallableT) -> CallableT: ...
class Job(Generic[_P]): ...
@simple_decorator
def func(__action: Job[_P]) -> Callable[_P, None]:
...
reveal_type(func) # N: Revealed type is "def [_P] (__main__.Job[_P`-1]) -> def (*_P.args, **_P.kwargs)"
[builtins fixtures/paramspec.pyi]
[case testConstraintsBetweenConcatenatePrefixes]
from typing import Any, Callable, Generic, TypeVar
from typing_extensions import Concatenate, ParamSpec
_P = ParamSpec("_P")
_T = TypeVar("_T")
class Awaitable(Generic[_T]): ...
def adds_await() -> Callable[
[Callable[Concatenate[_T, _P], None]],
Callable[Concatenate[_T, _P], Awaitable[None]],
]:
def decorator(
func: Callable[Concatenate[_T, _P], None],
) -> Callable[Concatenate[_T, _P], Awaitable[None]]:
...
return decorator # we want `_T` and `_P` to refer to the same things.
[builtins fixtures/paramspec.pyi]
[case testParamSpecVariance]
from typing import Callable, Generic
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
class Job(Generic[_P]):
def __init__(self, target: Callable[_P, None]) -> None: ...
def into_callable(self) -> Callable[_P, None]: ...
class A:
def func(self, var: int) -> None: ...
def other_func(self, job: Job[[int]]) -> None: ...
job = Job(A().func)
reveal_type(job) # N: Revealed type is "__main__.Job[[var: builtins.int]]"
A().other_func(job) # This should NOT error (despite the keyword)
# and yet the keyword should remain
job.into_callable()(var=42)
job.into_callable()(x=42) # E: Unexpected keyword argument "x"
# similar for other functions
def f1(n: object) -> None: ...
def f2(n: int) -> None: ...
def f3(n: bool) -> None: ...
# just like how this is legal...
a1: Callable[[bool], None]
a1 = f3
a1 = f2
a1 = f1
# ... this is also legal
a2: Job[[bool]]
a2 = Job(f3)
a2 = Job(f2)
a2 = Job(f1)
# and this is not legal
def f4(n: bytes) -> None: ...
a1 = f4 # E: Incompatible types in assignment (expression has type "Callable[[bytes], None]", variable has type "Callable[[bool], None]")
a2 = Job(f4) # E: Argument 1 to "Job" has incompatible type "Callable[[bytes], None]"; expected "Callable[[bool], None]"
# nor is this:
a4: Job[[int]]
a4 = Job(f3) # E: Argument 1 to "Job" has incompatible type "Callable[[bool], None]"; expected "Callable[[int], None]"
a4 = Job(f2)
a4 = Job(f1)
# just like this:
a3: Callable[[int], None]
a3 = f3 # E: Incompatible types in assignment (expression has type "Callable[[bool], None]", variable has type "Callable[[int], None]")
a3 = f2
a3 = f1
[builtins fixtures/paramspec.pyi]
[case testDecoratingClassesThatUseParamSpec]
from typing import Generic, TypeVar, Callable, Any
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
_T = TypeVar("_T")
_F = TypeVar("_F", bound=Callable[..., Any])
def f(x: _F) -> _F: ...
@f # Should be ok
class OnlyParamSpec(Generic[_P]):
pass
@f # Should be ok
class MixedWithTypeVar1(Generic[_P, _T]):
pass
@f # Should be ok
class MixedWithTypeVar2(Generic[_T, _P]):
pass
[builtins fixtures/dict.pyi]
[case testGenericsInInferredParamspec]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
_T = TypeVar("_T")
class Job(Generic[_P]):
def __init__(self, target: Callable[_P, None]) -> None: ...
def into_callable(self) -> Callable[_P, None]: ...
def generic_f(x: _T) -> None: ...
j = Job(generic_f)
reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`-1]]"
jf = j.into_callable()
reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`4)"
reveal_type(jf(1)) # N: Revealed type is "None"
[builtins fixtures/paramspec.pyi]
[case testGenericsInInferredParamspecReturn]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
_T = TypeVar("_T")
class Job(Generic[_P, _T]):
def __init__(self, target: Callable[_P, _T]) -> None: ...
def into_callable(self) -> Callable[_P, _T]: ...
def generic_f(x: _T) -> _T: ...
j = Job(generic_f)
reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`3], _T`3]"
jf = j.into_callable()
reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`4) -> _T`4"
reveal_type(jf(1)) # N: Revealed type is "builtins.int"
[builtins fixtures/paramspec.pyi]
[case testStackedConcatenateIsIllegal]
from typing_extensions import Concatenate, ParamSpec
from typing import Callable
P = ParamSpec("P")
def x(f: Callable[Concatenate[int, Concatenate[int, P]], None]) -> None: ... # E: Nested Concatenates are invalid
[builtins fixtures/paramspec.pyi]
[case testPropagatedAnyConstraintsAreOK]
from typing import Any, Callable, Generic, TypeVar
from typing_extensions import ParamSpec
T = TypeVar("T")
P = ParamSpec("P")
def callback(func: Callable[[Any], Any]) -> None: ...
class Job(Generic[P]): ...
@callback
def run_job(job: Job[...]) -> T: ... # E: A function returning TypeVar should receive at least one argument containing the same TypeVar
[builtins fixtures/tuple.pyi]
[case testTupleAndDictOperationsOnParamSpecArgsAndKwargs]
from typing import Callable, Iterator, Iterable, TypeVar, Tuple
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
def enumerate(x: Iterable[T]) -> Iterator[Tuple[int, T]]: ...
def func(callback: Callable[P, str]) -> Callable[P, str]:
def inner(*args: P.args, **kwargs: P.kwargs) -> str:
reveal_type(args[5]) # N: Revealed type is "builtins.object"
for a in args:
reveal_type(a) # N: Revealed type is "builtins.object"
for idx, a in enumerate(args):
reveal_type(idx) # N: Revealed type is "builtins.int"
reveal_type(a) # N: Revealed type is "builtins.object"
b = 'foo' in args
reveal_type(b) # N: Revealed type is "builtins.bool"
reveal_type(args.count(42)) # N: Revealed type is "builtins.int"
reveal_type(len(args)) # N: Revealed type is "builtins.int"
for c, d in kwargs.items():
reveal_type(c) # N: Revealed type is "builtins.str"
reveal_type(d) # N: Revealed type is "builtins.object"
kwargs.pop('bar')
return 'baz'
return inner
[builtins fixtures/paramspec.pyi]
[case testUnpackingParamsSpecArgsAndKwargs]
from typing import Callable
from typing_extensions import ParamSpec
P = ParamSpec("P")
def func(callback: Callable[P, str]) -> Callable[P, str]:
def inner(*args: P.args, **kwargs: P.kwargs) -> str:
a, *b = args
reveal_type(a) # N: Revealed type is "builtins.object"
reveal_type(b) # N: Revealed type is "builtins.list[builtins.object]"
c, *d = kwargs
reveal_type(c) # N: Revealed type is "builtins.str"
reveal_type(d) # N: Revealed type is "builtins.list[builtins.str]"
e = {**kwargs}
reveal_type(e) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
return "foo"
return inner
[builtins fixtures/paramspec.pyi]
[case testParamSpecArgsAndKwargsMismatch]
from typing import Callable
from typing_extensions import ParamSpec
P1 = ParamSpec("P1")
def func(callback: Callable[P1, str]) -> Callable[P1, str]:
def inner(
*args: P1.kwargs, # E: Use "P1.args" for variadic "*" parameter
**kwargs: P1.args, # E: Use "P1.kwargs" for variadic "**" parameter
) -> str:
return "foo"
return inner
[builtins fixtures/paramspec.pyi]
[case testParamSpecTestPropAccess]
from typing import Callable
from typing_extensions import ParamSpec
P1 = ParamSpec("P1")
def func1(callback: Callable[P1, str]) -> Callable[P1, str]:
def inner(
*args: P1.typo, # E: Use "P1.args" for variadic "*" parameter \
# E: Name "P1.typo" is not defined
**kwargs: P1.kwargs,
) -> str:
return "foo"
return inner
def func2(callback: Callable[P1, str]) -> Callable[P1, str]:
def inner(
*args: P1.args,
**kwargs: P1.__bound__, # E: Use "P1.kwargs" for variadic "**" parameter \
# E: Name "P1.__bound__" is not defined
) -> str:
return "foo"
return inner
def func3(callback: Callable[P1, str]) -> Callable[P1, str]:
def inner(
*args: P1.__bound__, # E: Use "P1.args" for variadic "*" parameter \
# E: Name "P1.__bound__" is not defined
**kwargs: P1.invalid, # E: Use "P1.kwargs" for variadic "**" parameter \
# E: Name "P1.invalid" is not defined
) -> str:
return "foo"
return inner
[builtins fixtures/paramspec.pyi]
[case testInvalidParamSpecDefinitionsWithArgsKwargs]
from typing import Callable, ParamSpec
P = ParamSpec('P')
def c1(f: Callable[P, int], *args: P.args, **kwargs: P.kwargs) -> int: ...
def c2(f: Callable[P, int]) -> int: ...
def c3(f: Callable[P, int], *args, **kwargs) -> int: ...
# It is ok to define,
def c4(f: Callable[P, int], *args: int, **kwargs: str) -> int:
# but not ok to call:
f(*args, **kwargs) # E: Argument 1 has incompatible type "*tuple[int, ...]"; expected "P.args" \
# E: Argument 2 has incompatible type "**dict[str, str]"; expected "P.kwargs"
return 1
def f1(f: Callable[P, int], *args, **kwargs: P.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f2(f: Callable[P, int], *args: P.args, **kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f3(f: Callable[P, int], *args: P.args) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f4(f: Callable[P, int], **kwargs: P.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f5(f: Callable[P, int], *args: P.args, extra_keyword_arg: int, **kwargs: P.kwargs) -> int: ... # E: Arguments not allowed after ParamSpec.args
# Error message test:
P1 = ParamSpec('P1')
def m1(f: Callable[P1, int], *a, **k: P1.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P1.args" and "**kwargs" typed as "P1.kwargs"
[builtins fixtures/paramspec.pyi]
[case testInvalidParamSpecAndConcatenateDefinitionsWithArgsKwargs]
from typing import Callable, ParamSpec
from typing_extensions import Concatenate
P = ParamSpec('P')
def c1(f: Callable[Concatenate[int, P], int], *args: P.args, **kwargs: P.kwargs) -> int: ...
def c2(f: Callable[Concatenate[int, P], int]) -> int: ...
def c3(f: Callable[Concatenate[int, P], int], *args, **kwargs) -> int: ...
# It is ok to define,
def c4(f: Callable[Concatenate[int, P], int], *args: int, **kwargs: str) -> int:
# but not ok to call:
f(1, *args, **kwargs) # E: Argument 2 has incompatible type "*tuple[int, ...]"; expected "P.args" \
# E: Argument 3 has incompatible type "**dict[str, str]"; expected "P.kwargs"
return 1
def f1(f: Callable[Concatenate[int, P], int], *args, **kwargs: P.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f2(f: Callable[Concatenate[int, P], int], *args: P.args, **kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f3(f: Callable[Concatenate[int, P], int], *args: P.args) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f4(f: Callable[Concatenate[int, P], int], **kwargs: P.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f5(f: Callable[Concatenate[int, P], int], *args: P.args, extra_keyword_arg: int, **kwargs: P.kwargs) -> int: ... # E: Arguments not allowed after ParamSpec.args
[builtins fixtures/paramspec.pyi]
[case testValidParamSpecInsideGenericWithoutArgsAndKwargs]
from typing import Callable, ParamSpec, Generic
from typing_extensions import Concatenate
P = ParamSpec('P')
class Some(Generic[P]): ...
def create(s: Some[P], *args: int): ...
def update(s: Some[P], **kwargs: int): ...
def delete(s: Some[P]): ...
def from_callable1(c: Callable[P, int], *args: int, **kwargs: int) -> Some[P]: ...
def from_callable2(c: Callable[P, int], **kwargs: int) -> Some[P]: ...
def from_callable3(c: Callable[P, int], *args: int) -> Some[P]: ...
def from_extra1(c: Callable[Concatenate[int, P], int], *args: int, **kwargs: int) -> Some[P]: ...
def from_extra2(c: Callable[Concatenate[int, P], int], **kwargs: int) -> Some[P]: ...
def from_extra3(c: Callable[Concatenate[int, P], int], *args: int) -> Some[P]: ...
[builtins fixtures/paramspec.pyi]
[case testUnboundParamSpec]
from typing import Callable, ParamSpec
P1 = ParamSpec('P1')
P2 = ParamSpec('P2')
def f0(f: Callable[P1, int], *args: P1.args, **kwargs: P2.kwargs): ... # E: ParamSpec must have "*args" typed as "P1.args" and "**kwargs" typed as "P1.kwargs" \
# E: ParamSpec "P2" is unbound
def f1(*args: P1.args): ... # E: ParamSpec "P1" is unbound
def f2(**kwargs: P1.kwargs): ... # E: ParamSpec "P1" is unbound
def f3(*args: P1.args, **kwargs: int): ... # E: ParamSpec "P1" is unbound
def f4(*args: int, **kwargs: P1.kwargs): ... # E: ParamSpec "P1" is unbound
# Error message is based on the `args` definition:
def f5(*args: P2.args, **kwargs: P1.kwargs): ... # E: ParamSpec "P2" is unbound \
# E: ParamSpec "P1" is unbound
def f6(*args: P1.args, **kwargs: P2.kwargs): ... # E: ParamSpec "P1" is unbound \
# E: ParamSpec "P2" is unbound
# Multiple `ParamSpec` variables can be found, they should not affect error message:
P3 = ParamSpec('P3')
def f7(first: Callable[P3, int], *args: P1.args, **kwargs: P2.kwargs): ... # E: ParamSpec "P1" is unbound \
# E: ParamSpec "P2" is unbound
def f8(first: Callable[P3, int], *args: P2.args, **kwargs: P1.kwargs): ... # E: ParamSpec "P2" is unbound \
# E: ParamSpec "P1" is unbound
[builtins fixtures/paramspec.pyi]
[case testArgsKwargsWithoutParamSpecVar]
from typing import Generic, Callable, ParamSpec
P = ParamSpec('P')
# This must be allowed:
class Some(Generic[P]):
def call(self, *args: P.args, **kwargs: P.kwargs): ...
def call(*args: P.args, **kwargs: P.kwargs): ... # E: ParamSpec "P" is unbound
[builtins fixtures/paramspec.pyi]
[case testParamSpecInferenceCrash]
from typing import Callable, Generic, ParamSpec, TypeVar
def foo(x: int) -> int: ...
T = TypeVar("T")
def bar(x: T) -> T: ...
P = ParamSpec("P")
class C(Generic[P]):
def __init__(self, fn: Callable[P, int], *args: P.args, **kwargs: P.kwargs): ...
reveal_type(bar(C(fn=foo, x=1))) # N: Revealed type is "__main__.C[[x: builtins.int]]"
[builtins fixtures/paramspec.pyi]
[case testParamSpecClassConstructor]
from typing import ParamSpec, Callable, TypeVar
P = ParamSpec("P")
class SomeClass:
def __init__(self, a: str) -> None:
pass
def func(t: Callable[P, SomeClass], val: Callable[P, SomeClass]) -> Callable[P, SomeClass]:
pass
def func_regular(t: Callable[[T], SomeClass], val: Callable[[T], SomeClass]) -> Callable[[T], SomeClass]:
pass
def constructor(a: str) -> SomeClass:
return SomeClass(a)
def wrong_constructor(a: bool) -> SomeClass:
return SomeClass("a")
def wrong_name_constructor(b: bool) -> SomeClass:
return SomeClass("a")
func(SomeClass, constructor)
reveal_type(func(SomeClass, wrong_constructor)) # N: Revealed type is "def (a: Never) -> __main__.SomeClass"
reveal_type(func_regular(SomeClass, wrong_constructor)) # N: Revealed type is "def (Never) -> __main__.SomeClass"
reveal_type(func(SomeClass, wrong_name_constructor)) # N: Revealed type is "def (Never) -> __main__.SomeClass"
[builtins fixtures/paramspec.pyi]
[case testParamSpecInTypeAliasBasic]
from typing import ParamSpec, Callable
P = ParamSpec("P")
C = Callable[P, int]
def f(n: C[P]) -> C[P]: ...
@f
def bar(x: int) -> int: ...
@f # E: Argument 1 to "f" has incompatible type "Callable[[int], str]"; expected "Callable[[int], int]"
def foo(x: int) -> str: ...
x: C[[int, str]]
reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int"
y: C[int, str]
reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int"
[builtins fixtures/paramspec.pyi]
[case testParamSpecInTypeAliasConcatenate]
from typing import ParamSpec, Callable
from typing_extensions import Concatenate
P = ParamSpec("P")
C = Callable[Concatenate[int, P], int]
def f(n: C[P]) -> C[P]: ...
@f # E: Argument 1 to "f" has incompatible type "Callable[[], int]"; expected "Callable[[int], int]"
def bad() -> int: ...
@f
def bar(x: int) -> int: ...
@f
def bar2(x: int, y: str) -> int: ...
reveal_type(bar2) # N: Revealed type is "def (builtins.int, y: builtins.str) -> builtins.int"
@f # E: Argument 1 to "f" has incompatible type "Callable[[int], str]"; expected "Callable[[int], int]" \
# N: This is likely because "foo" has named arguments: "x". Consider marking them positional-only
def foo(x: int) -> str: ...
@f # E: Argument 1 to "f" has incompatible type "Callable[[str, int], int]"; expected "Callable[[int, int], int]" \
# N: This is likely because "foo2" has named arguments: "x". Consider marking them positional-only
def foo2(x: str, y: int) -> int: ...
x: C[[int, str]]
reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.int, builtins.str) -> builtins.int"
y: C[int, str]
reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.int, builtins.str) -> builtins.int"
[builtins fixtures/paramspec.pyi]
[case testParamSpecInTypeAliasIllegalBare]
from typing import ParamSpec
from typing_extensions import Concatenate, TypeAlias
P = ParamSpec("P")
Bad1: TypeAlias = P # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
Bad2: TypeAlias = Concatenate[int, P] # E: Invalid location for Concatenate \
# N: You can use Concatenate as the first argument to Callable
[builtins fixtures/paramspec.pyi]
[case testParamSpecInTypeAliasRecursive]
from typing import ParamSpec, Callable, Union
P = ParamSpec("P")
C = Callable[P, Union[int, C[P]]]
def f(n: C[P]) -> C[P]: ...
@f
def bar(x: int) -> int: ...
@f
def bar2(__x: int) -> Callable[[int], int]: ...
@f # E: Argument 1 to "f" has incompatible type "Callable[[int], str]"; expected "C[[int]]"
def foo(x: int) -> str: ...
@f # E: Argument 1 to "f" has incompatible type "Callable[[int], Callable[[int], str]]"; expected "C[[int]]"
def foo2(__x: int) -> Callable[[int], str]: ...
x: C[[int, str]]
reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.str) -> Union[builtins.int, ...]"
y: C[int, str]
reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> Union[builtins.int, ...]"
[builtins fixtures/paramspec.pyi]
[case testParamSpecAliasInRuntimeContext]
from typing import ParamSpec, Generic
P = ParamSpec("P")
class C(Generic[P]): ...
c = C[int, str]()
reveal_type(c) # N: Revealed type is "__main__.C[[builtins.int, builtins.str]]"
A = C[P]
a = A[int, str]()
reveal_type(a) # N: Revealed type is "__main__.C[[builtins.int, builtins.str]]"
[builtins fixtures/paramspec.pyi]
[case testParamSpecAliasInvalidLocations]
from typing import ParamSpec, Generic, List, TypeVar, Callable
P = ParamSpec("P")
T = TypeVar("T")
A = List[T]
def f(x: A[[int, str]]) -> None: ... # E: Bracketed expression "[...]" is not valid as a type
def g(x: A[P]) -> None: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
C = Callable[P, T]
x: C[int] # E: Bad number of arguments for type alias, expected 2, given 1
y: C[int, str] # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
z: C[int, str, bytes] # E: Bad number of arguments for type alias, expected 2, given 3
[builtins fixtures/paramspec.pyi]
[case testTrivialParametersHandledCorrectly]
from typing import ParamSpec, Generic, TypeVar, Callable, Any
from typing_extensions import Concatenate
P = ParamSpec("P")
T = TypeVar("T")
S = TypeVar("S")
class C(Generic[S, P, T]): ...
def foo(f: Callable[P, int]) -> None:
x: C[Any, ..., Any]
x1: C[int, Concatenate[int, str, P], str]
x = x1 # OK
[builtins fixtures/paramspec.pyi]
[case testParamSpecAliasNested]
from typing import ParamSpec, Callable, List, TypeVar, Generic
from typing_extensions import Concatenate
P = ParamSpec("P")
A = List[Callable[P, None]]
B = List[Callable[Concatenate[int, P], None]]
fs: A[int, str]
reveal_type(fs) # N: Revealed type is "builtins.list[def (builtins.int, builtins.str)]"
gs: B[int, str]
reveal_type(gs) # N: Revealed type is "builtins.list[def (builtins.int, builtins.int, builtins.str)]"
T = TypeVar("T")
class C(Generic[T]): ...
C[Callable[P, int]]()
[builtins fixtures/paramspec.pyi]
[case testConcatDeferralNoCrash]
from typing import Callable, TypeVar
from typing_extensions import Concatenate, ParamSpec
P = ParamSpec("P")
T = TypeVar("T", bound="Defer")
Alias = Callable[P, bool]
Concat = Alias[Concatenate[T, P]]
def test(f: Concat[T, ...]) -> None: ...
class Defer: ...
[builtins fixtures/paramspec.pyi]
[case testNoParamSpecDoubling]
# https://github.com/python/mypy/issues/12734
from typing import Callable, ParamSpec
from typing_extensions import Concatenate
P = ParamSpec("P")
Q = ParamSpec("Q")
def foo(f: Callable[P, int]) -> Callable[P, int]:
return f
def bar(f: Callable[Concatenate[str, Q], int]) -> Callable[Concatenate[str, Q], int]:
return foo(f)
[builtins fixtures/paramspec.pyi]
[case testAlreadyExpandedCallableWithParamSpecReplacement]
from typing import Callable, Any, overload
from typing_extensions import Concatenate, ParamSpec
P = ParamSpec("P")
@overload
def command() -> Callable[[Callable[Concatenate[object, object, P], object]], None]:
...
@overload
def command(
cls: int = ...,
) -> Callable[[Callable[Concatenate[object, P], object]], None]:
...
def command(
cls: int = 42,
) -> Any:
...
[builtins fixtures/paramspec.pyi]
[case testCopiedParamSpecComparison]
# minimized from https://github.com/python/mypy/issues/12909
from typing import Callable
from typing_extensions import ParamSpec
P = ParamSpec("P")
def identity(func: Callable[P, None]) -> Callable[P, None]: ...
@identity
def f(f: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
[builtins fixtures/paramspec.pyi]
[case testParamSpecDecoratorAppliedToGeneric]
from typing import Callable, List, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec("P")
T = TypeVar("T")
U = TypeVar("U")
def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ...
def test(x: U) -> U: ...
reveal_type(dec) # N: Revealed type is "def [P, T] (f: def (*P.args, **P.kwargs) -> T`-2) -> def (*P.args, **P.kwargs) -> builtins.list[T`-2]"
reveal_type(dec(test)) # N: Revealed type is "def [T] (x: T`3) -> builtins.list[T`3]"
class A: ...
TA = TypeVar("TA", bound=A)
def test_with_bound(x: TA) -> TA: ...
reveal_type(dec(test_with_bound)) # N: Revealed type is "def [T <: __main__.A] (x: T`5) -> builtins.list[T`5]"
dec(test_with_bound)(0) # E: Value of type variable "T" of function cannot be "int"
dec(test_with_bound)(A()) # OK
[builtins fixtures/paramspec.pyi]
[case testParamSpecArgumentParamInferenceRegular]
from typing import TypeVar, Generic
from typing_extensions import ParamSpec
P = ParamSpec("P")
class Foo(Generic[P]):
def call(self, *args: P.args, **kwargs: P.kwargs) -> None: ...
def test(*args: P.args, **kwargs: P.kwargs) -> Foo[P]: ...
reveal_type(test(1, 2)) # N: Revealed type is "__main__.Foo[[Literal[1]?, Literal[2]?]]"
reveal_type(test(x=1, y=2)) # N: Revealed type is "__main__.Foo[[x: Literal[1]?, y: Literal[2]?]]"
ints = [1, 2, 3]
reveal_type(test(*ints)) # N: Revealed type is "__main__.Foo[[*builtins.int]]"
[builtins fixtures/paramspec.pyi]
[case testParamSpecArgumentParamInferenceGeneric]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec("P")
R = TypeVar("R")
def call(f: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
return f(*args, **kwargs)
T = TypeVar("T")
def identity(x: T) -> T:
return x
reveal_type(call(identity, 2)) # N: Revealed type is "builtins.int"
y: int = call(identity, 2)
[builtins fixtures/paramspec.pyi]
[case testParamSpecNestedApplyNoCrash]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec("P")
T = TypeVar("T")
def apply(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: ...
def test() -> int: ...
reveal_type(apply(apply, test)) # N: Revealed type is "builtins.int"
[builtins fixtures/paramspec.pyi]
[case testParamSpecNestedApplyPosVsNamed]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec("P")
T = TypeVar("T")
def apply(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> None: ...
def test(x: int) -> int: ...
apply(apply, test, x=42) # OK
apply(apply, test, 42) # Also OK (but requires some special casing)
apply(apply, test, "bad") # E: Argument 1 to "apply" has incompatible type "Callable[[Callable[P, T], **P], None]"; expected "Callable[[Callable[[int], int], str], None]"
def test2(x: int, y: str) -> None: ...
apply(apply, test2, 42, "yes")
apply(apply, test2, "no", 42) # E: Argument 1 to "apply" has incompatible type "Callable[[Callable[P, T], **P], None]"; expected "Callable[[Callable[[int, str], None], str, int], None]"
apply(apply, test2, x=42, y="yes")
apply(apply, test2, y="yes", x=42)
apply(apply, test2, y=42, x="no") # E: Argument 1 to "apply" has incompatible type "Callable[[Callable[P, T], **P], None]"; expected "Callable[[Callable[[int, str], None], int, str], None]"
[builtins fixtures/paramspec.pyi]
[case testParamSpecApplyPosVsNamedOptional]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec("P")
T = TypeVar("T")
def apply(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> None: ...
def test(x: str = ..., y: int = ...) -> int: ...
apply(test, y=42) # OK
[builtins fixtures/paramspec.pyi]
[case testParamSpecPrefixSubtypingGenericInvalid]
from typing import Generic
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec("P")
class A(Generic[P]):
def foo(self, *args: P.args, **kwargs: P.kwargs):
...
def bar(b: A[P]) -> A[Concatenate[int, P]]:
return b # E: Incompatible return value type (got "A[P]", expected "A[[int, **P]]")
[builtins fixtures/paramspec.pyi]
[case testParamSpecPrefixSubtypingProtocolInvalid]
from typing import Protocol
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec("P")
class A(Protocol[P]):
def foo(self, *args: P.args, **kwargs: P.kwargs):
...
def bar(b: A[P]) -> A[Concatenate[int, P]]:
return b # E: Incompatible return value type (got "A[P]", expected "A[[int, **P]]") \
# N: Following member(s) of "A[P]" have conflicts: \
# N: Expected: \
# N: def foo(self, int, /, *args: P.args, **kwargs: P.kwargs) -> Any \
# N: Got: \
# N: def foo(self, *args: P.args, **kwargs: P.kwargs) -> Any
[builtins fixtures/paramspec.pyi]
[case testParamSpecPrefixSubtypingValidNonStrict]
from typing import Protocol
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec("P")
class A(Protocol[P]):
def foo(self, a: int, *args: P.args, **kwargs: P.kwargs):
...
class B(Protocol[P]):
def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs):
...
def bar(b: B[P]) -> A[Concatenate[int, P]]:
return b
[builtins fixtures/paramspec.pyi]
[case testParamSpecPrefixSubtypingInvalidStrict]
# flags: --extra-checks
from typing import Protocol
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec("P")
class A(Protocol[P]):
def foo(self, a: int, *args: P.args, **kwargs: P.kwargs):
...
class B(Protocol[P]):
def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs):
...
def bar(b: B[P]) -> A[Concatenate[int, P]]:
return b # E: Incompatible return value type (got "B[P]", expected "A[[int, **P]]") \
# N: Following member(s) of "B[P]" have conflicts: \
# N: Expected: \
# N: def foo(self, a: int, int, /, *args: P.args, **kwargs: P.kwargs) -> Any \
# N: Got: \
# N: def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs) -> Any
[builtins fixtures/paramspec.pyi]
[case testParamSpecDecoratorOverload]
from typing import Callable, overload, TypeVar, List
from typing_extensions import ParamSpec
P = ParamSpec("P")
T = TypeVar("T")
def transform(func: Callable[P, List[T]]) -> Callable[P, T]: ...
@overload
def foo(x: int) -> List[float]: ...
@overload
def foo(x: str) -> List[str]: ...
def foo(x): ...
reveal_type(transform(foo)) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.float, def (x: builtins.str) -> builtins.str)"
@transform
@overload
def bar(x: int) -> List[float]: ...
@transform
@overload
def bar(x: str) -> List[str]: ...
@transform
def bar(x): ...
reveal_type(bar) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.float, def (x: builtins.str) -> builtins.str)"
[builtins fixtures/paramspec.pyi]
[case testParamSpecDecoratorOverloadNoCrashOnInvalidTypeVar]
from typing import Any, Callable, List
from typing_extensions import ParamSpec
P = ParamSpec("P")
T = 1
Alias = Callable[P, List[T]] # type: ignore
def dec(fn: Callable[P, T]) -> Alias[P, T]: ... # type: ignore
f: Any
dec(f) # No crash
[builtins fixtures/paramspec.pyi]
[case testParamSpecErrorNestedParams]
from typing import Generic
from typing_extensions import ParamSpec
P = ParamSpec("P")
class C(Generic[P]): ...
c: C[int, [int, str], str] # E: Nested parameter specifications are not allowed
reveal_type(c) # N: Revealed type is "__main__.C[Any]"
[builtins fixtures/paramspec.pyi]
[case testParamSpecInheritNoCrashOnNested]
from typing import Generic
from typing_extensions import ParamSpec
P = ParamSpec("P")
class C(Generic[P]): ...
class D(C[int, [int, str], str]): ... # E: Nested parameter specifications are not allowed
[builtins fixtures/paramspec.pyi]
[case testParamSpecConcatenateSelfType]
from typing import Callable
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec("P")
class A:
def __init__(self, a_param_1: str) -> None: ...
@classmethod
def add_params(cls: Callable[P, A]) -> Callable[Concatenate[float, P], A]:
def new_constructor(i: float, *args: P.args, **kwargs: P.kwargs) -> A:
return cls(*args, **kwargs)
return new_constructor
@classmethod
def remove_params(cls: Callable[Concatenate[str, P], A]) -> Callable[P, A]:
def new_constructor(*args: P.args, **kwargs: P.kwargs) -> A:
return cls("my_special_str", *args, **kwargs)
return new_constructor
reveal_type(A.add_params()) # N: Revealed type is "def (builtins.float, a_param_1: builtins.str) -> __main__.A"
reveal_type(A.remove_params()) # N: Revealed type is "def () -> __main__.A"
[builtins fixtures/paramspec.pyi]
[case testParamSpecConcatenateCallbackProtocol]
from typing import Protocol, TypeVar
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec("P")
R = TypeVar("R", covariant=True)
class Path: ...
class Function(Protocol[P, R]):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
def file_cache(fn: Function[Concatenate[Path, P], R]) -> Function[P, R]:
def wrapper(*args: P.args, **kw: P.kwargs) -> R:
return fn(Path(), *args, **kw)
return wrapper
@file_cache
def get_thing(path: Path, *, some_arg: int) -> int: ...
reveal_type(get_thing) # N: Revealed type is "__main__.Function[[*, some_arg: builtins.int], builtins.int]"
get_thing(some_arg=1) # OK
[builtins fixtures/paramspec.pyi]
[case testParamSpecConcatenateKeywordOnly]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec("P")
R = TypeVar("R")
class Path: ...
def file_cache(fn: Callable[Concatenate[Path, P], R]) -> Callable[P, R]:
def wrapper(*args: P.args, **kw: P.kwargs) -> R:
return fn(Path(), *args, **kw)
return wrapper
@file_cache
def get_thing(path: Path, *, some_arg: int) -> int: ...
reveal_type(get_thing) # N: Revealed type is "def (*, some_arg: builtins.int) -> builtins.int"
get_thing(some_arg=1) # OK
[builtins fixtures/paramspec.pyi]
[case testParamSpecConcatenateCallbackApply]
from typing import Callable, Protocol
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec("P")
class FuncType(Protocol[P]):
def __call__(self, x: int, s: str, *args: P.args, **kw_args: P.kwargs) -> str: ...
def forwarder1(fp: FuncType[P], *args: P.args, **kw_args: P.kwargs) -> str:
return fp(0, '', *args, **kw_args)
def forwarder2(fp: Callable[Concatenate[int, str, P], str], *args: P.args, **kw_args: P.kwargs) -> str:
return fp(0, '', *args, **kw_args)
def my_f(x: int, s: str, d: bool) -> str: ...
forwarder1(my_f, True) # OK
forwarder2(my_f, True) # OK
forwarder1(my_f, 1.0) # E: Argument 2 to "forwarder1" has incompatible type "float"; expected "bool"
forwarder2(my_f, 1.0) # E: Argument 2 to "forwarder2" has incompatible type "float"; expected "bool"
[builtins fixtures/paramspec.pyi]
[case testParamSpecCallbackProtocolSelf]
from typing import Callable, Protocol, TypeVar
from typing_extensions import ParamSpec, Concatenate
Params = ParamSpec("Params")
Result = TypeVar("Result", covariant=True)
class FancyMethod(Protocol):
def __call__(self, arg1: int, arg2: str) -> bool: ...
def return_me(self: Callable[Params, Result]) -> Callable[Params, Result]: ...
def return_part(self: Callable[Concatenate[int, Params], Result]) -> Callable[Params, Result]: ...
m: FancyMethod
reveal_type(m.return_me()) # N: Revealed type is "def (arg1: builtins.int, arg2: builtins.str) -> builtins.bool"
reveal_type(m.return_part()) # N: Revealed type is "def (arg2: builtins.str) -> builtins.bool"
[builtins fixtures/paramspec.pyi]
[case testParamSpecInferenceCallableAgainstAny]
from typing import Callable, TypeVar, Any
from typing_extensions import ParamSpec, Concatenate
_P = ParamSpec("_P")
_R = TypeVar("_R")
class A: ...
a = A()
def a_func(
func: Callable[Concatenate[A, _P], _R],
) -> Callable[Concatenate[Any, _P], _R]:
def wrapper(__a: Any, *args: _P.args, **kwargs: _P.kwargs) -> _R:
return func(a, *args, **kwargs)
return wrapper
def test(a, *args): ...
x: Any
y: object
a_func(test)
x = a_func(test)
y = a_func(test)
[builtins fixtures/paramspec.pyi]
[case testParamSpecInferenceWithCallbackProtocol]
from typing import Protocol, Callable, ParamSpec
class CB(Protocol):
def __call__(self, x: str, y: int) -> None: ...
P = ParamSpec('P')
def g(fn: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
cb: CB
g(cb, y=0, x='a') # OK
g(cb, y='a', x=0) # E: Argument "y" to "g" has incompatible type "str"; expected "int" \
# E: Argument "x" to "g" has incompatible type "int"; expected "str"
[builtins fixtures/paramspec.pyi]
[case testParamSpecBadRuntimeTypeApplication]
from typing import ParamSpec, TypeVar, Generic, Callable
R = TypeVar("R")
P = ParamSpec("P")
class C(Generic[P, R]):
x: Callable[P, R]
bad = C[int, str]() # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
reveal_type(bad) # N: Revealed type is "__main__.C[Any, Any]"
reveal_type(bad.x) # N: Revealed type is "def (*Any, **Any) -> Any"
[builtins fixtures/paramspec.pyi]
[case testParamSpecNoCrashOnUnificationAlias]
import mod
[file mod.pyi]
from typing import Callable, Protocol, TypeVar, overload
from typing_extensions import ParamSpec
P = ParamSpec("P")
R_co = TypeVar("R_co", covariant=True)
Handler = Callable[P, R_co]
class HandlerDecorator(Protocol):
def __call__(self, handler: Handler[P, R_co]) -> Handler[P, R_co]: ...
@overload
def event(event_handler: Handler[P, R_co]) -> Handler[P, R_co]: ...
@overload
def event(namespace: str, *args, **kwargs) -> HandlerDecorator: ...
[builtins fixtures/paramspec.pyi]
[case testParamSpecNoCrashOnUnificationCallable]
import mod
[file mod.pyi]
from typing import Callable, Protocol, TypeVar, overload
from typing_extensions import ParamSpec
P = ParamSpec("P")
R_co = TypeVar("R_co", covariant=True)
class HandlerDecorator(Protocol):
def __call__(self, handler: Callable[P, R_co]) -> Callable[P, R_co]: ...
@overload
def event(event_handler: Callable[P, R_co]) -> Callable[P, R_co]: ...
@overload
def event(namespace: str, *args, **kwargs) -> HandlerDecorator: ...
[builtins fixtures/paramspec.pyi]
[case testParamSpecNoCrashOnUnificationPrefix]
from typing import Any, Callable, TypeVar, overload
from typing_extensions import ParamSpec, Concatenate
T = TypeVar("T")
U = TypeVar("U")
V = TypeVar("V")
W = TypeVar("W")
P = ParamSpec("P")
@overload
def call(
func: Callable[Concatenate[T, P], U],
x: T,
*args: Any,
**kwargs: Any,
) -> U: ...
@overload
def call(
func: Callable[Concatenate[T, U, P], V],
x: T,
y: U,
*args: Any,
**kwargs: Any,
) -> V: ...
def call(*args: Any, **kwargs: Any) -> Any: ...
def test1(x: int) -> str: ...
def test2(x: int, y: int) -> str: ...
reveal_type(call(test1, 1)) # N: Revealed type is "builtins.str"
reveal_type(call(test2, 1, 2)) # N: Revealed type is "builtins.str"
[builtins fixtures/paramspec.pyi]
[case testParamSpecCorrectParameterNameInference]
from typing import Callable, Protocol
from typing_extensions import ParamSpec, Concatenate
def a(i: int) -> None: ...
def b(__i: int) -> None: ...
class WithName(Protocol):
def __call__(self, i: int) -> None: ...
NoName = Callable[[int], None]
def f1(__fn: WithName, i: int) -> None: ...
def f2(__fn: NoName, i: int) -> None: ...
P = ParamSpec("P")
def d(f: Callable[P, None], fn: Callable[Concatenate[Callable[P, None], P], None]) -> Callable[P, None]:
def inner(*args: P.args, **kwargs: P.kwargs) -> None:
fn(f, *args, **kwargs)
return inner
reveal_type(d(a, f1)) # N: Revealed type is "def (i: builtins.int)"
reveal_type(d(a, f2)) # N: Revealed type is "def (i: builtins.int)"
reveal_type(d(b, f1)) # E: Cannot infer value of type parameter "P" of "d" \
# N: Revealed type is "def (*Any, **Any)"
reveal_type(d(b, f2)) # N: Revealed type is "def (builtins.int)"
[builtins fixtures/paramspec.pyi]
[case testParamSpecGenericWithNamedArg1]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
R = TypeVar("R")
P = ParamSpec("P")
def run(func: Callable[[], R], *args: object, backend: str = "asyncio") -> R: ...
class Result: ...
def run_portal() -> Result: ...
def submit(func: Callable[P, R], /, *args: P.args, **kwargs: P.kwargs) -> R: ...
reveal_type(submit( # N: Revealed type is "__main__.Result"
run,
run_portal,
backend="asyncio",
))
submit(
run, # E: Argument 1 to "submit" has incompatible type "def [R] run(func: Callable[[], R], *args: object, backend: str = ...) -> R"; expected "Callable[[Callable[[], Result], int], Result]"
run_portal,
backend=int(),
)
[builtins fixtures/paramspec.pyi]
[case testInferenceAgainstGenericCallableUnionParamSpec]
from typing import Callable, TypeVar, List, Union
from typing_extensions import ParamSpec
T = TypeVar("T")
P = ParamSpec("P")
def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ...
@dec
def func(arg: T) -> Union[T, str]:
...
reveal_type(func) # N: Revealed type is "def [T] (arg: T`-1) -> builtins.list[Union[T`-1, builtins.str]]"
reveal_type(func(42)) # N: Revealed type is "builtins.list[Union[builtins.int, builtins.str]]"
def dec2(f: Callable[P, List[T]]) -> Callable[P, T]: ...
@dec2
def func2(arg: T) -> List[Union[T, str]]:
...
reveal_type(func2) # N: Revealed type is "def [T] (arg: T`-1) -> Union[T`-1, builtins.str]"
reveal_type(func2(42)) # N: Revealed type is "Union[builtins.int, builtins.str]"
[builtins fixtures/paramspec.pyi]
[case testParamSpecPreciseKindsUsedIfPossible]
from typing import Callable, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
class Case(Generic[P]):
def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
pass
def _test(a: int, b: int = 0) -> None: ...
def parametrize(
func: Callable[P, None], *cases: Case[P], **named_cases: Case[P]
) -> Callable[[], None]:
...
parametrize(_test, Case(1, 2), Case(3, 4))
parametrize(_test, Case(1, b=2), Case(3, b=4))
parametrize(_test, Case(1, 2), Case(3))
parametrize(_test, Case(1, 2), Case(3, b=4))
[builtins fixtures/paramspec.pyi]
[case testRunParamSpecInsufficientArgs]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable
_P = ParamSpec("_P")
def run(predicate: Callable[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> None: # N: "run" defined here
predicate() # E: Too few arguments
predicate(*args) # E: Too few arguments
predicate(**kwargs) # E: Too few arguments
predicate(*args, **kwargs)
def fn() -> None: ...
def fn_args(x: int) -> None: ...
def fn_posonly(x: int, /) -> None: ...
run(fn)
run(fn_args, 1)
run(fn_args, x=1)
run(fn_posonly, 1)
run(fn_posonly, x=1) # E: Unexpected keyword argument "x" for "run"
[builtins fixtures/paramspec.pyi]
[case testRunParamSpecConcatenateInsufficientArgs]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable
_P = ParamSpec("_P")
def run(predicate: Callable[Concatenate[int, _P], None], *args: _P.args, **kwargs: _P.kwargs) -> None: # N: "run" defined here
predicate() # E: Too few arguments
predicate(1) # E: Too few arguments
predicate(1, *args) # E: Too few arguments
predicate(1, *args) # E: Too few arguments
predicate(1, **kwargs) # E: Too few arguments
predicate(*args, **kwargs) # E: Argument 1 has incompatible type "*_P.args"; expected "int"
predicate(1, *args, **kwargs)
def fn() -> None: ...
def fn_args(x: int, y: str) -> None: ...
def fn_posonly(x: int, /) -> None: ...
def fn_posonly_args(x: int, /, y: str) -> None: ...
run(fn) # E: Argument 1 to "run" has incompatible type "Callable[[], None]"; expected "Callable[[int], None]"
run(fn_args, 1, 'a') # E: Too many arguments for "run" \
# E: Argument 2 to "run" has incompatible type "int"; expected "str"
run(fn_args, y='a')
run(fn_args, 'a')
run(fn_posonly)
run(fn_posonly, x=1) # E: Unexpected keyword argument "x" for "run"
run(fn_posonly_args) # E: Missing positional argument "y" in call to "run"
run(fn_posonly_args, 'a')
run(fn_posonly_args, y='a')
[builtins fixtures/paramspec.pyi]
[case testRunParamSpecConcatenateInsufficientArgsInDecorator]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable
P = ParamSpec("P")
def decorator(fn: Callable[Concatenate[str, P], None]) -> Callable[P, None]:
def inner(*args: P.args, **kwargs: P.kwargs) -> None:
fn("value") # E: Too few arguments
fn("value", *args) # E: Too few arguments
fn("value", **kwargs) # E: Too few arguments
fn(*args, **kwargs) # E: Argument 1 has incompatible type "*P.args"; expected "str"
fn("value", *args, **kwargs)
return inner
@decorator
def foo(s: str, s2: str) -> None: ...
[builtins fixtures/paramspec.pyi]
[case testRunParamSpecOverload]
from typing_extensions import ParamSpec
from typing import Callable, NoReturn, TypeVar, Union, overload
P = ParamSpec("P")
T = TypeVar("T")
@overload
def capture(
sync_fn: Callable[P, NoReturn],
*args: P.args,
**kwargs: P.kwargs,
) -> int: ...
@overload
def capture(
sync_fn: Callable[P, T],
*args: P.args,
**kwargs: P.kwargs,
) -> Union[T, int]: ...
def capture(
sync_fn: Callable[P, T],
*args: P.args,
**kwargs: P.kwargs,
) -> Union[T, int]:
return sync_fn(*args, **kwargs)
def fn() -> str: return ''
def err() -> NoReturn: ...
reveal_type(capture(fn)) # N: Revealed type is "Union[builtins.str, builtins.int]"
reveal_type(capture(err)) # N: Revealed type is "builtins.int"
[builtins fixtures/paramspec.pyi]
[case testRunParamSpecOverlappingOverloadsOrder]
from typing import Any, Callable, overload
from typing_extensions import ParamSpec
P = ParamSpec("P")
class Base:
pass
class Child(Base):
def __call__(self) -> str: ...
class NotChild:
def __call__(self) -> str: ...
@overload
def handle(func: Base) -> int: ...
@overload
def handle(func: Callable[P, str], *args: P.args, **kwargs: P.kwargs) -> str: ...
def handle(func: Any, *args: Any, **kwargs: Any) -> Any:
return func(*args, **kwargs)
@overload
def handle_reversed(func: Callable[P, str], *args: P.args, **kwargs: P.kwargs) -> str: ...
@overload
def handle_reversed(func: Base) -> int: ...
def handle_reversed(func: Any, *args: Any, **kwargs: Any) -> Any:
return func(*args, **kwargs)
reveal_type(handle(Child())) # N: Revealed type is "builtins.int"
reveal_type(handle(NotChild())) # N: Revealed type is "builtins.str"
reveal_type(handle_reversed(Child())) # N: Revealed type is "builtins.str"
reveal_type(handle_reversed(NotChild())) # N: Revealed type is "builtins.str"
[builtins fixtures/paramspec.pyi]
[case testBindPartial]
from functools import partial
from typing_extensions import ParamSpec
from typing import Callable, TypeVar
P = ParamSpec("P")
T = TypeVar("T")
def run(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, **kwargs)
return func2(*args)
def run2(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, *args)
return func2(**kwargs)
def run3(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, *args, **kwargs)
return func2()
def run4(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, *args, **kwargs)
return func2(**kwargs)
def run_bad(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, *args, **kwargs)
return func2(*args) # E: Too many arguments
def run_bad2(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, **kwargs)
return func2(**kwargs) # E: Too few arguments
def run_bad3(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, *args)
return func2() # E: Too few arguments
[builtins fixtures/paramspec.pyi]
[case testBindPartialConcatenate]
from functools import partial
from typing_extensions import Concatenate, ParamSpec
from typing import Callable, TypeVar
P = ParamSpec("P")
T = TypeVar("T")
def run(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, 1, **kwargs)
return func2(*args)
def run2(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, **kwargs)
p = [""]
func2(1, *p) # E: Too few arguments \
# E: Argument 2 has incompatible type "*list[str]"; expected "P.args"
func2(1, 2, *p) # E: Too few arguments \
# E: Argument 2 has incompatible type "int"; expected "P.args" \
# E: Argument 3 has incompatible type "*list[str]"; expected "P.args"
func2(1, *args, *p) # E: Argument 3 has incompatible type "*list[str]"; expected "P.args"
func2(1, *p, *args) # E: Argument 2 has incompatible type "*list[str]"; expected "P.args"
return func2(1, *args)
def run3(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, 1, *args)
d = {"":""}
func2(**d) # E: Too few arguments \
# E: Argument 1 has incompatible type "**dict[str, str]"; expected "P.kwargs"
return func2(**kwargs)
def run4(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, 1)
return func2(*args, **kwargs)
def run5(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, 1, *args, **kwargs)
func2()
return func2(**kwargs)
def run_bad(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, *args) # E: Argument 1 has incompatible type "*P.args"; expected "int"
return func2(1, **kwargs) # E: Argument 1 has incompatible type "int"; expected "P.args"
def run_bad2(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, 1, *args)
func2() # E: Too few arguments
func2(*args, **kwargs) # E: Too many arguments
return func2(1, **kwargs) # E: Argument 1 has incompatible type "int"; expected "P.args"
def run_bad3(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, 1, **kwargs)
func2() # E: Too few arguments
return func2(1, *args) # E: Argument 1 has incompatible type "int"; expected "P.args"
def run_bad4(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, 1)
func2() # E: Too few arguments
func2(*args) # E: Too few arguments
func2(1, *args) # E: Too few arguments \
# E: Argument 1 has incompatible type "int"; expected "P.args"
func2(1, **kwargs) # E: Too few arguments \
# E: Argument 1 has incompatible type "int"; expected "P.args"
return func2(**kwargs) # E: Too few arguments
[builtins fixtures/paramspec.pyi]
[case testOtherVarArgs]
from functools import partial
from typing_extensions import Concatenate, ParamSpec
from typing import Callable, TypeVar, Tuple
P = ParamSpec("P")
T = TypeVar("T")
def run(func: Callable[Concatenate[int, str, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
func2 = partial(func, **kwargs)
args_prefix: Tuple[int, str] = (1, 'a')
func2(*args_prefix) # E: Too few arguments
func2(*args, *args_prefix) # E: Argument 1 has incompatible type "*P.args"; expected "int" \
# E: Argument 1 has incompatible type "*P.args"; expected "str" \
# E: Argument 2 has incompatible type "*tuple[int, str]"; expected "P.args"
return func2(*args_prefix, *args)
[builtins fixtures/paramspec.pyi]
[case testParamSpecScoping]
from typing import Any, Callable, Generic
from typing_extensions import Concatenate, ParamSpec
P = ParamSpec("P")
P2 = ParamSpec("P2")
def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def contains_other(f: Callable[P2, None], c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def contains_only_other(c: Callable[P2, None], *args: P.args, **kwargs: P.kwargs) -> None: ... # E: ParamSpec "P" is unbound
def puts_p_into_scope(f: Callable[P, int]) -> None:
def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
def puts_p_into_scope_concatenate(f: Callable[Concatenate[int, P], int]) -> None:
def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
def wrapper() -> None:
def puts_p_into_scope1(f: Callable[P, int]) -> None:
def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
class Wrapper:
def puts_p_into_scope1(self, f: Callable[P, int]) -> None:
def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
def contains(self, c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def uses(self, *args: P.args, **kwargs: P.kwargs) -> None: ... # E: ParamSpec "P" is unbound
def method(self) -> None:
def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ... # E: ParamSpec "P" is unbound
class GenericWrapper(Generic[P]):
x: P.args # E: ParamSpec components are not allowed here
y: P.kwargs # E: ParamSpec components are not allowed here
def contains(self, c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def puts_p_into_scope1(self, f: Callable[P, int]) -> None:
def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
def uses(self, *args: P.args, **kwargs: P.kwargs) -> None: ...
def method(self) -> None:
def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
[builtins fixtures/paramspec.pyi]
[case testCallbackProtocolClassObjectParamSpec]
from typing import Any, Callable, Protocol, Optional, Generic
from typing_extensions import ParamSpec
P = ParamSpec("P")
class App: ...
class MiddlewareFactory(Protocol[P]):
def __call__(self, app: App, /, *args: P.args, **kwargs: P.kwargs) -> App:
...
class Capture(Generic[P]): ...
class ServerErrorMiddleware(App):
def __init__(
self,
app: App,
handler: Optional[str] = None,
debug: bool = False,
) -> None: ...
def fn(f: MiddlewareFactory[P]) -> Capture[P]: ...
reveal_type(fn(ServerErrorMiddleware)) # N: Revealed type is "__main__.Capture[[handler: Union[builtins.str, None] =, debug: builtins.bool =]]"
[builtins fixtures/paramspec.pyi]
[case testRunParamSpecDuplicateArgsKwargs]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, Union
_P = ParamSpec("_P")
def run(predicate: Callable[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> None:
predicate(*args, *args, **kwargs) # E: ParamSpec.args should only be passed once
predicate(*args, **kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once
predicate(*args, *args, **kwargs, **kwargs) # E: ParamSpec.args should only be passed once \
# E: ParamSpec.kwargs should only be passed once
copy_args = args
copy_kwargs = kwargs
predicate(*args, *copy_args, **kwargs) # E: ParamSpec.args should only be passed once
predicate(*copy_args, *args, **kwargs) # E: ParamSpec.args should only be passed once
predicate(*args, **copy_kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once
predicate(*args, **kwargs, **copy_kwargs) # E: ParamSpec.kwargs should only be passed once
def run2(predicate: Callable[Concatenate[int, _P], None], *args: _P.args, **kwargs: _P.kwargs) -> None:
predicate(*args, *args, **kwargs) # E: ParamSpec.args should only be passed once \
# E: Argument 1 has incompatible type "*_P.args"; expected "int"
predicate(*args, **kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once \
# E: Argument 1 has incompatible type "*_P.args"; expected "int"
predicate(1, *args, *args, **kwargs) # E: ParamSpec.args should only be passed once
predicate(1, *args, **kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once
predicate(1, *args, *args, **kwargs, **kwargs) # E: ParamSpec.args should only be passed once \
# E: ParamSpec.kwargs should only be passed once
copy_args = args
copy_kwargs = kwargs
predicate(1, *args, *copy_args, **kwargs) # E: ParamSpec.args should only be passed once
predicate(1, *copy_args, *args, **kwargs) # E: ParamSpec.args should only be passed once
predicate(1, *args, **copy_kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once
predicate(1, *args, **kwargs, **copy_kwargs) # E: ParamSpec.kwargs should only be passed once
def run3(predicate: Callable[Concatenate[int, str, _P], None], *args: _P.args, **kwargs: _P.kwargs) -> None:
base_ok: tuple[int, str]
predicate(*base_ok, *args, **kwargs)
base_bad: tuple[Union[int, str], ...]
predicate(*base_bad, *args, **kwargs) # E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "int" \
# E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "str" \
# E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "_P.args"
[builtins fixtures/paramspec.pyi]
|