@@ -2192,3 +2192,114 @@ parametrize(_test, Case(1, b=2), Case(3, b=4))
2192
2192
parametrize(_test, Case(1, 2), Case(3))
2193
2193
parametrize(_test, Case(1, 2), Case(3, b=4))
2194
2194
[builtins fixtures/paramspec.pyi]
2195
+
2196
+ [case testRunParamSpecInsufficientArgs]
2197
+ from typing_extensions import ParamSpec, Concatenate
2198
+ from typing import Callable
2199
+
2200
+ _P = ParamSpec("_P")
2201
+
2202
+ def run(predicate: Callable[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> None: # N: "run" defined here
2203
+ predicate() # E: Too few arguments
2204
+ predicate(*args) # E: Too few arguments
2205
+ predicate(**kwargs) # E: Too few arguments
2206
+ predicate(*args, **kwargs)
2207
+
2208
+ def fn() -> None: ...
2209
+ def fn_args(x: int) -> None: ...
2210
+ def fn_posonly(x: int, /) -> None: ...
2211
+
2212
+ run(fn)
2213
+ run(fn_args, 1)
2214
+ run(fn_args, x=1)
2215
+ run(fn_posonly, 1)
2216
+ run(fn_posonly, x=1) # E: Unexpected keyword argument "x" for "run"
2217
+
2218
+ [builtins fixtures/paramspec.pyi]
2219
+
2220
+ [case testRunParamSpecConcatenateInsufficientArgs]
2221
+ from typing_extensions import ParamSpec, Concatenate
2222
+ from typing import Callable
2223
+
2224
+ _P = ParamSpec("_P")
2225
+
2226
+ def run(predicate: Callable[Concatenate[int, _P], None], *args: _P.args, **kwargs: _P.kwargs) -> None: # N: "run" defined here
2227
+ predicate() # E: Too few arguments
2228
+ predicate(1) # E: Too few arguments
2229
+ predicate(1, *args) # E: Too few arguments
2230
+ predicate(1, *args) # E: Too few arguments
2231
+ predicate(1, **kwargs) # E: Too few arguments
2232
+ predicate(*args, **kwargs) # E: Argument 1 has incompatible type "*_P.args"; expected "int"
2233
+ predicate(1, *args, **kwargs)
2234
+
2235
+ def fn() -> None: ...
2236
+ def fn_args(x: int, y: str) -> None: ...
2237
+ def fn_posonly(x: int, /) -> None: ...
2238
+ def fn_posonly_args(x: int, /, y: str) -> None: ...
2239
+
2240
+ run(fn) # E: Argument 1 to "run" has incompatible type "Callable[[], None]"; expected "Callable[[int], None]"
2241
+ run(fn_args, 1, 'a') # E: Too many arguments for "run" \
2242
+ # E: Argument 2 to "run" has incompatible type "int"; expected "str"
2243
+ run(fn_args, y='a')
2244
+ run(fn_args, 'a')
2245
+ run(fn_posonly)
2246
+ run(fn_posonly, x=1) # E: Unexpected keyword argument "x" for "run"
2247
+ run(fn_posonly_args) # E: Missing positional argument "y" in call to "run"
2248
+ run(fn_posonly_args, 'a')
2249
+ run(fn_posonly_args, y='a')
2250
+
2251
+ [builtins fixtures/paramspec.pyi]
2252
+
2253
+ [case testRunParamSpecConcatenateInsufficientArgsInDecorator]
2254
+ from typing_extensions import ParamSpec, Concatenate
2255
+ from typing import Callable
2256
+
2257
+ P = ParamSpec("P")
2258
+
2259
+ def decorator(fn: Callable[Concatenate[str, P], None]) -> Callable[P, None]:
2260
+ def inner(*args: P.args, **kwargs: P.kwargs) -> None:
2261
+ fn("value") # E: Too few arguments
2262
+ fn("value", *args) # E: Too few arguments
2263
+ fn("value", **kwargs) # E: Too few arguments
2264
+ fn(*args, **kwargs) # E: Argument 1 has incompatible type "*P.args"; expected "str"
2265
+ fn("value", *args, **kwargs)
2266
+ return inner
2267
+
2268
+ @decorator
2269
+ def foo(s: str, s2: str) -> None: ...
2270
+
2271
+ [builtins fixtures/paramspec.pyi]
2272
+
2273
+ [case testRunParamSpecOverload]
2274
+ from typing_extensions import ParamSpec
2275
+ from typing import Callable, NoReturn, TypeVar, Union, overload
2276
+
2277
+ P = ParamSpec("P")
2278
+ T = TypeVar("T")
2279
+
2280
+ @overload
2281
+ def capture(
2282
+ sync_fn: Callable[P, NoReturn],
2283
+ *args: P.args,
2284
+ **kwargs: P.kwargs,
2285
+ ) -> int: ...
2286
+ @overload
2287
+ def capture(
2288
+ sync_fn: Callable[P, T],
2289
+ *args: P.args,
2290
+ **kwargs: P.kwargs,
2291
+ ) -> Union[T, int]: ...
2292
+ def capture(
2293
+ sync_fn: Callable[P, T],
2294
+ *args: P.args,
2295
+ **kwargs: P.kwargs,
2296
+ ) -> Union[T, int]:
2297
+ return sync_fn(*args, **kwargs)
2298
+
2299
+ def fn() -> str: return ''
2300
+ def err() -> NoReturn: ...
2301
+
2302
+ reveal_type(capture(fn)) # N: Revealed type is "Union[builtins.str, builtins.int]"
2303
+ reveal_type(capture(err)) # N: Revealed type is "builtins.int"
2304
+
2305
+ [builtins fixtures/paramspec.pyi]
0 commit comments