Debian Codegraph @

test-data/unit/check-parameter-specification.test

raw
1 [case testBasicParamSpec]
2 from typing_extensions import ParamSpec
3 P = ParamSpec('P')
4 [builtins fixtures/tuple.pyi]
5
6 [case testInvalidParamSpecDefinitions]
7 from typing import ParamSpec
8
9 P1 = ParamSpec("P1", covariant=True) # E: The variance and bound arguments to ParamSpec do not have defined semantics yet
10 P2 = ParamSpec("P2", contravariant=True) # E: The variance and bound arguments to ParamSpec do not have defined semantics yet
11 P3 = ParamSpec("P3", bound=int) # E: The variance and bound arguments to ParamSpec do not have defined semantics yet
12 P4 = ParamSpec("P4", int, str) # E: Too many positional arguments for "ParamSpec"
13 P5 = ParamSpec("P5", covariant=True, bound=int) # E: The variance and bound arguments to ParamSpec do not have defined semantics yet
14 [builtins fixtures/paramspec.pyi]
15
16 [case testParamSpecLocations]
17 from typing import Any, Callable, List, Type
18 from typing_extensions import ParamSpec, Concatenate
19 P = ParamSpec('P')
20
21 x: P # E: ParamSpec "P" is unbound
22
23 def foo1(x: Callable[P, int]) -> Callable[P, str]: ...
24
25 def foo2(x: P) -> P: ... # E: Invalid location for ParamSpec "P" \
26 # N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
27
28 def foo3(x: Concatenate[int, P]) -> int: ... # E: Invalid location for Concatenate \
29 # N: You can use Concatenate as the first argument to Callable
30
31 def foo4(x: List[P]) -> None: ... # E: Invalid location for ParamSpec "P" \
32 # N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
33
34 def foo5(x: Callable[[int, str], P]) -> None: ... # E: Invalid location for ParamSpec "P" \
35 # N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
36
37 def foo6(x: Callable[[P], int]) -> None: ... # E: Invalid location for ParamSpec "P" \
38 # N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
39
40 def foo7(
41 *args: P.args, **kwargs: P.kwargs # E: ParamSpec "P" is unbound
42 ) -> Callable[[Callable[P, T]], Type[T]]:
43 ...
44
45 def wrapper(f: Callable[P, int]) -> None:
46 def inner(*args: P.args, **kwargs: P.kwargs) -> None: ... # OK
47
48 def extra_args_left(x: int, *args: P.args, **kwargs: P.kwargs) -> None: ... # OK
49 def extra_args_between(*args: P.args, x: int, **kwargs: P.kwargs) -> None: ... # E: Arguments not allowed after ParamSpec.args
50
51 def swapped(*args: P.kwargs, **kwargs: P.args) -> None: ... # E: Use "P.args" for variadic "*" parameter \
52 # E: Use "P.kwargs" for variadic "**" parameter
53 def bad_kwargs(*args: P.args, **kwargs: P.args) -> None: ... # E: Use "P.kwargs" for variadic "**" parameter
54 def bad_args(*args: P.kwargs, **kwargs: P.kwargs) -> None: ... # E: Use "P.args" for variadic "*" parameter
55
56 def misplaced(x: P.args) -> None: ... # E: ParamSpec components are not allowed here
57 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"
58 [builtins fixtures/paramspec.pyi]
59
60 [case testParamSpecImports]
61 import lib
62 from lib import Base
63
64 class C(Base[[int]]):
65 def test(self, x: int): ...
66
67 class D(lib.Base[[int]]):
68 def test(self, x: int): ...
69
70 class E(lib.Base[...]): ...
71 reveal_type(E().test) # N: Revealed type is "def (*Any, **Any)"
72
73 [file lib.py]
74 from typing import Generic
75 from typing_extensions import ParamSpec
76
77 P = ParamSpec("P")
78 class Base(Generic[P]):
79 def test(self, *args: P.args, **kwargs: P.kwargs) -> None:
80 ...
81 [builtins fixtures/paramspec.pyi]
82
83 [case testParamSpecEllipsisInAliases]
84 from typing import Any, Callable, Generic, TypeVar
85 from typing_extensions import ParamSpec
86
87 P = ParamSpec('P')
88 R = TypeVar('R')
89 Alias = Callable[P, R]
90
91 class B(Generic[P]): ...
92 Other = B[P]
93
94 T = TypeVar('T', bound=Alias[..., Any])
95 Alias[..., Any] # E: Type application is only supported for generic classes
96 B[...]
97 Other[...]
98 [builtins fixtures/paramspec.pyi]
99
100 [case testParamSpecEllipsisInConcatenate]
101 from typing import Any, Callable, Generic, TypeVar
102 from typing_extensions import ParamSpec, Concatenate
103
104 P = ParamSpec('P')
105 R = TypeVar('R')
106 Alias = Callable[P, R]
107
108 IntFun = Callable[Concatenate[int, ...], None]
109 f: IntFun
110 reveal_type(f) # N: Revealed type is "def (builtins.int, *Any, **Any)"
111
112 g: Callable[Concatenate[int, ...], None]
113 reveal_type(g) # N: Revealed type is "def (builtins.int, *Any, **Any)"
114
115 class B(Generic[P]):
116 def test(self, *args: P.args, **kwargs: P.kwargs) -> None:
117 ...
118
119 x: B[Concatenate[int, ...]]
120 reveal_type(x.test) # N: Revealed type is "def (builtins.int, *Any, **Any)"
121
122 Bad = Callable[Concatenate[int, [int, str]], None] # E: The last parameter to Concatenate needs to be a ParamSpec \
123 # E: Bracketed expression "[...]" is not valid as a type
124 def bad(fn: Callable[Concatenate[P, int], None]): # E: The last parameter to Concatenate needs to be a ParamSpec
125 ...
126 [builtins fixtures/paramspec.pyi]
127
128 [case testParamSpecContextManagerLike]
129 from typing import Callable, List, Iterator, TypeVar
130 from typing_extensions import ParamSpec
131 P = ParamSpec('P')
132 T = TypeVar('T')
133
134 def tmpcontextmanagerlike(x: Callable[P, Iterator[T]]) -> Callable[P, List[T]]: ...
135
136 @tmpcontextmanagerlike
137 def whatever(x: int) -> Iterator[int]:
138 yield x
139
140 reveal_type(whatever) # N: Revealed type is "def (x: builtins.int) -> builtins.list[builtins.int]"
141 reveal_type(whatever(217)) # N: Revealed type is "builtins.list[builtins.int]"
142 [builtins fixtures/paramspec.pyi]
143
144 [case testInvalidParamSpecType]
145 from typing import ParamSpec
146
147 P = ParamSpec("P")
148
149 class MyFunction(P): # E: Invalid base class "P"
150 ...
151
152 [case testParamSpecRevealType]
153 from typing import Callable
154 from typing_extensions import ParamSpec
155
156 P = ParamSpec('P')
157
158 def f(x: Callable[P, int]) -> None: ...
159 reveal_type(f) # N: Revealed type is "def [P] (x: def (*P.args, **P.kwargs) -> builtins.int)"
160 [builtins fixtures/paramspec.pyi]
161
162 [case testParamSpecSimpleFunction]
163 from typing import Callable, TypeVar
164 from typing_extensions import ParamSpec
165
166 P = ParamSpec('P')
167
168 def changes_return_type_to_str(x: Callable[P, int]) -> Callable[P, str]: ...
169
170 def returns_int(a: str, b: bool) -> int: ...
171
172 reveal_type(changes_return_type_to_str(returns_int)) # N: Revealed type is "def (a: builtins.str, b: builtins.bool) -> builtins.str"
173 [builtins fixtures/paramspec.pyi]
174
175 [case testParamSpecSimpleClass]
176 from typing import Callable, TypeVar, Generic
177 from typing_extensions import ParamSpec
178
179 P = ParamSpec('P')
180
181 class C(Generic[P]):
182 def __init__(self, x: Callable[P, None]) -> None: ...
183
184 def m(self, *args: P.args, **kwargs: P.kwargs) -> int:
185 return 1
186
187 def f(x: int, y: str) -> None: ...
188
189 reveal_type(C(f)) # N: Revealed type is "__main__.C[[x: builtins.int, y: builtins.str]]"
190 reveal_type(C(f).m) # N: Revealed type is "def (x: builtins.int, y: builtins.str) -> builtins.int"
191 [builtins fixtures/dict.pyi]
192
193 [case testParamSpecClassWithPrefixArgument]
194 from typing import Callable, TypeVar, Generic
195 from typing_extensions import ParamSpec
196
197 P = ParamSpec('P')
198
199 class C(Generic[P]):
200 def __init__(self, x: Callable[P, None]) -> None: ...
201
202 def m(self, a: str, *args: P.args, **kwargs: P.kwargs) -> int:
203 return 1
204
205 def f(x: int, y: str) -> None: ...
206
207 reveal_type(C(f).m) # N: Revealed type is "def (a: builtins.str, x: builtins.int, y: builtins.str) -> builtins.int"
208 reveal_type(C(f).m('', 1, '')) # N: Revealed type is "builtins.int"
209 [builtins fixtures/dict.pyi]
210
211 [case testParamSpecDecorator]
212 from typing import Callable, TypeVar, Generic
213 from typing_extensions import ParamSpec
214
215 P = ParamSpec('P')
216 R = TypeVar('R')
217
218 class W(Generic[P, R]):
219 f: Callable[P, R]
220 x: int
221 def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
222 reveal_type(self.f(*args, **kwargs)) # N: Revealed type is "R`2"
223 return self.f(*args, **kwargs)
224
225 def dec() -> Callable[[Callable[P, R]], W[P, R]]:
226 pass
227
228 @dec()
229 def f(a: int, b: str) -> None: ...
230
231 reveal_type(f) # N: Revealed type is "__main__.W[[a: builtins.int, b: builtins.str], None]"
232 reveal_type(f(1, '')) # N: Revealed type is "None"
233 reveal_type(f.x) # N: Revealed type is "builtins.int"
234
235 ## TODO: How should this work?
236 #
237 # class C:
238 # @dec()
239 # def m(self, x: int) -> str: ...
240 #
241 # reveal_type(C().m(x=1))
242 [builtins fixtures/dict.pyi]
243
244 [case testParamSpecFunction]
245 from typing import Callable, TypeVar
246 from typing_extensions import ParamSpec
247
248 P = ParamSpec('P')
249 R = TypeVar('R')
250
251 def f(x: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
252 return x(*args, **kwargs)
253
254 def g(x: int, y: str) -> None: ...
255
256 reveal_type(f(g, 1, y='x')) # N: Revealed type is "None"
257 f(g, 'x', y='x') # E: Argument 2 to "f" has incompatible type "str"; expected "int"
258 f(g, 1, y=1) # E: Argument "y" to "f" has incompatible type "int"; expected "str"
259 f(g) # E: Missing positional arguments "x", "y" in call to "f"
260 [builtins fixtures/dict.pyi]
261
262 [case testParamSpecSpecialCase]
263 from typing import Callable, TypeVar
264 from typing_extensions import ParamSpec
265
266 P = ParamSpec('P')
267 T = TypeVar('T')
268
269 def register(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> Callable[P, T]: ...
270
271 def f(x: int, y: str, z: int, a: str) -> None: ...
272
273 x = register(f, 1, '', 1, '')
274 [builtins fixtures/dict.pyi]
275
276 [case testParamSpecInferredFromAny]
277 from typing import Callable, Any
278 from typing_extensions import ParamSpec
279
280 P = ParamSpec('P')
281
282 def f(x: Callable[P, int]) -> Callable[P, str]: ...
283
284 g: Any
285 reveal_type(f(g)) # N: Revealed type is "def (*Any, **Any) -> builtins.str"
286
287 f(g)(1, 3, x=1, y=2)
288 [builtins fixtures/paramspec.pyi]
289
290 [case testParamSpecDecoratorImplementation]
291 from typing import Callable, Any, TypeVar, List
292 from typing_extensions import ParamSpec
293
294 P = ParamSpec('P')
295 T = TypeVar('T')
296
297 def dec(f: Callable[P, T]) -> Callable[P, List[T]]:
298 def wrapper(*args: P.args, **kwargs: P.kwargs) -> List[T]:
299 return [f(*args, **kwargs)]
300 return wrapper
301
302 @dec
303 def g(x: int, y: str = '') -> int: ...
304
305 reveal_type(g) # N: Revealed type is "def (x: builtins.int, y: builtins.str =) -> builtins.list[builtins.int]"
306 [builtins fixtures/dict.pyi]
307
308 [case testParamSpecArgsAndKwargsTypes]
309 from typing import Callable, TypeVar, Generic
310 from typing_extensions import ParamSpec
311
312 P = ParamSpec('P')
313
314 class C(Generic[P]):
315 def __init__(self, x: Callable[P, None]) -> None: ...
316
317 def m(self, *args: P.args, **kwargs: P.kwargs) -> None:
318 reveal_type(args) # N: Revealed type is "P.args`1"
319 reveal_type(kwargs) # N: Revealed type is "P.kwargs`1"
320 [builtins fixtures/dict.pyi]
321
322 [case testParamSpecSubtypeChecking1]
323 from typing import Callable, TypeVar, Generic, Any
324 from typing_extensions import ParamSpec
325
326 P = ParamSpec('P')
327
328 class C(Generic[P]):
329 def __init__(self, x: Callable[P, None]) -> None: ...
330
331 def m(self, *args: P.args, **kwargs: P.kwargs) -> None:
332 args = args
333 kwargs = kwargs
334 o: object
335 o = args
336 o = kwargs
337 o2: object
338 args = o2 # E: Incompatible types in assignment (expression has type "object", variable has type "P.args")
339 kwargs = o2 # E: Incompatible types in assignment (expression has type "object", variable has type "P.kwargs")
340 a: Any
341 a = args
342 a = kwargs
343 args = kwargs # E: Incompatible types in assignment (expression has type "P.kwargs", variable has type "P.args")
344 kwargs = args # E: Incompatible types in assignment (expression has type "P.args", variable has type "P.kwargs")
345 a1: Any
346 args = a1
347 kwargs = a1
348 [builtins fixtures/dict.pyi]
349
350 [case testParamSpecSubtypeChecking2]
351 from typing import Callable, Generic
352 from typing_extensions import ParamSpec
353
354 P = ParamSpec('P')
355 P2 = ParamSpec('P2')
356
357 class C(Generic[P]):
358 pass
359
360 def f(c1: C[P], c2: C[P2]) -> None:
361 c1 = c1
362 c2 = c2
363 c1 = c2 # E: Incompatible types in assignment (expression has type "C[P2]", variable has type "C[P]")
364 c2 = c1 # E: Incompatible types in assignment (expression has type "C[P]", variable has type "C[P2]")
365
366 def g(f: Callable[P, None], g: Callable[P2, None]) -> None:
367 f = f
368 g = g
369 f = g # E: Incompatible types in assignment (expression has type "Callable[P2, None]", variable has type "Callable[P, None]")
370 g = f # E: Incompatible types in assignment (expression has type "Callable[P, None]", variable has type "Callable[P2, None]")
371 [builtins fixtures/dict.pyi]
372
373 [case testParamSpecJoin]
374 from typing import Callable, Generic, TypeVar
375 from typing_extensions import ParamSpec
376
377 P = ParamSpec('P')
378 P2 = ParamSpec('P2')
379 P3 = ParamSpec('P3')
380 T = TypeVar('T')
381
382 def join(x: T, y: T) -> T: ...
383
384 class C(Generic[P, P2]):
385 def m(self, f: Callable[P, None], g: Callable[P2, None]) -> None:
386 reveal_type(join(f, f)) # N: Revealed type is "def (*P.args, **P.kwargs)"
387 reveal_type(join(f, g)) # N: Revealed type is "builtins.function"
388
389 def m2(self, *args: P.args, **kwargs: P.kwargs) -> None:
390 reveal_type(join(args, args)) # N: Revealed type is "P.args`1"
391 reveal_type(join(kwargs, kwargs)) # N: Revealed type is "P.kwargs`1"
392 reveal_type(join(args, kwargs)) # N: Revealed type is "builtins.object"
393 def f(*args2: P2.args, **kwargs2: P2.kwargs) -> None:
394 reveal_type(join(args, args2)) # N: Revealed type is "builtins.object"
395 reveal_type(join(kwargs, kwargs2)) # N: Revealed type is "builtins.object"
396
397 def m3(self, c: C[P, P3]) -> None:
398 reveal_type(join(c, c)) # N: Revealed type is "__main__.C[P`1, P3`-1]"
399 reveal_type(join(self, c)) # N: Revealed type is "builtins.object"
400 [builtins fixtures/dict.pyi]
401
402 [case testParamSpecClassWithAny]
403 from typing import Callable, Generic, Any
404 from typing_extensions import ParamSpec
405
406 P = ParamSpec('P')
407
408 class C(Generic[P]):
409 def __init__(self, x: Callable[P, None]) -> None: ...
410
411 def m(self, *args: P.args, **kwargs: P.kwargs) -> int:
412 return 1
413
414 c: C[Any]
415 reveal_type(c) # N: Revealed type is "__main__.C[Any]"
416 reveal_type(c.m) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> builtins.int"
417 c.m(4, 6, y='x')
418 c = c
419
420 def f() -> None: pass
421
422 c2 = C(f)
423 c2 = c
424 c3 = C(f)
425 c = c3
426 [builtins fixtures/dict.pyi]
427
428 [case testParamSpecInferredFromLambda]
429 from typing import Callable, TypeVar
430 from typing_extensions import ParamSpec
431
432 P = ParamSpec('P')
433 T = TypeVar('T')
434
435 # Similar to atexit.register
436 def register(f: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> Callable[P, T]: ...
437
438 def f(x: int) -> None: pass
439 def g(x: int, y: str) -> None: pass
440
441 reveal_type(register(lambda: f(1))) # N: Revealed type is "def ()"
442 reveal_type(register(lambda x: f(x), x=1)) # N: Revealed type is "def (x: Literal[1]?)"
443 register(lambda x: f(x)) # E: Cannot infer type of lambda \
444 # E: Argument 1 to "register" has incompatible type "Callable[[Any], None]"; expected "Callable[[], None]"
445 register(lambda x: f(x), y=1) # E: Argument 1 to "register" has incompatible type "def (x: int) -> None"; expected "def (y: int) -> None"
446 reveal_type(register(lambda x: f(x), 1)) # N: Revealed type is "def (Literal[1]?)"
447 reveal_type(register(lambda x, y: g(x, y), 1, "a")) # N: Revealed type is "def (Literal[1]?, Literal['a']?)"
448 reveal_type(register(lambda x, y: g(x, y), 1, y="a")) # N: Revealed type is "def (Literal[1]?, y: Literal['a']?)"
449 [builtins fixtures/dict.pyi]
450
451 [case testParamSpecInvalidCalls]
452 from typing import Callable, Generic
453 from typing_extensions import ParamSpec
454
455 P = ParamSpec('P')
456 P2 = ParamSpec('P2')
457
458 class C(Generic[P, P2]):
459 def m1(self, *args: P.args, **kwargs: P.kwargs) -> None:
460 self.m1(*args, **kwargs)
461 self.m2(*args, **kwargs) # E: Argument 1 to "m2" of "C" has incompatible type "*P.args"; expected "P2.args" \
462 # E: Argument 2 to "m2" of "C" has incompatible type "**P.kwargs"; expected "P2.kwargs"
463 self.m1(*kwargs, **args) # E: Argument 1 to "m1" of "C" has incompatible type "*P.kwargs"; expected "P.args" \
464 # E: Argument 2 to "m1" of "C" has incompatible type "**P.args"; expected "P.kwargs"
465 self.m3(*args, **kwargs) # E: Argument 1 to "m3" of "C" has incompatible type "*P.args"; expected "int" \
466 # E: Argument 2 to "m3" of "C" has incompatible type "**P.kwargs"; expected "int"
467 self.m4(*args, **kwargs) # E: Argument 1 to "m4" of "C" has incompatible type "*P.args"; expected "int" \
468 # E: Argument 2 to "m4" of "C" has incompatible type "**P.kwargs"; expected "int"
469
470 self.m1(*args, **args) # E: Argument 2 to "m1" of "C" has incompatible type "**P.args"; expected "P.kwargs"
471 self.m1(*kwargs, **kwargs) # E: Argument 1 to "m1" of "C" has incompatible type "*P.kwargs"; expected "P.args"
472
473 def m2(self, *args: P2.args, **kwargs: P2.kwargs) -> None:
474 pass
475
476 def m3(self, *args: int, **kwargs: int) -> None:
477 pass
478
479 def m4(self, x: int) -> None:
480 pass
481 [builtins fixtures/dict.pyi]
482
483 [case testParamSpecOverUnannotatedDecorator]
484 from typing import Callable, Iterator, TypeVar, ContextManager, Any
485 from typing_extensions import ParamSpec
486
487 from nonexistent import deco2 # type: ignore
488
489 T = TypeVar("T")
490 P = ParamSpec("P")
491 T_co = TypeVar("T_co", covariant=True)
492
493 class CM(ContextManager[T_co]):
494 def __call__(self, func: T) -> T: ...
495
496 def deco1(
497 func: Callable[P, Iterator[T]]) -> Callable[P, CM[T]]: ...
498
499 @deco1
500 @deco2
501 def f():
502 pass
503
504 reveal_type(f) # N: Revealed type is "def (*Any, **Any) -> __main__.CM[Any]"
505
506 with f() as x:
507 pass
508 [builtins fixtures/dict.pyi]
509 [typing fixtures/typing-full.pyi]
510
511 [case testParamSpecLiterals]
512 from typing_extensions import ParamSpec, TypeAlias
513 from typing import Generic, TypeVar
514
515 P = ParamSpec("P")
516 T = TypeVar("T")
517
518 class Z(Generic[P]): ...
519
520 # literals can be applied
521 n: Z[[int]]
522
523 nt1 = Z[[int]]
524 nt2: TypeAlias = Z[[int]]
525
526 unt1: nt1
527 unt2: nt2
528
529 # literals actually keep types
530 reveal_type(n) # N: Revealed type is "__main__.Z[[builtins.int]]"
531 reveal_type(unt1) # N: Revealed type is "__main__.Z[[builtins.int]]"
532 reveal_type(unt2) # N: Revealed type is "__main__.Z[[builtins.int]]"
533
534 # passing into a function keeps the type
535 def fT(a: T) -> T: ...
536 def fP(a: Z[P]) -> Z[P]: ...
537
538 reveal_type(fT(n)) # N: Revealed type is "__main__.Z[[builtins.int]]"
539 reveal_type(fP(n)) # N: Revealed type is "__main__.Z[[builtins.int]]"
540
541 # literals can be in function args and return type
542 def k(a: Z[[int]]) -> Z[[str]]: ...
543
544 # functions work
545 reveal_type(k(n)) # N: Revealed type is "__main__.Z[[builtins.str]]"
546
547 # literals can be matched in arguments
548 def kb(a: Z[[bytes]]) -> Z[[str]]: ...
549
550 reveal_type(kb(n)) # N: Revealed type is "__main__.Z[[builtins.str]]" \
551 # E: Argument 1 to "kb" has incompatible type "Z[[int]]"; expected "Z[[bytes]]"
552
553
554 n2: Z[bytes]
555
556 reveal_type(kb(n2)) # N: Revealed type is "__main__.Z[[builtins.str]]"
557 [builtins fixtures/tuple.pyi]
558
559 [case testParamSpecConcatenateFromPep]
560 from typing_extensions import ParamSpec, Concatenate
561 from typing import Callable, TypeVar, Generic
562
563 P = ParamSpec("P")
564 R = TypeVar("R")
565
566 # CASE 1
567 class Request:
568 ...
569
570 def with_request(f: Callable[Concatenate[Request, P], R]) -> Callable[P, R]:
571 def inner(*args: P.args, **kwargs: P.kwargs) -> R:
572 return f(Request(), *args, **kwargs)
573 return inner
574
575 @with_request
576 def takes_int_str(request: Request, x: int, y: str) -> int:
577 # use request
578 return x + 7
579
580 reveal_type(takes_int_str) # N: Revealed type is "def (x: builtins.int, y: builtins.str) -> builtins.int"
581
582 takes_int_str(1, "A") # Accepted
583 takes_int_str("B", 2) # E: Argument 1 to "takes_int_str" has incompatible type "str"; expected "int" \
584 # E: Argument 2 to "takes_int_str" has incompatible type "int"; expected "str"
585
586 # CASE 2
587 T = TypeVar("T")
588 P_2 = ParamSpec("P_2")
589
590 class X(Generic[T, P]):
591 f: Callable[P, int]
592 x: T
593
594 def f1(x: X[int, P_2]) -> str: ... # Accepted
595 def f2(x: X[int, Concatenate[int, P_2]]) -> str: ... # Accepted
596 def f3(x: X[int, [int, bool]]) -> str: ... # Accepted
597 # ellipsis only show up here, but I can assume it works like Callable[..., R]
598 def f4(x: X[int, ...]) -> str: ... # Accepted
599 def f5(x: X[int, int]) -> str: ... # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
600
601 # CASE 3
602 def bar(x: int, *args: bool) -> int: ...
603 def add(x: Callable[P, int]) -> Callable[Concatenate[str, P], bool]: ...
604
605 reveal_type(add(bar)) # N: Revealed type is "def (builtins.str, x: builtins.int, *args: builtins.bool) -> builtins.bool"
606
607 def remove(x: Callable[Concatenate[int, P], int]) -> Callable[P, bool]: ...
608
609 reveal_type(remove(bar)) # N: Revealed type is "def (*args: builtins.bool) -> builtins.bool"
610
611 def transform(
612 x: Callable[Concatenate[int, P], int]
613 ) -> Callable[Concatenate[str, P], bool]: ...
614
615 # In the PEP, "__a" appears. What is that? Autogenerated names? To what spec?
616 reveal_type(transform(bar)) # N: Revealed type is "def (builtins.str, *args: builtins.bool) -> builtins.bool"
617
618 # CASE 4
619 def expects_int_first(x: Callable[Concatenate[int, P], int]) -> None: ...
620
621 @expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "Callable[[str], int]"; expected "Callable[[int], int]" \
622 # N: This is likely because "one" has named arguments: "x". Consider marking them positional-only
623 def one(x: str) -> int: ...
624
625 @expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "def two(*, x: int) -> int"; expected "def (int, /, *, x: int) -> int"
626 def two(*, x: int) -> int: ...
627
628 @expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "def three(**kwargs: int) -> int"; expected "def (int, /, **kwargs: int) -> int"
629 def three(**kwargs: int) -> int: ...
630
631 @expects_int_first # Accepted
632 def four(*args: int) -> int: ...
633 [builtins fixtures/dict.pyi]
634
635 [case testParamSpecTwiceSolving]
636 from typing_extensions import ParamSpec, Concatenate
637 from typing import Callable, TypeVar
638
639 P = ParamSpec("P")
640 R = TypeVar("R")
641
642 def f(one: Callable[Concatenate[int, P], R], two: Callable[Concatenate[str, P], R]) -> Callable[P, R]: ...
643
644 a: Callable[[int, bytes], str]
645 b: Callable[[str, bytes], str]
646
647 reveal_type(f(a, b)) # N: Revealed type is "def (builtins.bytes) -> builtins.str"
648 [builtins fixtures/paramspec.pyi]
649
650 [case testParamSpecConcatenateInReturn]
651 from typing_extensions import ParamSpec, Concatenate
652 from typing import Callable, Protocol
653
654 P = ParamSpec("P")
655
656 def f(i: Callable[Concatenate[int, P], str]) -> Callable[Concatenate[int, P], str]: ...
657
658 n: Callable[[int, bytes], str]
659
660 reveal_type(f(n)) # N: Revealed type is "def (builtins.int, builtins.bytes) -> builtins.str"
661 [builtins fixtures/paramspec.pyi]
662
663 [case testParamSpecConcatenateNamedArgs]
664 # flags: --extra-checks
665 # this is one noticeable deviation from PEP but I believe it is for the better
666 from typing_extensions import ParamSpec, Concatenate
667 from typing import Callable, TypeVar
668
669 P = ParamSpec("P")
670 R = TypeVar("R")
671
672 def f1(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
673 def result(x: int, /, *args: P.args, **kwargs: P.kwargs) -> R: ...
674
675 return result # Accepted
676
677 def f2(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
678 def result(x: int, *args: P.args, **kwargs: P.kwargs) -> R: ...
679
680 return result # Rejected
681
682 # reason for rejection:
683 f2(lambda x: 42)(42, x=42)
684 [builtins fixtures/paramspec.pyi]
685 [out]
686 main:17: error: Incompatible return value type (got "Callable[[Arg(int, 'x'), **P], R]", expected "Callable[[int, **P], R]")
687 main:17: note: This is likely because "result" has named arguments: "x". Consider marking them positional-only
688
689 [case testNonStrictParamSpecConcatenateNamedArgs]
690 # this is one noticeable deviation from PEP but I believe it is for the better
691 from typing_extensions import ParamSpec, Concatenate
692 from typing import Callable, TypeVar
693
694 P = ParamSpec("P")
695 R = TypeVar("R")
696
697 def f1(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
698 def result(x: int, /, *args: P.args, **kwargs: P.kwargs) -> R: ...
699
700 return result # Accepted
701
702 def f2(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
703 def result(x: int, *args: P.args, **kwargs: P.kwargs) -> R: ...
704
705 return result # Rejected -> Accepted
706
707 # reason for rejection:
708 f2(lambda x: 42)(42, x=42)
709 [builtins fixtures/paramspec.pyi]
710
711 [case testParamSpecConcatenateWithTypeVar]
712 from typing_extensions import ParamSpec, Concatenate
713 from typing import Callable, TypeVar
714
715 P = ParamSpec("P")
716 R = TypeVar("R")
717 S = TypeVar("S")
718
719 def f(c: Callable[Concatenate[S, P], R]) -> Callable[Concatenate[S, P], R]: ...
720
721 def a(n: int) -> None: ...
722
723 n = f(a)
724
725 reveal_type(n) # N: Revealed type is "def (builtins.int)"
726 reveal_type(n(42)) # N: Revealed type is "None"
727 [builtins fixtures/paramspec.pyi]
728
729 [case testCallablesAsParameters]
730 # credits to https://github.com/microsoft/pyright/issues/2705
731 from typing_extensions import ParamSpec, Concatenate
732 from typing import Generic, Callable, Any
733
734 P = ParamSpec("P")
735
736 class Foo(Generic[P]):
737 def __init__(self, func: Callable[P, Any]) -> None: ...
738 def bar(baz: Foo[Concatenate[int, P]]) -> Foo[P]: ...
739
740 def test(a: int, /, b: str) -> str: ...
741
742 abc = Foo(test)
743 reveal_type(abc)
744 bar(abc)
745 [builtins fixtures/paramspec.pyi]
746 [out]
747 main:14: note: Revealed type is "__main__.Foo[[builtins.int, b: builtins.str]]"
748
749 [case testSolveParamSpecWithSelfType]
750 from typing_extensions import ParamSpec, Concatenate
751 from typing import Callable, Generic
752
753 P = ParamSpec("P")
754
755 class Foo(Generic[P]):
756 def foo(self: 'Foo[P]', other: Callable[P, None]) -> None: ...
757
758 n: Foo[[int]]
759 def f(x: int) -> None: ...
760
761 n.foo(f)
762 [builtins fixtures/paramspec.pyi]
763
764 [case testParamSpecLiteralsTypeApplication]
765 from typing_extensions import ParamSpec
766 from typing import Generic, Callable
767
768 P = ParamSpec("P")
769
770 class Z(Generic[P]):
771 def __init__(self, c: Callable[P, None]) -> None:
772 ...
773
774 # it allows valid functions
775 reveal_type(Z[[int]](lambda x: None)) # N: Revealed type is "__main__.Z[[builtins.int]]"
776 reveal_type(Z[[]](lambda: None)) # N: Revealed type is "__main__.Z[[]]"
777 reveal_type(Z[bytes, str](lambda b, s: None)) # N: Revealed type is "__main__.Z[[builtins.bytes, builtins.str]]"
778
779 # it disallows invalid functions
780 def f1(n: str) -> None: ...
781 def f2(b: bytes, i: int) -> None: ...
782
783 Z[[int]](lambda one, two: None) # E: Cannot infer type of lambda \
784 # E: Argument 1 to "Z" has incompatible type "Callable[[Any, Any], None]"; expected "Callable[[int], None]"
785 Z[[int]](f1) # E: Argument 1 to "Z" has incompatible type "Callable[[str], None]"; expected "Callable[[int], None]"
786
787 Z[[]](lambda one: None) # E: Cannot infer type of lambda \
788 # E: Argument 1 to "Z" has incompatible type "Callable[[Any], None]"; expected "Callable[[], None]"
789
790 Z[bytes, str](lambda one: None) # E: Cannot infer type of lambda \
791 # E: Argument 1 to "Z" has incompatible type "Callable[[Any], None]"; expected "Callable[[bytes, str], None]"
792 Z[bytes, str](f2) # E: Argument 1 to "Z" has incompatible type "Callable[[bytes, int], None]"; expected "Callable[[bytes, str], None]"
793
794 [builtins fixtures/paramspec.pyi]
795
796 [case testParamSpecLiteralEllipsis]
797 from typing_extensions import ParamSpec
798 from typing import Generic, Callable
799
800 P = ParamSpec("P")
801
802 class Z(Generic[P]):
803 def __init__(self: 'Z[P]', c: Callable[P, None]) -> None:
804 ...
805
806 def f1() -> None: ...
807 def f2(*args: int) -> None: ...
808 def f3(a: int, *, b: bytes) -> None: ...
809
810 def f4(b: bytes) -> None: ...
811
812 argh: Callable[..., None] = f4
813
814 # check it works
815 Z[...](f1)
816 Z[...](f2)
817 Z[...](f3)
818
819 # check subtyping works
820 n: Z[...]
821 n = Z(f1)
822 n = Z(f2)
823 n = Z(f3)
824
825 [builtins fixtures/paramspec.pyi]
826
827 [case testParamSpecApplyConcatenateTwice]
828 from typing_extensions import ParamSpec, Concatenate
829 from typing import Generic, Callable, Optional
830
831 P = ParamSpec("P")
832
833 class C(Generic[P]):
834 # think PhantomData<T> from rust
835 phantom: Optional[Callable[P, None]]
836
837 def add_str(self) -> C[Concatenate[str, P]]:
838 return C[Concatenate[str, P]]()
839
840 def add_int(self) -> C[Concatenate[int, P]]:
841 return C[Concatenate[int, P]]()
842
843 def f(c: C[P]) -> None:
844 reveal_type(c) # N: Revealed type is "__main__.C[P`-1]"
845
846 n1 = c.add_str()
847 reveal_type(n1) # N: Revealed type is "__main__.C[[builtins.str, **P`-1]]"
848 n2 = n1.add_int()
849 reveal_type(n2) # N: Revealed type is "__main__.C[[builtins.int, builtins.str, **P`-1]]"
850
851 p1 = c.add_int()
852 reveal_type(p1) # N: Revealed type is "__main__.C[[builtins.int, **P`-1]]"
853 p2 = p1.add_str()
854 reveal_type(p2) # N: Revealed type is "__main__.C[[builtins.str, builtins.int, **P`-1]]"
855 [builtins fixtures/paramspec.pyi]
856
857 [case testParamSpecLiteralJoin]
858 from typing import Generic, Callable, Union
859 from typing_extensions import ParamSpec
860
861
862 _P = ParamSpec("_P")
863
864 class Job(Generic[_P]):
865 def __init__(self, target: Callable[_P, None]) -> None:
866 self.target = target
867
868 def func(
869 action: Union[Job[int], Callable[[int], None]],
870 ) -> None:
871 job = action if isinstance(action, Job) else Job(action)
872 reveal_type(job) # N: Revealed type is "__main__.Job[[builtins.int]]"
873 [builtins fixtures/paramspec.pyi]
874
875 [case testApplyParamSpecToParamSpecLiterals]
876 from typing import TypeVar, Generic, Callable
877 from typing_extensions import ParamSpec
878
879 _P = ParamSpec("_P")
880 _R_co = TypeVar("_R_co", covariant=True)
881
882 class Job(Generic[_P, _R_co]):
883 def __init__(self, target: Callable[_P, _R_co]) -> None:
884 self.target = target
885
886 def run_job(job: Job[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> None:
887 ...
888
889
890 def func(job: Job[[int, str], None]) -> None:
891 run_job(job, 42, "Hello")
892 run_job(job, "Hello", 42) # E: Argument 2 to "run_job" has incompatible type "str"; expected "int" \
893 # E: Argument 3 to "run_job" has incompatible type "int"; expected "str"
894 run_job(job, 42, msg="Hello") # E: Unexpected keyword argument "msg" for "run_job"
895 run_job(job, "Hello") # E: Too few arguments for "run_job" \
896 # E: Argument 2 to "run_job" has incompatible type "str"; expected "int"
897
898 def func2(job: Job[..., None]) -> None:
899 run_job(job, 42, "Hello")
900 run_job(job, "Hello", 42)
901 run_job(job, 42, msg="Hello")
902 run_job(job, x=42, msg="Hello")
903 [builtins fixtures/paramspec.pyi]
904
905 [case testExpandNonBareParamSpecAgainstCallable]
906 from typing import Callable, TypeVar, Any
907 from typing_extensions import ParamSpec
908
909 CallableT = TypeVar("CallableT", bound=Callable[..., Any])
910 _P = ParamSpec("_P")
911 _R = TypeVar("_R")
912
913 def simple_decorator(callable: CallableT) -> CallableT:
914 # set some attribute on 'callable'
915 return callable
916
917
918 class A:
919 @simple_decorator
920 def func(self, action: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R:
921 ...
922
923 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"
924 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"
925
926 def f(x: int) -> int:
927 ...
928
929 reveal_type(A().func(f, 42)) # N: Revealed type is "builtins.int"
930
931 reveal_type(A().func(lambda x: x + x, 42)) # N: Revealed type is "builtins.int"
932 [builtins fixtures/paramspec.pyi]
933
934 [case testParamSpecConstraintOnOtherParamSpec]
935 from typing import Callable, TypeVar, Any, Generic
936 from typing_extensions import ParamSpec
937
938 CallableT = TypeVar("CallableT", bound=Callable[..., Any])
939 _P = ParamSpec("_P")
940 _R_co = TypeVar("_R_co", covariant=True)
941
942 def simple_decorator(callable: CallableT) -> CallableT:
943 ...
944
945 class Job(Generic[_P, _R_co]):
946 def __init__(self, target: Callable[_P, _R_co]) -> None:
947 ...
948
949
950 class A:
951 @simple_decorator
952 def func(self, action: Job[_P, None]) -> Job[_P, None]:
953 ...
954
955 reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`3, None]) -> __main__.Job[_P`3, None]"
956 reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`5, None]) -> __main__.Job[_P`5, None]"
957 reveal_type(A().func(Job(lambda x: x))) # N: Revealed type is "__main__.Job[[x: Any], None]"
958
959 def f(x: int, y: int) -> None: ...
960 reveal_type(A().func(Job(f))) # N: Revealed type is "__main__.Job[[x: builtins.int, y: builtins.int], None]"
961 [builtins fixtures/paramspec.pyi]
962
963 [case testConstraintBetweenParamSpecFunctions1]
964 from typing import Callable, TypeVar, Any, Generic
965 from typing_extensions import ParamSpec
966
967 _P = ParamSpec("_P")
968 _R_co = TypeVar("_R_co", covariant=True)
969
970 def simple_decorator(callable: Callable[_P, _R_co]) -> Callable[_P, _R_co]: ...
971 class Job(Generic[_P]): ...
972
973
974 @simple_decorator
975 def func(__action: Job[_P]) -> Callable[_P, None]:
976 ...
977
978 reveal_type(func) # N: Revealed type is "def [_P] (__main__.Job[_P`-1]) -> def (*_P.args, **_P.kwargs)"
979 [builtins fixtures/paramspec.pyi]
980
981 [case testConstraintBetweenParamSpecFunctions2]
982 from typing import Callable, TypeVar, Any, Generic
983 from typing_extensions import ParamSpec
984
985 CallableT = TypeVar("CallableT", bound=Callable[..., Any])
986 _P = ParamSpec("_P")
987
988 def simple_decorator(callable: CallableT) -> CallableT: ...
989 class Job(Generic[_P]): ...
990
991
992 @simple_decorator
993 def func(__action: Job[_P]) -> Callable[_P, None]:
994 ...
995
996 reveal_type(func) # N: Revealed type is "def [_P] (__main__.Job[_P`-1]) -> def (*_P.args, **_P.kwargs)"
997 [builtins fixtures/paramspec.pyi]
998
999 [case testConstraintsBetweenConcatenatePrefixes]
1000 from typing import Any, Callable, Generic, TypeVar
1001 from typing_extensions import Concatenate, ParamSpec
1002
1003 _P = ParamSpec("_P")
1004 _T = TypeVar("_T")
1005
1006 class Awaitable(Generic[_T]): ...
1007
1008 def adds_await() -> Callable[
1009 [Callable[Concatenate[_T, _P], None]],
1010 Callable[Concatenate[_T, _P], Awaitable[None]],
1011 ]:
1012 def decorator(
1013 func: Callable[Concatenate[_T, _P], None],
1014 ) -> Callable[Concatenate[_T, _P], Awaitable[None]]:
1015 ...
1016
1017 return decorator # we want `_T` and `_P` to refer to the same things.
1018 [builtins fixtures/paramspec.pyi]
1019
1020 [case testParamSpecVariance]
1021 from typing import Callable, Generic
1022 from typing_extensions import ParamSpec
1023
1024 _P = ParamSpec("_P")
1025
1026 class Job(Generic[_P]):
1027 def __init__(self, target: Callable[_P, None]) -> None: ...
1028 def into_callable(self) -> Callable[_P, None]: ...
1029
1030 class A:
1031 def func(self, var: int) -> None: ...
1032 def other_func(self, job: Job[[int]]) -> None: ...
1033
1034
1035 job = Job(A().func)
1036 reveal_type(job) # N: Revealed type is "__main__.Job[[var: builtins.int]]"
1037 A().other_func(job) # This should NOT error (despite the keyword)
1038
1039 # and yet the keyword should remain
1040 job.into_callable()(var=42)
1041 job.into_callable()(x=42) # E: Unexpected keyword argument "x"
1042
1043 # similar for other functions
1044 def f1(n: object) -> None: ...
1045 def f2(n: int) -> None: ...
1046 def f3(n: bool) -> None: ...
1047
1048 # just like how this is legal...
1049 a1: Callable[[bool], None]
1050 a1 = f3
1051 a1 = f2
1052 a1 = f1
1053
1054 # ... this is also legal
1055 a2: Job[[bool]]
1056 a2 = Job(f3)
1057 a2 = Job(f2)
1058 a2 = Job(f1)
1059
1060 # and this is not legal
1061 def f4(n: bytes) -> None: ...
1062 a1 = f4 # E: Incompatible types in assignment (expression has type "Callable[[bytes], None]", variable has type "Callable[[bool], None]")
1063 a2 = Job(f4) # E: Argument 1 to "Job" has incompatible type "Callable[[bytes], None]"; expected "Callable[[bool], None]"
1064
1065 # nor is this:
1066 a4: Job[[int]]
1067 a4 = Job(f3) # E: Argument 1 to "Job" has incompatible type "Callable[[bool], None]"; expected "Callable[[int], None]"
1068 a4 = Job(f2)
1069 a4 = Job(f1)
1070
1071 # just like this:
1072 a3: Callable[[int], None]
1073 a3 = f3 # E: Incompatible types in assignment (expression has type "Callable[[bool], None]", variable has type "Callable[[int], None]")
1074 a3 = f2
1075 a3 = f1
1076 [builtins fixtures/paramspec.pyi]
1077
1078 [case testDecoratingClassesThatUseParamSpec]
1079 from typing import Generic, TypeVar, Callable, Any
1080 from typing_extensions import ParamSpec
1081
1082 _P = ParamSpec("_P")
1083 _T = TypeVar("_T")
1084 _F = TypeVar("_F", bound=Callable[..., Any])
1085
1086 def f(x: _F) -> _F: ...
1087
1088 @f # Should be ok
1089 class OnlyParamSpec(Generic[_P]):
1090 pass
1091
1092 @f # Should be ok
1093 class MixedWithTypeVar1(Generic[_P, _T]):
1094 pass
1095
1096 @f # Should be ok
1097 class MixedWithTypeVar2(Generic[_T, _P]):
1098 pass
1099 [builtins fixtures/dict.pyi]
1100
1101 [case testGenericsInInferredParamspec]
1102 from typing import Callable, TypeVar, Generic
1103 from typing_extensions import ParamSpec
1104
1105 _P = ParamSpec("_P")
1106 _T = TypeVar("_T")
1107
1108 class Job(Generic[_P]):
1109 def __init__(self, target: Callable[_P, None]) -> None: ...
1110 def into_callable(self) -> Callable[_P, None]: ...
1111
1112 def generic_f(x: _T) -> None: ...
1113
1114 j = Job(generic_f)
1115 reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`-1]]"
1116
1117 jf = j.into_callable()
1118 reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`4)"
1119 reveal_type(jf(1)) # N: Revealed type is "None"
1120 [builtins fixtures/paramspec.pyi]
1121
1122 [case testGenericsInInferredParamspecReturn]
1123 from typing import Callable, TypeVar, Generic
1124 from typing_extensions import ParamSpec
1125
1126 _P = ParamSpec("_P")
1127 _T = TypeVar("_T")
1128
1129 class Job(Generic[_P, _T]):
1130 def __init__(self, target: Callable[_P, _T]) -> None: ...
1131 def into_callable(self) -> Callable[_P, _T]: ...
1132
1133 def generic_f(x: _T) -> _T: ...
1134
1135 j = Job(generic_f)
1136 reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`3], _T`3]"
1137
1138 jf = j.into_callable()
1139 reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`4) -> _T`4"
1140 reveal_type(jf(1)) # N: Revealed type is "builtins.int"
1141 [builtins fixtures/paramspec.pyi]
1142
1143 [case testStackedConcatenateIsIllegal]
1144 from typing_extensions import Concatenate, ParamSpec
1145 from typing import Callable
1146
1147 P = ParamSpec("P")
1148
1149 def x(f: Callable[Concatenate[int, Concatenate[int, P]], None]) -> None: ... # E: Nested Concatenates are invalid
1150 [builtins fixtures/paramspec.pyi]
1151
1152 [case testPropagatedAnyConstraintsAreOK]
1153 from typing import Any, Callable, Generic, TypeVar
1154 from typing_extensions import ParamSpec
1155
1156 T = TypeVar("T")
1157 P = ParamSpec("P")
1158
1159 def callback(func: Callable[[Any], Any]) -> None: ...
1160 class Job(Generic[P]): ...
1161
1162 @callback
1163 def run_job(job: Job[...]) -> T: ... # E: A function returning TypeVar should receive at least one argument containing the same TypeVar
1164 [builtins fixtures/tuple.pyi]
1165
1166 [case testTupleAndDictOperationsOnParamSpecArgsAndKwargs]
1167 from typing import Callable, Iterator, Iterable, TypeVar, Tuple
1168 from typing_extensions import ParamSpec
1169
1170 P = ParamSpec('P')
1171 T = TypeVar('T')
1172 def enumerate(x: Iterable[T]) -> Iterator[Tuple[int, T]]: ...
1173
1174 def func(callback: Callable[P, str]) -> Callable[P, str]:
1175 def inner(*args: P.args, **kwargs: P.kwargs) -> str:
1176 reveal_type(args[5]) # N: Revealed type is "builtins.object"
1177 for a in args:
1178 reveal_type(a) # N: Revealed type is "builtins.object"
1179 for idx, a in enumerate(args):
1180 reveal_type(idx) # N: Revealed type is "builtins.int"
1181 reveal_type(a) # N: Revealed type is "builtins.object"
1182 b = 'foo' in args
1183 reveal_type(b) # N: Revealed type is "builtins.bool"
1184 reveal_type(args.count(42)) # N: Revealed type is "builtins.int"
1185 reveal_type(len(args)) # N: Revealed type is "builtins.int"
1186 for c, d in kwargs.items():
1187 reveal_type(c) # N: Revealed type is "builtins.str"
1188 reveal_type(d) # N: Revealed type is "builtins.object"
1189 kwargs.pop('bar')
1190 return 'baz'
1191 return inner
1192 [builtins fixtures/paramspec.pyi]
1193
1194 [case testUnpackingParamsSpecArgsAndKwargs]
1195 from typing import Callable
1196 from typing_extensions import ParamSpec
1197
1198 P = ParamSpec("P")
1199
1200 def func(callback: Callable[P, str]) -> Callable[P, str]:
1201 def inner(*args: P.args, **kwargs: P.kwargs) -> str:
1202 a, *b = args
1203 reveal_type(a) # N: Revealed type is "builtins.object"
1204 reveal_type(b) # N: Revealed type is "builtins.list[builtins.object]"
1205 c, *d = kwargs
1206 reveal_type(c) # N: Revealed type is "builtins.str"
1207 reveal_type(d) # N: Revealed type is "builtins.list[builtins.str]"
1208 e = {**kwargs}
1209 reveal_type(e) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
1210 return "foo"
1211 return inner
1212 [builtins fixtures/paramspec.pyi]
1213
1214 [case testParamSpecArgsAndKwargsMismatch]
1215 from typing import Callable
1216 from typing_extensions import ParamSpec
1217
1218 P1 = ParamSpec("P1")
1219
1220 def func(callback: Callable[P1, str]) -> Callable[P1, str]:
1221 def inner(
1222 *args: P1.kwargs, # E: Use "P1.args" for variadic "*" parameter
1223 **kwargs: P1.args, # E: Use "P1.kwargs" for variadic "**" parameter
1224 ) -> str:
1225 return "foo"
1226 return inner
1227 [builtins fixtures/paramspec.pyi]
1228
1229 [case testParamSpecTestPropAccess]
1230 from typing import Callable
1231 from typing_extensions import ParamSpec
1232
1233 P1 = ParamSpec("P1")
1234
1235 def func1(callback: Callable[P1, str]) -> Callable[P1, str]:
1236 def inner(
1237 *args: P1.typo, # E: Use "P1.args" for variadic "*" parameter \
1238 # E: Name "P1.typo" is not defined
1239 **kwargs: P1.kwargs,
1240 ) -> str:
1241 return "foo"
1242 return inner
1243
1244 def func2(callback: Callable[P1, str]) -> Callable[P1, str]:
1245 def inner(
1246 *args: P1.args,
1247 **kwargs: P1.__bound__, # E: Use "P1.kwargs" for variadic "**" parameter \
1248 # E: Name "P1.__bound__" is not defined
1249 ) -> str:
1250 return "foo"
1251 return inner
1252
1253 def func3(callback: Callable[P1, str]) -> Callable[P1, str]:
1254 def inner(
1255 *args: P1.__bound__, # E: Use "P1.args" for variadic "*" parameter \
1256 # E: Name "P1.__bound__" is not defined
1257 **kwargs: P1.invalid, # E: Use "P1.kwargs" for variadic "**" parameter \
1258 # E: Name "P1.invalid" is not defined
1259 ) -> str:
1260 return "foo"
1261 return inner
1262 [builtins fixtures/paramspec.pyi]
1263
1264
1265 [case testInvalidParamSpecDefinitionsWithArgsKwargs]
1266 from typing import Callable, ParamSpec
1267
1268 P = ParamSpec('P')
1269
1270 def c1(f: Callable[P, int], *args: P.args, **kwargs: P.kwargs) -> int: ...
1271 def c2(f: Callable[P, int]) -> int: ...
1272 def c3(f: Callable[P, int], *args, **kwargs) -> int: ...
1273
1274 # It is ok to define,
1275 def c4(f: Callable[P, int], *args: int, **kwargs: str) -> int:
1276 # but not ok to call:
1277 f(*args, **kwargs) # E: Argument 1 has incompatible type "*tuple[int, ...]"; expected "P.args" \
1278 # E: Argument 2 has incompatible type "**dict[str, str]"; expected "P.kwargs"
1279 return 1
1280
1281 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"
1282 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"
1283 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"
1284 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"
1285 def f5(f: Callable[P, int], *args: P.args, extra_keyword_arg: int, **kwargs: P.kwargs) -> int: ... # E: Arguments not allowed after ParamSpec.args
1286
1287 # Error message test:
1288 P1 = ParamSpec('P1')
1289
1290 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"
1291 [builtins fixtures/paramspec.pyi]
1292
1293
1294 [case testInvalidParamSpecAndConcatenateDefinitionsWithArgsKwargs]
1295 from typing import Callable, ParamSpec
1296 from typing_extensions import Concatenate
1297
1298 P = ParamSpec('P')
1299
1300 def c1(f: Callable[Concatenate[int, P], int], *args: P.args, **kwargs: P.kwargs) -> int: ...
1301 def c2(f: Callable[Concatenate[int, P], int]) -> int: ...
1302 def c3(f: Callable[Concatenate[int, P], int], *args, **kwargs) -> int: ...
1303
1304 # It is ok to define,
1305 def c4(f: Callable[Concatenate[int, P], int], *args: int, **kwargs: str) -> int:
1306 # but not ok to call:
1307 f(1, *args, **kwargs) # E: Argument 2 has incompatible type "*tuple[int, ...]"; expected "P.args" \
1308 # E: Argument 3 has incompatible type "**dict[str, str]"; expected "P.kwargs"
1309 return 1
1310
1311 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"
1312 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"
1313 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"
1314 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"
1315 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
1316 [builtins fixtures/paramspec.pyi]
1317
1318
1319 [case testValidParamSpecInsideGenericWithoutArgsAndKwargs]
1320 from typing import Callable, ParamSpec, Generic
1321 from typing_extensions import Concatenate
1322
1323 P = ParamSpec('P')
1324
1325 class Some(Generic[P]): ...
1326
1327 def create(s: Some[P], *args: int): ...
1328 def update(s: Some[P], **kwargs: int): ...
1329 def delete(s: Some[P]): ...
1330
1331 def from_callable1(c: Callable[P, int], *args: int, **kwargs: int) -> Some[P]: ...
1332 def from_callable2(c: Callable[P, int], **kwargs: int) -> Some[P]: ...
1333 def from_callable3(c: Callable[P, int], *args: int) -> Some[P]: ...
1334
1335 def from_extra1(c: Callable[Concatenate[int, P], int], *args: int, **kwargs: int) -> Some[P]: ...
1336 def from_extra2(c: Callable[Concatenate[int, P], int], **kwargs: int) -> Some[P]: ...
1337 def from_extra3(c: Callable[Concatenate[int, P], int], *args: int) -> Some[P]: ...
1338 [builtins fixtures/paramspec.pyi]
1339
1340
1341 [case testUnboundParamSpec]
1342 from typing import Callable, ParamSpec
1343
1344 P1 = ParamSpec('P1')
1345 P2 = ParamSpec('P2')
1346
1347 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" \
1348 # E: ParamSpec "P2" is unbound
1349
1350 def f1(*args: P1.args): ... # E: ParamSpec "P1" is unbound
1351 def f2(**kwargs: P1.kwargs): ... # E: ParamSpec "P1" is unbound
1352 def f3(*args: P1.args, **kwargs: int): ... # E: ParamSpec "P1" is unbound
1353 def f4(*args: int, **kwargs: P1.kwargs): ... # E: ParamSpec "P1" is unbound
1354
1355 # Error message is based on the `args` definition:
1356 def f5(*args: P2.args, **kwargs: P1.kwargs): ... # E: ParamSpec "P2" is unbound \
1357 # E: ParamSpec "P1" is unbound
1358 def f6(*args: P1.args, **kwargs: P2.kwargs): ... # E: ParamSpec "P1" is unbound \
1359 # E: ParamSpec "P2" is unbound
1360
1361 # Multiple `ParamSpec` variables can be found, they should not affect error message:
1362 P3 = ParamSpec('P3')
1363
1364 def f7(first: Callable[P3, int], *args: P1.args, **kwargs: P2.kwargs): ... # E: ParamSpec "P1" is unbound \
1365 # E: ParamSpec "P2" is unbound
1366 def f8(first: Callable[P3, int], *args: P2.args, **kwargs: P1.kwargs): ... # E: ParamSpec "P2" is unbound \
1367 # E: ParamSpec "P1" is unbound
1368
1369 [builtins fixtures/paramspec.pyi]
1370
1371
1372 [case testArgsKwargsWithoutParamSpecVar]
1373 from typing import Generic, Callable, ParamSpec
1374
1375 P = ParamSpec('P')
1376
1377 # This must be allowed:
1378 class Some(Generic[P]):
1379 def call(self, *args: P.args, **kwargs: P.kwargs): ...
1380
1381 def call(*args: P.args, **kwargs: P.kwargs): ... # E: ParamSpec "P" is unbound
1382
1383 [builtins fixtures/paramspec.pyi]
1384
1385 [case testParamSpecInferenceCrash]
1386 from typing import Callable, Generic, ParamSpec, TypeVar
1387
1388 def foo(x: int) -> int: ...
1389 T = TypeVar("T")
1390 def bar(x: T) -> T: ...
1391
1392 P = ParamSpec("P")
1393
1394 class C(Generic[P]):
1395 def __init__(self, fn: Callable[P, int], *args: P.args, **kwargs: P.kwargs): ...
1396
1397 reveal_type(bar(C(fn=foo, x=1))) # N: Revealed type is "__main__.C[[x: builtins.int]]"
1398 [builtins fixtures/paramspec.pyi]
1399
1400 [case testParamSpecClassConstructor]
1401 from typing import ParamSpec, Callable, TypeVar
1402
1403 P = ParamSpec("P")
1404
1405 class SomeClass:
1406 def __init__(self, a: str) -> None:
1407 pass
1408
1409 def func(t: Callable[P, SomeClass], val: Callable[P, SomeClass]) -> Callable[P, SomeClass]:
1410 pass
1411
1412 def func_regular(t: Callable[[T], SomeClass], val: Callable[[T], SomeClass]) -> Callable[[T], SomeClass]:
1413 pass
1414
1415 def constructor(a: str) -> SomeClass:
1416 return SomeClass(a)
1417
1418 def wrong_constructor(a: bool) -> SomeClass:
1419 return SomeClass("a")
1420
1421 def wrong_name_constructor(b: bool) -> SomeClass:
1422 return SomeClass("a")
1423
1424 func(SomeClass, constructor)
1425 reveal_type(func(SomeClass, wrong_constructor)) # N: Revealed type is "def (a: Never) -> __main__.SomeClass"
1426 reveal_type(func_regular(SomeClass, wrong_constructor)) # N: Revealed type is "def (Never) -> __main__.SomeClass"
1427 reveal_type(func(SomeClass, wrong_name_constructor)) # N: Revealed type is "def (Never) -> __main__.SomeClass"
1428 [builtins fixtures/paramspec.pyi]
1429
1430 [case testParamSpecInTypeAliasBasic]
1431 from typing import ParamSpec, Callable
1432
1433 P = ParamSpec("P")
1434 C = Callable[P, int]
1435 def f(n: C[P]) -> C[P]: ...
1436
1437 @f
1438 def bar(x: int) -> int: ...
1439 @f # E: Argument 1 to "f" has incompatible type "Callable[[int], str]"; expected "Callable[[int], int]"
1440 def foo(x: int) -> str: ...
1441
1442 x: C[[int, str]]
1443 reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int"
1444 y: C[int, str]
1445 reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int"
1446 [builtins fixtures/paramspec.pyi]
1447
1448 [case testParamSpecInTypeAliasConcatenate]
1449 from typing import ParamSpec, Callable
1450 from typing_extensions import Concatenate
1451
1452 P = ParamSpec("P")
1453 C = Callable[Concatenate[int, P], int]
1454 def f(n: C[P]) -> C[P]: ...
1455
1456 @f # E: Argument 1 to "f" has incompatible type "Callable[[], int]"; expected "Callable[[int], int]"
1457 def bad() -> int: ...
1458
1459 @f
1460 def bar(x: int) -> int: ...
1461
1462 @f
1463 def bar2(x: int, y: str) -> int: ...
1464 reveal_type(bar2) # N: Revealed type is "def (builtins.int, y: builtins.str) -> builtins.int"
1465
1466 @f # E: Argument 1 to "f" has incompatible type "Callable[[int], str]"; expected "Callable[[int], int]" \
1467 # N: This is likely because "foo" has named arguments: "x". Consider marking them positional-only
1468 def foo(x: int) -> str: ...
1469
1470 @f # E: Argument 1 to "f" has incompatible type "Callable[[str, int], int]"; expected "Callable[[int, int], int]" \
1471 # N: This is likely because "foo2" has named arguments: "x". Consider marking them positional-only
1472 def foo2(x: str, y: int) -> int: ...
1473
1474 x: C[[int, str]]
1475 reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.int, builtins.str) -> builtins.int"
1476 y: C[int, str]
1477 reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.int, builtins.str) -> builtins.int"
1478 [builtins fixtures/paramspec.pyi]
1479
1480 [case testParamSpecInTypeAliasIllegalBare]
1481 from typing import ParamSpec
1482 from typing_extensions import Concatenate, TypeAlias
1483
1484 P = ParamSpec("P")
1485 Bad1: TypeAlias = P # E: Invalid location for ParamSpec "P" \
1486 # N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
1487 Bad2: TypeAlias = Concatenate[int, P] # E: Invalid location for Concatenate \
1488 # N: You can use Concatenate as the first argument to Callable
1489 [builtins fixtures/paramspec.pyi]
1490
1491 [case testParamSpecInTypeAliasRecursive]
1492 from typing import ParamSpec, Callable, Union
1493
1494 P = ParamSpec("P")
1495 C = Callable[P, Union[int, C[P]]]
1496 def f(n: C[P]) -> C[P]: ...
1497
1498 @f
1499 def bar(x: int) -> int: ...
1500
1501 @f
1502 def bar2(__x: int) -> Callable[[int], int]: ...
1503
1504 @f # E: Argument 1 to "f" has incompatible type "Callable[[int], str]"; expected "C[[int]]"
1505 def foo(x: int) -> str: ...
1506
1507 @f # E: Argument 1 to "f" has incompatible type "Callable[[int], Callable[[int], str]]"; expected "C[[int]]"
1508 def foo2(__x: int) -> Callable[[int], str]: ...
1509
1510 x: C[[int, str]]
1511 reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int | (...)"
1512 y: C[int, str]
1513 reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int | (...)"
1514 [builtins fixtures/paramspec.pyi]
1515
1516 [case testParamSpecAliasInRuntimeContext]
1517 from typing import ParamSpec, Generic
1518
1519 P = ParamSpec("P")
1520 class C(Generic[P]): ...
1521
1522 c = C[int, str]()
1523 reveal_type(c) # N: Revealed type is "__main__.C[[builtins.int, builtins.str]]"
1524
1525 A = C[P]
1526 a = A[int, str]()
1527 reveal_type(a) # N: Revealed type is "__main__.C[[builtins.int, builtins.str]]"
1528 [builtins fixtures/paramspec.pyi]
1529
1530 [case testParamSpecAliasInvalidLocations]
1531 from typing import ParamSpec, Generic, List, TypeVar, Callable
1532
1533 P = ParamSpec("P")
1534 T = TypeVar("T")
1535 A = List[T]
1536 def f(x: A[[int, str]]) -> None: ... # E: Bracketed expression "[...]" is not valid as a type
1537 def g(x: A[P]) -> None: ... # E: Invalid location for ParamSpec "P" \
1538 # N: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
1539
1540 C = Callable[P, T]
1541 x: C[int] # E: Bad number of arguments for type alias, expected 2, given 1
1542 y: C[int, str] # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
1543 z: C[int, str, bytes] # E: Bad number of arguments for type alias, expected 2, given 3
1544
1545 [builtins fixtures/paramspec.pyi]
1546
1547 [case testTrivialParametersHandledCorrectly]
1548 from typing import ParamSpec, Generic, TypeVar, Callable, Any
1549 from typing_extensions import Concatenate
1550
1551 P = ParamSpec("P")
1552 T = TypeVar("T")
1553 S = TypeVar("S")
1554
1555 class C(Generic[S, P, T]): ...
1556
1557 def foo(f: Callable[P, int]) -> None:
1558 x: C[Any, ..., Any]
1559 x1: C[int, Concatenate[int, str, P], str]
1560 x = x1 # OK
1561 [builtins fixtures/paramspec.pyi]
1562
1563 [case testParamSpecAliasNested]
1564 from typing import ParamSpec, Callable, List, TypeVar, Generic
1565 from typing_extensions import Concatenate
1566
1567 P = ParamSpec("P")
1568 A = List[Callable[P, None]]
1569 B = List[Callable[Concatenate[int, P], None]]
1570
1571 fs: A[int, str]
1572 reveal_type(fs) # N: Revealed type is "builtins.list[def (builtins.int, builtins.str)]"
1573 gs: B[int, str]
1574 reveal_type(gs) # N: Revealed type is "builtins.list[def (builtins.int, builtins.int, builtins.str)]"
1575
1576 T = TypeVar("T")
1577 class C(Generic[T]): ...
1578 C[Callable[P, int]]()
1579 [builtins fixtures/paramspec.pyi]
1580
1581 [case testConcatDeferralNoCrash]
1582 from typing import Callable, TypeVar
1583 from typing_extensions import Concatenate, ParamSpec
1584
1585 P = ParamSpec("P")
1586 T = TypeVar("T", bound="Defer")
1587
1588 Alias = Callable[P, bool]
1589 Concat = Alias[Concatenate[T, P]]
1590
1591 def test(f: Concat[T, ...]) -> None: ...
1592
1593 class Defer: ...
1594 [builtins fixtures/paramspec.pyi]
1595
1596 [case testNoParamSpecDoubling]
1597 # https://github.com/python/mypy/issues/12734
1598 from typing import Callable, ParamSpec
1599 from typing_extensions import Concatenate
1600
1601 P = ParamSpec("P")
1602 Q = ParamSpec("Q")
1603
1604 def foo(f: Callable[P, int]) -> Callable[P, int]:
1605 return f
1606
1607 def bar(f: Callable[Concatenate[str, Q], int]) -> Callable[Concatenate[str, Q], int]:
1608 return foo(f)
1609 [builtins fixtures/paramspec.pyi]
1610
1611 [case testAlreadyExpandedCallableWithParamSpecReplacement]
1612 from typing import Callable, Any, overload
1613 from typing_extensions import Concatenate, ParamSpec
1614
1615 P = ParamSpec("P")
1616
1617 @overload
1618 def command() -> Callable[[Callable[Concatenate[object, object, P], object]], None]:
1619 ...
1620
1621 @overload
1622 def command(
1623 cls: int = ...,
1624 ) -> Callable[[Callable[Concatenate[object, P], object]], None]:
1625 ...
1626
1627 def command(
1628 cls: int = 42,
1629 ) -> Any:
1630 ...
1631 [builtins fixtures/paramspec.pyi]
1632
1633 [case testCopiedParamSpecComparison]
1634 # minimized from https://github.com/python/mypy/issues/12909
1635 from typing import Callable
1636 from typing_extensions import ParamSpec
1637
1638 P = ParamSpec("P")
1639
1640 def identity(func: Callable[P, None]) -> Callable[P, None]: ...
1641
1642 @identity
1643 def f(f: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
1644 [builtins fixtures/paramspec.pyi]
1645
1646 [case testParamSpecDecoratorAppliedToGeneric]
1647 from typing import Callable, List, TypeVar
1648 from typing_extensions import ParamSpec
1649
1650 P = ParamSpec("P")
1651 T = TypeVar("T")
1652 U = TypeVar("U")
1653
1654 def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ...
1655 def test(x: U) -> U: ...
1656 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]"
1657 reveal_type(dec(test)) # N: Revealed type is "def [T] (x: T`3) -> builtins.list[T`3]"
1658
1659 class A: ...
1660 TA = TypeVar("TA", bound=A)
1661
1662 def test_with_bound(x: TA) -> TA: ...
1663 reveal_type(dec(test_with_bound)) # N: Revealed type is "def [T <: __main__.A] (x: T`5) -> builtins.list[T`5]"
1664 dec(test_with_bound)(0) # E: Value of type variable "T" of function cannot be "int"
1665 dec(test_with_bound)(A()) # OK
1666 [builtins fixtures/paramspec.pyi]
1667
1668 [case testParamSpecArgumentParamInferenceRegular]
1669 from typing import TypeVar, Generic
1670 from typing_extensions import ParamSpec
1671
1672 P = ParamSpec("P")
1673 class Foo(Generic[P]):
1674 def call(self, *args: P.args, **kwargs: P.kwargs) -> None: ...
1675 def test(*args: P.args, **kwargs: P.kwargs) -> Foo[P]: ...
1676
1677 reveal_type(test(1, 2)) # N: Revealed type is "__main__.Foo[[Literal[1]?, Literal[2]?]]"
1678 reveal_type(test(x=1, y=2)) # N: Revealed type is "__main__.Foo[[x: Literal[1]?, y: Literal[2]?]]"
1679 ints = [1, 2, 3]
1680 reveal_type(test(*ints)) # N: Revealed type is "__main__.Foo[[*builtins.int]]"
1681 [builtins fixtures/paramspec.pyi]
1682
1683 [case testParamSpecArgumentParamInferenceGeneric]
1684 from typing import Callable, TypeVar
1685 from typing_extensions import ParamSpec
1686
1687 P = ParamSpec("P")
1688 R = TypeVar("R")
1689 def call(f: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
1690 return f(*args, **kwargs)
1691
1692 T = TypeVar("T")
1693 def identity(x: T) -> T:
1694 return x
1695
1696 reveal_type(call(identity, 2)) # N: Revealed type is "builtins.int"
1697 y: int = call(identity, 2)
1698 [builtins fixtures/paramspec.pyi]
1699
1700 [case testParamSpecNestedApplyNoCrash]
1701 from typing import Callable, TypeVar
1702 from typing_extensions import ParamSpec
1703
1704 P = ParamSpec("P")
1705 T = TypeVar("T")
1706
1707 def apply(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: ...
1708 def test() -> int: ...
1709 reveal_type(apply(apply, test)) # N: Revealed type is "builtins.int"
1710 [builtins fixtures/paramspec.pyi]
1711
1712 [case testParamSpecNestedApplyPosVsNamed]
1713 from typing import Callable, TypeVar
1714 from typing_extensions import ParamSpec
1715
1716 P = ParamSpec("P")
1717 T = TypeVar("T")
1718
1719 def apply(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> None: ...
1720
1721 def test(x: int) -> int: ...
1722 apply(apply, test, x=42) # OK
1723 apply(apply, test, 42) # Also OK (but requires some special casing)
1724 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]"
1725
1726 def test2(x: int, y: str) -> None: ...
1727 apply(apply, test2, 42, "yes")
1728 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]"
1729 apply(apply, test2, x=42, y="yes")
1730 apply(apply, test2, y="yes", x=42)
1731 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]"
1732 [builtins fixtures/paramspec.pyi]
1733
1734 [case testParamSpecApplyPosVsNamedOptional]
1735 from typing import Callable, TypeVar
1736 from typing_extensions import ParamSpec
1737
1738 P = ParamSpec("P")
1739 T = TypeVar("T")
1740
1741 def apply(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> None: ...
1742 def test(x: str = ..., y: int = ...) -> int: ...
1743 apply(test, y=42) # OK
1744 [builtins fixtures/paramspec.pyi]
1745
1746 [case testParamSpecPrefixSubtypingGenericInvalid]
1747 from typing import Generic
1748 from typing_extensions import ParamSpec, Concatenate
1749
1750 P = ParamSpec("P")
1751
1752 class A(Generic[P]):
1753 def foo(self, *args: P.args, **kwargs: P.kwargs):
1754 ...
1755
1756 def bar(b: A[P]) -> A[Concatenate[int, P]]:
1757 return b # E: Incompatible return value type (got "A[P]", expected "A[[int, **P]]")
1758 [builtins fixtures/paramspec.pyi]
1759
1760 [case testParamSpecPrefixSubtypingProtocolInvalid]
1761 from typing import Protocol
1762 from typing_extensions import ParamSpec, Concatenate
1763
1764 P = ParamSpec("P")
1765
1766 class A(Protocol[P]):
1767 def foo(self, *args: P.args, **kwargs: P.kwargs):
1768 ...
1769
1770 def bar(b: A[P]) -> A[Concatenate[int, P]]:
1771 return b # E: Incompatible return value type (got "A[P]", expected "A[[int, **P]]") \
1772 # N: Following member(s) of "A[P]" have conflicts: \
1773 # N: Expected: \
1774 # N: def foo(self, int, /, *args: P.args, **kwargs: P.kwargs) -> Any \
1775 # N: Got: \
1776 # N: def foo(self, *args: P.args, **kwargs: P.kwargs) -> Any
1777 [builtins fixtures/paramspec.pyi]
1778
1779 [case testParamSpecPrefixSubtypingValidNonStrict]
1780 from typing import Protocol
1781 from typing_extensions import ParamSpec, Concatenate
1782
1783 P = ParamSpec("P")
1784
1785 class A(Protocol[P]):
1786 def foo(self, a: int, *args: P.args, **kwargs: P.kwargs):
1787 ...
1788
1789 class B(Protocol[P]):
1790 def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs):
1791 ...
1792
1793 def bar(b: B[P]) -> A[Concatenate[int, P]]:
1794 return b
1795 [builtins fixtures/paramspec.pyi]
1796
1797 [case testParamSpecPrefixSubtypingInvalidStrict]
1798 # flags: --extra-checks
1799 from typing import Protocol
1800 from typing_extensions import ParamSpec, Concatenate
1801
1802 P = ParamSpec("P")
1803
1804 class A(Protocol[P]):
1805 def foo(self, a: int, *args: P.args, **kwargs: P.kwargs):
1806 ...
1807
1808 class B(Protocol[P]):
1809 def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs):
1810 ...
1811
1812 def bar(b: B[P]) -> A[Concatenate[int, P]]:
1813 return b # E: Incompatible return value type (got "B[P]", expected "A[[int, **P]]") \
1814 # N: Following member(s) of "B[P]" have conflicts: \
1815 # N: Expected: \
1816 # N: def foo(self, a: int, int, /, *args: P.args, **kwargs: P.kwargs) -> Any \
1817 # N: Got: \
1818 # N: def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs) -> Any
1819 [builtins fixtures/paramspec.pyi]
1820
1821 [case testParamSpecDecoratorOverload]
1822 from typing import Callable, overload, TypeVar, List
1823 from typing_extensions import ParamSpec
1824
1825 P = ParamSpec("P")
1826 T = TypeVar("T")
1827 def transform(func: Callable[P, List[T]]) -> Callable[P, T]: ...
1828
1829 @overload
1830 def foo(x: int) -> List[float]: ...
1831 @overload
1832 def foo(x: str) -> List[str]: ...
1833 def foo(x): ...
1834
1835 reveal_type(transform(foo)) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.float, def (x: builtins.str) -> builtins.str)"
1836
1837 @transform
1838 @overload
1839 def bar(x: int) -> List[float]: ...
1840 @transform
1841 @overload
1842 def bar(x: str) -> List[str]: ...
1843 @transform
1844 def bar(x): ...
1845
1846 reveal_type(bar) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.float, def (x: builtins.str) -> builtins.str)"
1847 [builtins fixtures/paramspec.pyi]
1848
1849 [case testParamSpecDecoratorOverloadNoCrashOnInvalidTypeVar]
1850 from typing import Any, Callable, List
1851 from typing_extensions import ParamSpec
1852
1853 P = ParamSpec("P")
1854 T = 1
1855
1856 Alias = Callable[P, List[T]] # type: ignore
1857 def dec(fn: Callable[P, T]) -> Alias[P, T]: ... # type: ignore
1858 f: Any
1859 dec(f) # No crash
1860 [builtins fixtures/paramspec.pyi]
1861
1862 [case testParamSpecErrorNestedParams]
1863 from typing import Generic
1864 from typing_extensions import ParamSpec
1865
1866 P = ParamSpec("P")
1867 class C(Generic[P]): ...
1868 c: C[int, [int, str], str] # E: Nested parameter specifications are not allowed
1869 reveal_type(c) # N: Revealed type is "__main__.C[Any]"
1870 [builtins fixtures/paramspec.pyi]
1871
1872 [case testParamSpecInheritNoCrashOnNested]
1873 from typing import Generic
1874 from typing_extensions import ParamSpec
1875
1876 P = ParamSpec("P")
1877 class C(Generic[P]): ...
1878 class D(C[int, [int, str], str]): ... # E: Nested parameter specifications are not allowed
1879 [builtins fixtures/paramspec.pyi]
1880
1881 [case testParamSpecConcatenateSelfType]
1882 from typing import Callable
1883 from typing_extensions import ParamSpec, Concatenate
1884
1885 P = ParamSpec("P")
1886 class A:
1887 def __init__(self, a_param_1: str) -> None: ...
1888
1889 @classmethod
1890 def add_params(cls: Callable[P, A]) -> Callable[Concatenate[float, P], A]:
1891 def new_constructor(i: float, *args: P.args, **kwargs: P.kwargs) -> A:
1892 return cls(*args, **kwargs)
1893 return new_constructor
1894
1895 @classmethod
1896 def remove_params(cls: Callable[Concatenate[str, P], A]) -> Callable[P, A]:
1897 def new_constructor(*args: P.args, **kwargs: P.kwargs) -> A:
1898 return cls("my_special_str", *args, **kwargs)
1899 return new_constructor
1900
1901 reveal_type(A.add_params()) # N: Revealed type is "def (builtins.float, a_param_1: builtins.str) -> __main__.A"
1902 reveal_type(A.remove_params()) # N: Revealed type is "def () -> __main__.A"
1903 [builtins fixtures/paramspec.pyi]
1904
1905 [case testParamSpecConcatenateCallbackProtocol]
1906 from typing import Protocol, TypeVar
1907 from typing_extensions import ParamSpec, Concatenate
1908
1909 P = ParamSpec("P")
1910 R = TypeVar("R", covariant=True)
1911
1912 class Path: ...
1913
1914 class Function(Protocol[P, R]):
1915 def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
1916
1917 def file_cache(fn: Function[Concatenate[Path, P], R]) -> Function[P, R]:
1918 def wrapper(*args: P.args, **kw: P.kwargs) -> R:
1919 return fn(Path(), *args, **kw)
1920 return wrapper
1921
1922 @file_cache
1923 def get_thing(path: Path, *, some_arg: int) -> int: ...
1924 reveal_type(get_thing) # N: Revealed type is "__main__.Function[[*, some_arg: builtins.int], builtins.int]"
1925 get_thing(some_arg=1) # OK
1926 [builtins fixtures/paramspec.pyi]
1927
1928 [case testParamSpecConcatenateKeywordOnly]
1929 from typing import Callable, TypeVar
1930 from typing_extensions import ParamSpec, Concatenate
1931
1932 P = ParamSpec("P")
1933 R = TypeVar("R")
1934
1935 class Path: ...
1936
1937 def file_cache(fn: Callable[Concatenate[Path, P], R]) -> Callable[P, R]:
1938 def wrapper(*args: P.args, **kw: P.kwargs) -> R:
1939 return fn(Path(), *args, **kw)
1940 return wrapper
1941
1942 @file_cache
1943 def get_thing(path: Path, *, some_arg: int) -> int: ...
1944 reveal_type(get_thing) # N: Revealed type is "def (*, some_arg: builtins.int) -> builtins.int"
1945 get_thing(some_arg=1) # OK
1946 [builtins fixtures/paramspec.pyi]
1947
1948 [case testParamSpecConcatenateCallbackApply]
1949 from typing import Callable, Protocol
1950 from typing_extensions import ParamSpec, Concatenate
1951
1952 P = ParamSpec("P")
1953
1954 class FuncType(Protocol[P]):
1955 def __call__(self, x: int, s: str, *args: P.args, **kw_args: P.kwargs) -> str: ...
1956
1957 def forwarder1(fp: FuncType[P], *args: P.args, **kw_args: P.kwargs) -> str:
1958 return fp(0, '', *args, **kw_args)
1959
1960 def forwarder2(fp: Callable[Concatenate[int, str, P], str], *args: P.args, **kw_args: P.kwargs) -> str:
1961 return fp(0, '', *args, **kw_args)
1962
1963 def my_f(x: int, s: str, d: bool) -> str: ...
1964 forwarder1(my_f, True) # OK
1965 forwarder2(my_f, True) # OK
1966 forwarder1(my_f, 1.0) # E: Argument 2 to "forwarder1" has incompatible type "float"; expected "bool"
1967 forwarder2(my_f, 1.0) # E: Argument 2 to "forwarder2" has incompatible type "float"; expected "bool"
1968 [builtins fixtures/paramspec.pyi]
1969
1970 [case testParamSpecCallbackProtocolSelf]
1971 from typing import Callable, Protocol, TypeVar
1972 from typing_extensions import ParamSpec, Concatenate
1973
1974 Params = ParamSpec("Params")
1975 Result = TypeVar("Result", covariant=True)
1976
1977 class FancyMethod(Protocol):
1978 def __call__(self, arg1: int, arg2: str) -> bool: ...
1979 def return_me(self: Callable[Params, Result]) -> Callable[Params, Result]: ...
1980 def return_part(self: Callable[Concatenate[int, Params], Result]) -> Callable[Params, Result]: ...
1981
1982 m: FancyMethod
1983 reveal_type(m.return_me()) # N: Revealed type is "def (arg1: builtins.int, arg2: builtins.str) -> builtins.bool"
1984 reveal_type(m.return_part()) # N: Revealed type is "def (arg2: builtins.str) -> builtins.bool"
1985 [builtins fixtures/paramspec.pyi]
1986
1987 [case testParamSpecInferenceCallableAgainstAny]
1988 from typing import Callable, TypeVar, Any
1989 from typing_extensions import ParamSpec, Concatenate
1990
1991 _P = ParamSpec("_P")
1992 _R = TypeVar("_R")
1993
1994 class A: ...
1995 a = A()
1996
1997 def a_func(
1998 func: Callable[Concatenate[A, _P], _R],
1999 ) -> Callable[Concatenate[Any, _P], _R]:
2000 def wrapper(__a: Any, *args: _P.args, **kwargs: _P.kwargs) -> _R:
2001 return func(a, *args, **kwargs)
2002 return wrapper
2003
2004 def test(a, *args): ...
2005 x: Any
2006 y: object
2007
2008 a_func(test)
2009 x = a_func(test)
2010 y = a_func(test)
2011 [builtins fixtures/paramspec.pyi]
2012
2013 [case testParamSpecInferenceWithCallbackProtocol]
2014 from typing import Protocol, Callable, ParamSpec
2015
2016 class CB(Protocol):
2017 def __call__(self, x: str, y: int) -> None: ...
2018
2019 P = ParamSpec('P')
2020 def g(fn: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2021
2022 cb: CB
2023 g(cb, y=0, x='a') # OK
2024 g(cb, y='a', x=0) # E: Argument "y" to "g" has incompatible type "str"; expected "int" \
2025 # E: Argument "x" to "g" has incompatible type "int"; expected "str"
2026 [builtins fixtures/paramspec.pyi]
2027
2028 [case testParamSpecBadRuntimeTypeApplication]
2029 from typing import ParamSpec, TypeVar, Generic, Callable
2030
2031 R = TypeVar("R")
2032 P = ParamSpec("P")
2033 class C(Generic[P, R]):
2034 x: Callable[P, R]
2035
2036 bad = C[int, str]() # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
2037 reveal_type(bad) # N: Revealed type is "__main__.C[Any, Any]"
2038 reveal_type(bad.x) # N: Revealed type is "def (*Any, **Any) -> Any"
2039 [builtins fixtures/paramspec.pyi]
2040
2041 [case testParamSpecNoCrashOnUnificationAlias]
2042 import mod
2043 [file mod.pyi]
2044 from typing import Callable, Protocol, TypeVar, overload
2045 from typing_extensions import ParamSpec
2046
2047 P = ParamSpec("P")
2048 R_co = TypeVar("R_co", covariant=True)
2049 Handler = Callable[P, R_co]
2050
2051 class HandlerDecorator(Protocol):
2052 def __call__(self, handler: Handler[P, R_co]) -> Handler[P, R_co]: ...
2053
2054 @overload
2055 def event(event_handler: Handler[P, R_co]) -> Handler[P, R_co]: ...
2056 @overload
2057 def event(namespace: str, *args, **kwargs) -> HandlerDecorator: ...
2058 [builtins fixtures/paramspec.pyi]
2059
2060 [case testParamSpecNoCrashOnUnificationCallable]
2061 import mod
2062 [file mod.pyi]
2063 from typing import Callable, Protocol, TypeVar, overload
2064 from typing_extensions import ParamSpec
2065
2066 P = ParamSpec("P")
2067 R_co = TypeVar("R_co", covariant=True)
2068
2069 class HandlerDecorator(Protocol):
2070 def __call__(self, handler: Callable[P, R_co]) -> Callable[P, R_co]: ...
2071
2072 @overload
2073 def event(event_handler: Callable[P, R_co]) -> Callable[P, R_co]: ...
2074 @overload
2075 def event(namespace: str, *args, **kwargs) -> HandlerDecorator: ...
2076 [builtins fixtures/paramspec.pyi]
2077
2078 [case testParamSpecNoCrashOnUnificationPrefix]
2079 from typing import Any, Callable, TypeVar, overload
2080 from typing_extensions import ParamSpec, Concatenate
2081
2082 T = TypeVar("T")
2083 U = TypeVar("U")
2084 V = TypeVar("V")
2085 W = TypeVar("W")
2086 P = ParamSpec("P")
2087
2088 @overload
2089 def call(
2090 func: Callable[Concatenate[T, P], U],
2091 x: T,
2092 *args: Any,
2093 **kwargs: Any,
2094 ) -> U: ...
2095 @overload
2096 def call(
2097 func: Callable[Concatenate[T, U, P], V],
2098 x: T,
2099 y: U,
2100 *args: Any,
2101 **kwargs: Any,
2102 ) -> V: ...
2103 def call(*args: Any, **kwargs: Any) -> Any: ...
2104
2105 def test1(x: int) -> str: ...
2106 def test2(x: int, y: int) -> str: ...
2107 reveal_type(call(test1, 1)) # N: Revealed type is "builtins.str"
2108 reveal_type(call(test2, 1, 2)) # N: Revealed type is "builtins.str"
2109 [builtins fixtures/paramspec.pyi]
2110
2111 [case testParamSpecCorrectParameterNameInference]
2112 from typing import Callable, Protocol
2113 from typing_extensions import ParamSpec, Concatenate
2114
2115 def a(i: int) -> None: ...
2116 def b(__i: int) -> None: ...
2117
2118 class WithName(Protocol):
2119 def __call__(self, i: int) -> None: ...
2120 NoName = Callable[[int], None]
2121
2122 def f1(__fn: WithName, i: int) -> None: ...
2123 def f2(__fn: NoName, i: int) -> None: ...
2124
2125 P = ParamSpec("P")
2126 def d(f: Callable[P, None], fn: Callable[Concatenate[Callable[P, None], P], None]) -> Callable[P, None]:
2127 def inner(*args: P.args, **kwargs: P.kwargs) -> None:
2128 fn(f, *args, **kwargs)
2129 return inner
2130
2131 reveal_type(d(a, f1)) # N: Revealed type is "def (i: builtins.int)"
2132 reveal_type(d(a, f2)) # N: Revealed type is "def (i: builtins.int)"
2133 reveal_type(d(b, f1)) # E: Cannot infer value of type parameter "P" of "d" \
2134 # N: Revealed type is "def (*Any, **Any)"
2135 reveal_type(d(b, f2)) # N: Revealed type is "def (builtins.int)"
2136 [builtins fixtures/paramspec.pyi]
2137
2138 [case testParamSpecGenericWithNamedArg1]
2139 from typing import Callable, TypeVar
2140 from typing_extensions import ParamSpec
2141
2142 R = TypeVar("R")
2143 P = ParamSpec("P")
2144
2145 def run(func: Callable[[], R], *args: object, backend: str = "asyncio") -> R: ...
2146 class Result: ...
2147 def run_portal() -> Result: ...
2148 def submit(func: Callable[P, R], /, *args: P.args, **kwargs: P.kwargs) -> R: ...
2149
2150 reveal_type(submit( # N: Revealed type is "__main__.Result"
2151 run,
2152 run_portal,
2153 backend="asyncio",
2154 ))
2155 submit(
2156 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]"
2157 run_portal,
2158 backend=int(),
2159 )
2160 [builtins fixtures/paramspec.pyi]
2161
2162 [case testInferenceAgainstGenericCallableUnionParamSpec]
2163 from typing import Callable, TypeVar, List, Union
2164 from typing_extensions import ParamSpec
2165
2166 T = TypeVar("T")
2167 P = ParamSpec("P")
2168
2169 def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ...
2170 @dec
2171 def func(arg: T) -> Union[T, str]:
2172 ...
2173 reveal_type(func) # N: Revealed type is "def [T] (arg: T`-1) -> builtins.list[T`-1 | builtins.str]"
2174 reveal_type(func(42)) # N: Revealed type is "builtins.list[builtins.int | builtins.str]"
2175
2176 def dec2(f: Callable[P, List[T]]) -> Callable[P, T]: ...
2177 @dec2
2178 def func2(arg: T) -> List[Union[T, str]]:
2179 ...
2180 reveal_type(func2) # N: Revealed type is "def [T] (arg: T`-1) -> T`-1 | builtins.str"
2181 reveal_type(func2(42)) # N: Revealed type is "builtins.int | builtins.str"
2182 [builtins fixtures/paramspec.pyi]
2183
2184 [case testParamSpecPreciseKindsUsedIfPossible]
2185 from typing import Callable, Generic
2186 from typing_extensions import ParamSpec
2187
2188 P = ParamSpec('P')
2189
2190 class Case(Generic[P]):
2191 def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
2192 pass
2193
2194 def _test(a: int, b: int = 0) -> None: ...
2195
2196 def parametrize(
2197 func: Callable[P, None], *cases: Case[P], **named_cases: Case[P]
2198 ) -> Callable[[], None]:
2199 ...
2200
2201 parametrize(_test, Case(1, 2), Case(3, 4))
2202 parametrize(_test, Case(1, b=2), Case(3, b=4))
2203 parametrize(_test, Case(1, 2), Case(3))
2204 parametrize(_test, Case(1, 2), Case(3, b=4))
2205 [builtins fixtures/paramspec.pyi]
2206
2207 [case testRunParamSpecInsufficientArgs]
2208 from typing_extensions import ParamSpec, Concatenate
2209 from typing import Callable
2210
2211 _P = ParamSpec("_P")
2212
2213 def run(predicate: Callable[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> None:
2214 predicate() # E: Too few arguments
2215 predicate(*args) # E: Too few arguments
2216 predicate(**kwargs) # E: Too few arguments
2217 predicate(*args, **kwargs)
2218
2219 def fn() -> None: ...
2220 def fn_args(x: int) -> None: ...
2221 def fn_posonly(x: int, /) -> None: ...
2222
2223 run(fn)
2224 run(fn_args, 1)
2225 run(fn_args, x=1)
2226 run(fn_posonly, 1)
2227 run(fn_posonly, x=1) # E: Unexpected keyword argument "x" for "run"
2228
2229 [builtins fixtures/paramspec.pyi]
2230
2231 [case testRunParamSpecConcatenateInsufficientArgs]
2232 from typing_extensions import ParamSpec, Concatenate
2233 from typing import Callable
2234
2235 _P = ParamSpec("_P")
2236
2237 def run(predicate: Callable[Concatenate[int, _P], None], *args: _P.args, **kwargs: _P.kwargs) -> None:
2238 predicate() # E: Too few arguments
2239 predicate(1) # E: Too few arguments
2240 predicate(1, *args) # E: Too few arguments
2241 predicate(1, *args) # E: Too few arguments
2242 predicate(1, **kwargs) # E: Too few arguments
2243 predicate(*args, **kwargs) # E: Argument 1 has incompatible type "*_P.args"; expected "int"
2244 predicate(1, *args, **kwargs)
2245
2246 def fn() -> None: ...
2247 def fn_args(x: int, y: str) -> None: ...
2248 def fn_posonly(x: int, /) -> None: ...
2249 def fn_posonly_args(x: int, /, y: str) -> None: ...
2250
2251 run(fn) # E: Argument 1 to "run" has incompatible type "Callable[[], None]"; expected "Callable[[int], None]"
2252 run(fn_args, 1, 'a') # E: Too many arguments for "run" \
2253 # E: Argument 2 to "run" has incompatible type "int"; expected "str"
2254 run(fn_args, y='a')
2255 run(fn_args, 'a')
2256 run(fn_posonly)
2257 run(fn_posonly, x=1) # E: Unexpected keyword argument "x" for "run"
2258 run(fn_posonly_args) # E: Missing positional argument "y" in call to "run"
2259 run(fn_posonly_args, 'a')
2260 run(fn_posonly_args, y='a')
2261
2262 [builtins fixtures/paramspec.pyi]
2263
2264 [case testRunParamSpecConcatenateInsufficientArgsInDecorator]
2265 from typing_extensions import ParamSpec, Concatenate
2266 from typing import Callable
2267
2268 P = ParamSpec("P")
2269
2270 def decorator(fn: Callable[Concatenate[str, P], None]) -> Callable[P, None]:
2271 def inner(*args: P.args, **kwargs: P.kwargs) -> None:
2272 fn("value") # E: Too few arguments
2273 fn("value", *args) # E: Too few arguments
2274 fn("value", **kwargs) # E: Too few arguments
2275 fn(*args, **kwargs) # E: Argument 1 has incompatible type "*P.args"; expected "str"
2276 fn("value", *args, **kwargs)
2277 return inner
2278
2279 @decorator
2280 def foo(s: str, s2: str) -> None: ...
2281
2282 [builtins fixtures/paramspec.pyi]
2283
2284 [case testRunParamSpecOverload]
2285 from typing_extensions import ParamSpec
2286 from typing import Callable, NoReturn, TypeVar, Union, overload
2287
2288 P = ParamSpec("P")
2289 T = TypeVar("T")
2290
2291 @overload
2292 def capture(
2293 sync_fn: Callable[P, NoReturn],
2294 *args: P.args,
2295 **kwargs: P.kwargs,
2296 ) -> int: ...
2297 @overload
2298 def capture(
2299 sync_fn: Callable[P, T],
2300 *args: P.args,
2301 **kwargs: P.kwargs,
2302 ) -> Union[T, int]: ...
2303 def capture(
2304 sync_fn: Callable[P, T],
2305 *args: P.args,
2306 **kwargs: P.kwargs,
2307 ) -> Union[T, int]:
2308 return sync_fn(*args, **kwargs)
2309
2310 def fn() -> str: return ''
2311 def err() -> NoReturn: ...
2312
2313 reveal_type(capture(fn)) # N: Revealed type is "builtins.str | builtins.int"
2314 reveal_type(capture(err)) # N: Revealed type is "builtins.int"
2315
2316 [builtins fixtures/paramspec.pyi]
2317
2318 [case testRunParamSpecOverlappingOverloadsOrder]
2319 from typing import Any, Callable, overload
2320 from typing_extensions import ParamSpec
2321
2322 P = ParamSpec("P")
2323
2324 class Base:
2325 pass
2326 class Child(Base):
2327 def __call__(self) -> str: ...
2328 class NotChild:
2329 def __call__(self) -> str: ...
2330
2331 @overload
2332 def handle(func: Base) -> int: ...
2333 @overload
2334 def handle(func: Callable[P, str], *args: P.args, **kwargs: P.kwargs) -> str: ...
2335 def handle(func: Any, *args: Any, **kwargs: Any) -> Any:
2336 return func(*args, **kwargs)
2337
2338 @overload
2339 def handle_reversed(func: Callable[P, str], *args: P.args, **kwargs: P.kwargs) -> str: ...
2340 @overload
2341 def handle_reversed(func: Base) -> int: ...
2342 def handle_reversed(func: Any, *args: Any, **kwargs: Any) -> Any:
2343 return func(*args, **kwargs)
2344
2345 reveal_type(handle(Child())) # N: Revealed type is "builtins.int"
2346 reveal_type(handle(NotChild())) # N: Revealed type is "builtins.str"
2347
2348 reveal_type(handle_reversed(Child())) # N: Revealed type is "builtins.str"
2349 reveal_type(handle_reversed(NotChild())) # N: Revealed type is "builtins.str"
2350
2351 [builtins fixtures/paramspec.pyi]
2352
2353 [case testBindPartial]
2354 from functools import partial
2355 from typing_extensions import ParamSpec
2356 from typing import Callable, TypeVar
2357
2358 P = ParamSpec("P")
2359 T = TypeVar("T")
2360
2361 def run(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
2362 func2 = partial(func, **kwargs)
2363 return func2(*args)
2364
2365 def run2(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
2366 func2 = partial(func, *args)
2367 return func2(**kwargs)
2368
2369 def run3(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
2370 func2 = partial(func, *args, **kwargs)
2371 return func2()
2372
2373 def run4(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
2374 func2 = partial(func, *args, **kwargs)
2375 return func2(**kwargs)
2376
2377 def run_bad(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
2378 func2 = partial(func, *args, **kwargs)
2379 return func2(*args) # E: Too many arguments
2380
2381 def run_bad2(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
2382 func2 = partial(func, **kwargs)
2383 return func2(**kwargs) # E: Too few arguments
2384
2385 def run_bad3(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
2386 func2 = partial(func, *args)
2387 return func2() # E: Too few arguments
2388
2389 [builtins fixtures/paramspec.pyi]
2390
2391 [case testBindPartialConcatenate]
2392 from functools import partial
2393 from typing_extensions import Concatenate, ParamSpec
2394 from typing import Callable, TypeVar
2395
2396 P = ParamSpec("P")
2397 T = TypeVar("T")
2398
2399 def run(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2400 func2 = partial(func, 1, **kwargs)
2401 return func2(*args)
2402
2403 def run2(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2404 func2 = partial(func, **kwargs)
2405 p = [""]
2406 func2(1, *p) # E: Too few arguments \
2407 # E: Argument 2 has incompatible type "*list[str]"; expected "P.args"
2408 func2(1, 2, *p) # E: Too few arguments \
2409 # E: Argument 2 has incompatible type "int"; expected "P.args" \
2410 # E: Argument 3 has incompatible type "*list[str]"; expected "P.args"
2411 func2(1, *args, *p) # E: Argument 3 has incompatible type "*list[str]"; expected "P.args"
2412 func2(1, *p, *args) # E: Argument 2 has incompatible type "*list[str]"; expected "P.args"
2413 return func2(1, *args)
2414
2415 def run3(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2416 func2 = partial(func, 1, *args)
2417 d = {"":""}
2418 func2(**d) # E: Too few arguments \
2419 # E: Argument 1 has incompatible type "**dict[str, str]"; expected "P.kwargs"
2420 return func2(**kwargs)
2421
2422 def run4(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2423 func2 = partial(func, 1)
2424 return func2(*args, **kwargs)
2425
2426 def run5(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2427 func2 = partial(func, 1, *args, **kwargs)
2428 func2()
2429 return func2(**kwargs)
2430
2431 def run_bad(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2432 func2 = partial(func, *args) # E: Argument 1 has incompatible type "*P.args"; expected "int"
2433 return func2(1, **kwargs) # E: Argument 1 has incompatible type "int"; expected "P.args"
2434
2435 def run_bad2(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2436 func2 = partial(func, 1, *args)
2437 func2() # E: Too few arguments
2438 func2(*args, **kwargs) # E: Too many arguments
2439 return func2(1, **kwargs) # E: Argument 1 has incompatible type "int"; expected "P.args"
2440
2441 def run_bad3(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2442 func2 = partial(func, 1, **kwargs)
2443 func2() # E: Too few arguments
2444 return func2(1, *args) # E: Argument 1 has incompatible type "int"; expected "P.args"
2445
2446 def run_bad4(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2447 func2 = partial(func, 1)
2448 func2() # E: Too few arguments
2449 func2(*args) # E: Too few arguments
2450 func2(1, *args) # E: Too few arguments \
2451 # E: Argument 1 has incompatible type "int"; expected "P.args"
2452 func2(1, **kwargs) # E: Too few arguments \
2453 # E: Argument 1 has incompatible type "int"; expected "P.args"
2454 return func2(**kwargs) # E: Too few arguments
2455
2456 [builtins fixtures/paramspec.pyi]
2457
2458 [case testOtherVarArgs]
2459 from functools import partial
2460 from typing_extensions import Concatenate, ParamSpec
2461 from typing import Callable, TypeVar, Tuple
2462
2463 P = ParamSpec("P")
2464 T = TypeVar("T")
2465
2466 def run(func: Callable[Concatenate[int, str, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
2467 func2 = partial(func, **kwargs)
2468 args_prefix: Tuple[int, str] = (1, 'a')
2469 func2(*args_prefix) # E: Too few arguments
2470 func2(*args, *args_prefix) # E: Argument 1 has incompatible type "*P.args"; expected "int" \
2471 # E: Argument 1 has incompatible type "*P.args"; expected "str" \
2472 # E: Argument 2 has incompatible type "*tuple[int, str]"; expected "P.args"
2473 return func2(*args_prefix, *args)
2474
2475 [builtins fixtures/paramspec.pyi]
2476
2477 [case testParamSpecScoping]
2478 from typing import Any, Callable, Generic
2479 from typing_extensions import Concatenate, ParamSpec
2480
2481 P = ParamSpec("P")
2482 P2 = ParamSpec("P2")
2483
2484 def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2485 def contains_other(f: Callable[P2, None], c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2486
2487 def contains_only_other(c: Callable[P2, None], *args: P.args, **kwargs: P.kwargs) -> None: ... # E: ParamSpec "P" is unbound
2488
2489 def puts_p_into_scope(f: Callable[P, int]) -> None:
2490 def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2491 def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
2492
2493 def puts_p_into_scope_concatenate(f: Callable[Concatenate[int, P], int]) -> None:
2494 def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2495 def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
2496
2497 def wrapper() -> None:
2498 def puts_p_into_scope1(f: Callable[P, int]) -> None:
2499 def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2500 def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
2501
2502 class Wrapper:
2503 def puts_p_into_scope1(self, f: Callable[P, int]) -> None:
2504 def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2505 def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
2506
2507 def contains(self, c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2508
2509 def uses(self, *args: P.args, **kwargs: P.kwargs) -> None: ... # E: ParamSpec "P" is unbound
2510
2511 def method(self) -> None:
2512 def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2513 def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ... # E: ParamSpec "P" is unbound
2514
2515 class GenericWrapper(Generic[P]):
2516 x: P.args # E: ParamSpec components are not allowed here
2517 y: P.kwargs # E: ParamSpec components are not allowed here
2518
2519 def contains(self, c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2520
2521 def puts_p_into_scope1(self, f: Callable[P, int]) -> None:
2522 def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2523 def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
2524
2525 def uses(self, *args: P.args, **kwargs: P.kwargs) -> None: ...
2526
2527 def method(self) -> None:
2528 def contains(c: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
2529 def inherits(*args: P.args, **kwargs: P.kwargs) -> None: ...
2530 [builtins fixtures/paramspec.pyi]
2531
2532 [case testCallbackProtocolClassObjectParamSpec]
2533 from typing import Any, Callable, Protocol, Optional, Generic
2534 from typing_extensions import ParamSpec
2535
2536 P = ParamSpec("P")
2537
2538 class App: ...
2539
2540 class MiddlewareFactory(Protocol[P]):
2541 def __call__(self, app: App, /, *args: P.args, **kwargs: P.kwargs) -> App:
2542 ...
2543
2544 class Capture(Generic[P]): ...
2545
2546 class ServerErrorMiddleware(App):
2547 def __init__(
2548 self,
2549 app: App,
2550 handler: Optional[str] = None,
2551 debug: bool = False,
2552 ) -> None: ...
2553
2554 def fn(f: MiddlewareFactory[P]) -> Capture[P]: ...
2555
2556 reveal_type(fn(ServerErrorMiddleware)) # N: Revealed type is "__main__.Capture[[handler: builtins.str | None =, debug: builtins.bool =]]"
2557 [builtins fixtures/paramspec.pyi]
2558
2559 [case testRunParamSpecDuplicateArgsKwargs]
2560 from typing_extensions import ParamSpec, Concatenate
2561 from typing import Callable, Union
2562
2563 _P = ParamSpec("_P")
2564
2565 def run(predicate: Callable[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> None:
2566 predicate(*args, *args, **kwargs) # E: ParamSpec.args should only be passed once
2567 predicate(*args, **kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once
2568 predicate(*args, *args, **kwargs, **kwargs) # E: ParamSpec.args should only be passed once \
2569 # E: ParamSpec.kwargs should only be passed once
2570 copy_args = args
2571 copy_kwargs = kwargs
2572 predicate(*args, *copy_args, **kwargs) # E: ParamSpec.args should only be passed once
2573 predicate(*copy_args, *args, **kwargs) # E: ParamSpec.args should only be passed once
2574 predicate(*args, **copy_kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once
2575 predicate(*args, **kwargs, **copy_kwargs) # E: ParamSpec.kwargs should only be passed once
2576
2577 def run2(predicate: Callable[Concatenate[int, _P], None], *args: _P.args, **kwargs: _P.kwargs) -> None:
2578 predicate(*args, *args, **kwargs) # E: ParamSpec.args should only be passed once \
2579 # E: Argument 1 has incompatible type "*_P.args"; expected "int"
2580 predicate(*args, **kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once \
2581 # E: Argument 1 has incompatible type "*_P.args"; expected "int"
2582 predicate(1, *args, *args, **kwargs) # E: ParamSpec.args should only be passed once
2583 predicate(1, *args, **kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once
2584 predicate(1, *args, *args, **kwargs, **kwargs) # E: ParamSpec.args should only be passed once \
2585 # E: ParamSpec.kwargs should only be passed once
2586 copy_args = args
2587 copy_kwargs = kwargs
2588 predicate(1, *args, *copy_args, **kwargs) # E: ParamSpec.args should only be passed once
2589 predicate(1, *copy_args, *args, **kwargs) # E: ParamSpec.args should only be passed once
2590 predicate(1, *args, **copy_kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once
2591 predicate(1, *args, **kwargs, **copy_kwargs) # E: ParamSpec.kwargs should only be passed once
2592
2593 def run3(predicate: Callable[Concatenate[int, str, _P], None], *args: _P.args, **kwargs: _P.kwargs) -> None:
2594 base_ok: tuple[int, str]
2595 predicate(*base_ok, *args, **kwargs)
2596 base_bad: tuple[Union[int, str], ...]
2597 predicate(*base_bad, *args, **kwargs) # E: Argument 1 has incompatible type "*tuple[int | str, ...]"; expected "int" \
2598 # E: Argument 1 has incompatible type "*tuple[int | str, ...]"; expected "str" \
2599 # E: Argument 1 has incompatible type "*tuple[int | str, ...]"; expected "_P.args"
2600 [builtins fixtures/paramspec.pyi]
2601
2602 [case testRevealBoundParamSpecArgs]
2603 from typing import Callable, Generic, ParamSpec
2604 from typing_extensions import Concatenate, TypeVarTuple, Unpack
2605
2606 P = ParamSpec("P")
2607 Ts = TypeVarTuple("Ts")
2608
2609 class Sneaky(Generic[P]):
2610 def __init__(self, fn: Callable[P, object], *args: P.args, **kwargs: P.kwargs) -> None:
2611 self.fn = fn
2612 self.args = args
2613 self.kwargs = kwargs
2614
2615 class SneakyPrefix(Generic[P]):
2616 def __init__(self, fn: Callable[Concatenate[int, P], object], _: int, *args: P.args, **kwargs: P.kwargs) -> None:
2617 self.fn = fn
2618 self.args = args
2619 self.kwargs = kwargs
2620
2621 def f1() -> int:
2622 return 0
2623 def f2(x: int) -> int:
2624 return 0
2625 def f3(x: int, /) -> int:
2626 return 0
2627 def f4(*, x: int) -> int:
2628 return 0
2629 def f5(x: int, y: int = 0) -> int:
2630 return 0
2631 def f6(x: int, *args: int) -> int:
2632 return 0
2633 def f7(x: int, *args: Unpack[Ts]) -> int:
2634 return 0
2635 def f8(x: int, *args: Unpack[tuple[str, ...]]) -> int:
2636 return 0
2637 def f9(x: int, *args: Unpack[tuple[str, int]]) -> int:
2638 return 0
2639 def f10(x: int=0, *args: Unpack[tuple[str, ...]]) -> int:
2640 return 0
2641 def f11(x: int = 0, /) -> int:
2642 return 0
2643
2644 reveal_type(Sneaky(f1).args) # N: Revealed type is "tuple[()]"
2645 reveal_type(SneakyPrefix(f1).args) # E: Missing positional argument "_" in call to "SneakyPrefix" \
2646 # N: Revealed type is "tuple[()]" \
2647 # E: Argument 1 to "SneakyPrefix" has incompatible type "Callable[[], int]"; expected "Callable[[int], object]"
2648
2649 reveal_type(Sneaky(f2, 1).args) # N: Revealed type is "tuple[()] | tuple[builtins.int]"
2650 reveal_type(SneakyPrefix(f2, 1).args) # N: Revealed type is "tuple[()]"
2651
2652 reveal_type(Sneaky(f3, 1).args) # N: Revealed type is "tuple[builtins.int]"
2653 reveal_type(SneakyPrefix(f3, 1).args) # N: Revealed type is "tuple[()]"
2654
2655 reveal_type(Sneaky(f4, x=1).args) # N: Revealed type is "tuple[()]"
2656
2657 reveal_type(Sneaky(f5, 1).args) # N: Revealed type is "tuple[()] | tuple[builtins.int] | tuple[builtins.int, builtins.int]"
2658 reveal_type(SneakyPrefix(f5, 1).args) # N: Revealed type is "tuple[()] | tuple[builtins.int]"
2659 reveal_type(Sneaky(f5, 1, 2).args) # N: Revealed type is "tuple[()] | tuple[builtins.int] | tuple[builtins.int, builtins.int]"
2660 reveal_type(SneakyPrefix(f5, 1, 2).args) # N: Revealed type is "tuple[()] | tuple[builtins.int]"
2661
2662 reveal_type(Sneaky(f6, 1).args) # N: Revealed type is "tuple[()] | tuple[builtins.int] | tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]"
2663 reveal_type(SneakyPrefix(f6, 1).args) # N: Revealed type is "tuple[()] | tuple[Unpack[builtins.tuple[builtins.int, ...]]]"
2664 reveal_type(Sneaky(f6, 1, 2).args) # N: Revealed type is "tuple[()] | tuple[builtins.int] | tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]"
2665 reveal_type(SneakyPrefix(f6, 1, 2).args) # N: Revealed type is "tuple[()] | tuple[Unpack[builtins.tuple[builtins.int, ...]]]"
2666
2667 reveal_type(Sneaky(f7, 1, 2).args) # N: Revealed type is "tuple[Literal[1]?, Literal[2]?]"
2668 reveal_type(SneakyPrefix(f7, 1, 2).args) # N: Revealed type is "tuple[Literal[2]?]"
2669
2670 reveal_type(Sneaky(f8, 1, '').args) # N: Revealed type is "tuple[()] | tuple[builtins.int] | tuple[builtins.int, Unpack[builtins.tuple[builtins.str, ...]]]"
2671 reveal_type(SneakyPrefix(f8, 1, '').args) # N: Revealed type is "tuple[()] | tuple[Unpack[builtins.tuple[builtins.str, ...]]]"
2672
2673 reveal_type(Sneaky(f9, 1, '', 0).args) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.int]"
2674 reveal_type(SneakyPrefix(f9, 1, '', 0).args) # N: Revealed type is "tuple[builtins.str, builtins.int]"
2675
2676 reveal_type(Sneaky(f10, 1, '', '').args) # N: Revealed type is "tuple[()] | tuple[builtins.int] | tuple[builtins.int, Unpack[builtins.tuple[builtins.str, ...]]]"
2677 reveal_type(SneakyPrefix(f10, 1, '', '').args) # N: Revealed type is "tuple[()] | tuple[Unpack[builtins.tuple[builtins.str, ...]]]"
2678
2679 reveal_type(Sneaky(f11).args) # N: Revealed type is "tuple[()] | tuple[builtins.int]"
2680 [builtins fixtures/paramspec.pyi]
2681
2682 [case testRevealBoundParamSpecGeneric]
2683 from typing import Callable, Generic, ParamSpec, TypeVar
2684 from typing_extensions import TypeVarTuple, Unpack
2685
2686 T = TypeVar("T")
2687 P = ParamSpec("P")
2688 Ts = TypeVarTuple("Ts")
2689
2690 class SplitSneaky(Generic[P]):
2691 def __init__(self, target: Callable[P, None]) -> None:
2692 ...
2693
2694 def run(self, *args: P.args, **kwargs: P.kwargs) -> None:
2695 self.args = args
2696 self.kwargs = kwargs
2697
2698 def f1(x: T) -> None: ...
2699 def f2(*xs: Unpack[Ts]) -> None: ...
2700 def f3(fn: Callable[P, None]) -> None: ...
2701
2702 reveal_type(SplitSneaky(f1).args) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
2703 reveal_type(SplitSneaky(f1).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
2704 reveal_type(SplitSneaky(f2).args) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
2705 reveal_type(SplitSneaky(f2).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
2706 reveal_type(SplitSneaky(f3).args) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
2707 reveal_type(SplitSneaky(f3).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
2708 [builtins fixtures/paramspec.pyi]
2709
2710 [case testRevealBoundParamSpecKwargs]
2711 from typing import Callable, Generic, ParamSpec
2712 from typing_extensions import Unpack, NotRequired, TypedDict
2713
2714 P = ParamSpec("P")
2715
2716 class Sneaky(Generic[P]):
2717 def __init__(self, fn: Callable[P, object], *args: P.args, **kwargs: P.kwargs) -> None:
2718 self.fn = fn
2719 self.args = args
2720 self.kwargs = kwargs
2721
2722 class Opt(TypedDict):
2723 y: int
2724 z: NotRequired[str]
2725
2726 def f1() -> int:
2727 return 0
2728 def f2(x: int) -> int:
2729 return 0
2730 def f3(x: int, /) -> int:
2731 return 0
2732 def f4(*, x: int) -> int:
2733 return 0
2734 def f5(x: int, y: int = 0) -> int:
2735 return 0
2736 def f6(**kwargs: int) -> int:
2737 return 0
2738 def f7(x: int, **kwargs: str) -> int:
2739 return 0
2740 def f8(x: int, /, **kwargs: str) -> int:
2741 return 0
2742 def f9(x: int, **kwargs: Unpack[Opt]) -> int:
2743 return 0
2744
2745 reveal_type(Sneaky(f1).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {}, closed=True)"
2746 reveal_type(Sneaky(f2, 1).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int}, closed=True)"
2747 reveal_type(Sneaky(f3, 1).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {}, closed=True)"
2748 reveal_type(Sneaky(f4, x=1).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x': builtins.int}, closed=True)"
2749 reveal_type(Sneaky(f5, 1).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y'?: builtins.int}, closed=True)"
2750 reveal_type(Sneaky(f5, 1, 2).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y'?: builtins.int}, closed=True)"
2751 reveal_type(Sneaky(f6, x=1).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
2752 reveal_type(Sneaky(f6, x=1, y=2).kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
2753 reveal_type(Sneaky(f7, 1, y='').kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int})"
2754 reveal_type(Sneaky(f8, 1, y='').kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.str]"
2755 reveal_type(Sneaky(f9, 1, y=0).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y': builtins.int, 'z'?: builtins.str}, closed=True)"
2756 reveal_type(Sneaky(f9, 1, y=0, z='').kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y': builtins.int, 'z'?: builtins.str}, closed=True)"
2757 [builtins fixtures/paramspec.pyi]