Skip to content

Commit

Permalink
Add more tests for variadic Callables (#16198)
Browse files Browse the repository at this point in the history
Supersedes #15254

Note the error message for one of the test is slightly different.
Although it _may_ suggest that `Unpack[...]` is a valid type on its own,
this error is kind of more consistent with old style `*args: int`
annotations.
  • Loading branch information
ilevkivskyi committed Sep 28, 2023
1 parent fddfc8d commit 181cbe8
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1604,3 +1604,52 @@ reveal_type(t[i]) # N: Revealed type is "builtins.int"
t1: Tuple[int, Unpack[Tuple[int, ...]]]
reveal_type(t1[i]) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testTypeVarTupleNotConcreteCallable]
from typing_extensions import Unpack, TypeVarTuple
from typing import Callable, TypeVar, Tuple

T = TypeVar("T")
Args = TypeVarTuple("Args")
Args2 = TypeVarTuple("Args2")

def submit(fn: Callable[[Unpack[Args]], T], *args: Unpack[Args]) -> T:
...

def submit2(fn: Callable[[int, Unpack[Args]], T], *args: Unpack[Tuple[int, Unpack[Args]]]) -> T:
...

def foo(func: Callable[[Unpack[Args]], T], *args: Unpack[Args]) -> T:
return submit(func, *args)

def foo2(func: Callable[[Unpack[Args2]], T], *args: Unpack[Args2]) -> T:
return submit(func, *args)

def foo3(func: Callable[[int, Unpack[Args2]], T], *args: Unpack[Args2]) -> T:
return submit2(func, 1, *args)

def foo_bad(func: Callable[[Unpack[Args2]], T], *args: Unpack[Args2]) -> T:
return submit2(func, 1, *args) # E: Argument 1 to "submit2" has incompatible type "Callable[[VarArg(Unpack[Args2])], T]"; expected "Callable[[int, VarArg(Unpack[Args2])], T]"
[builtins fixtures/tuple.pyi]

[case testTypeVarTupleParamSpecInteraction]
from typing_extensions import Unpack, TypeVarTuple, ParamSpec
from typing import Callable, TypeVar

T = TypeVar("T")
Args = TypeVarTuple("Args")
Args2 = TypeVarTuple("Args2")
P = ParamSpec("P")

def submit(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
...

def foo(func: Callable[[Unpack[Args]], T], *args: Unpack[Args]) -> T:
return submit(func, *args)

def foo2(func: Callable[[Unpack[Args]], T], *args: Unpack[Args2]) -> T:
return submit(func, *args) # E: Argument 2 to "submit" has incompatible type "*Tuple[Unpack[Args2]]"; expected "Unpack[Args]"

def foo3(func: Callable[[int, Unpack[Args2]], T], *args: Unpack[Args2]) -> T:
return submit(func, 1, *args)
[builtins fixtures/tuple.pyi]

0 comments on commit 181cbe8

Please sign in to comment.