@@ -571,8 +571,7 @@ from typing_extensions import Unpack, TypeVarTuple
571571
572572Ts = TypeVarTuple("Ts")
573573Us = TypeVarTuple("Us")
574- a: Callable[[Unpack[Ts], Unpack[Us]], int] # E: Var args may not appear after named or var args \
575- # E: More than one Unpack in a type is not allowed
574+ a: Callable[[Unpack[Ts], Unpack[Us]], int] # E: More than one Unpack in a type is not allowed
576575reveal_type(a) # N: Revealed type is "def [Ts, Us] (*Unpack[Ts`-1]) -> builtins.int"
577576b: Callable[[Unpack], int] # E: Unpack[...] requires exactly one type argument
578577reveal_type(b) # N: Revealed type is "def (*Any) -> builtins.int"
@@ -730,8 +729,7 @@ A = Tuple[Unpack[Ts], Unpack[Us]] # E: More than one Unpack in a type is not al
730729x: A[int, str]
731730reveal_type(x) # N: Revealed type is "Tuple[builtins.int, builtins.str]"
732731
733- B = Callable[[Unpack[Ts], Unpack[Us]], int] # E: Var args may not appear after named or var args \
734- # E: More than one Unpack in a type is not allowed
732+ B = Callable[[Unpack[Ts], Unpack[Us]], int] # E: More than one Unpack in a type is not allowed
735733y: B[int, str]
736734reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int"
737735
@@ -1912,3 +1910,101 @@ reveal_type(y) # N: Revealed type is "__main__.C[builtins.int, Unpack[builtins.
19121910z = C[int]() # E: Bad number of arguments, expected: at least 2, given: 1
19131911reveal_type(z) # N: Revealed type is "__main__.C[Any, Unpack[builtins.tuple[Any, ...]], Any]"
19141912[builtins fixtures/tuple.pyi]
1913+
1914+ [case testTypeVarTupleBothUnpacksSimple]
1915+ from typing import Tuple
1916+ from typing_extensions import Unpack, TypeVarTuple, TypedDict
1917+
1918+ class Keywords(TypedDict):
1919+ a: str
1920+ b: str
1921+
1922+ Ints = Tuple[int, ...]
1923+
1924+ def f(*args: Unpack[Ints], other: str = "no", **kwargs: Unpack[Keywords]) -> None: ...
1925+ reveal_type(f) # N: Revealed type is "def (*args: builtins.int, other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
1926+ f(1, 2, a="a", b="b") # OK
1927+ f(1, 2, 3) # E: Missing named argument "a" for "f" \
1928+ # E: Missing named argument "b" for "f"
1929+
1930+ Ts = TypeVarTuple("Ts")
1931+ def g(*args: Unpack[Ts], other: str = "no", **kwargs: Unpack[Keywords]) -> None: ...
1932+ reveal_type(g) # N: Revealed type is "def [Ts] (*args: Unpack[Ts`-1], other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
1933+ g(1, 2, a="a", b="b") # OK
1934+ g(1, 2, 3) # E: Missing named argument "a" for "g" \
1935+ # E: Missing named argument "b" for "g"
1936+
1937+ def bad(
1938+ *args: Unpack[Keywords], # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple)
1939+ **kwargs: Unpack[Ints], # E: Unpack item in ** argument must be a TypedDict
1940+ ) -> None: ...
1941+ reveal_type(bad) # N: Revealed type is "def (*args: Any, **kwargs: Any)"
1942+
1943+ def bad2(
1944+ one: int,
1945+ *args: Unpack[Keywords], # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple)
1946+ other: str = "no",
1947+ **kwargs: Unpack[Ints], # E: Unpack item in ** argument must be a TypedDict
1948+ ) -> None: ...
1949+ reveal_type(bad2) # N: Revealed type is "def (one: builtins.int, *args: Any, other: builtins.str =, **kwargs: Any)"
1950+ [builtins fixtures/tuple.pyi]
1951+
1952+ [case testTypeVarTupleBothUnpacksCallable]
1953+ from typing import Callable, Tuple
1954+ from typing_extensions import Unpack, TypedDict
1955+
1956+ class Keywords(TypedDict):
1957+ a: str
1958+ b: str
1959+ Ints = Tuple[int, ...]
1960+
1961+ cb: Callable[[Unpack[Ints], Unpack[Keywords]], None]
1962+ reveal_type(cb) # N: Revealed type is "def (*builtins.int, **Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
1963+
1964+ cb2: Callable[[int, Unpack[Ints], int, Unpack[Keywords]], None]
1965+ reveal_type(cb2) # N: Revealed type is "def (builtins.int, *Unpack[Tuple[Unpack[builtins.tuple[builtins.int, ...]], builtins.int]], **Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
1966+ cb2(1, 2, 3, a="a", b="b")
1967+ cb2(1, a="a", b="b") # E: Too few arguments
1968+ cb2(1, 2, 3, a="a") # E: Missing named argument "b"
1969+
1970+ bad1: Callable[[Unpack[Ints], Unpack[Ints]], None] # E: More than one Unpack in a type is not allowed
1971+ reveal_type(bad1) # N: Revealed type is "def (*builtins.int)"
1972+ bad2: Callable[[Unpack[Keywords], Unpack[Keywords]], None] # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple)
1973+ reveal_type(bad2) # N: Revealed type is "def (*Any, **Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
1974+ bad3: Callable[[Unpack[Keywords], Unpack[Ints]], None] # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple) \
1975+ # E: More than one Unpack in a type is not allowed
1976+ reveal_type(bad3) # N: Revealed type is "def (*Any)"
1977+ [builtins fixtures/tuple.pyi]
1978+
1979+ [case testTypeVarTupleBothUnpacksApplication]
1980+ from typing import Callable, TypeVar, Optional
1981+ from typing_extensions import Unpack, TypeVarTuple, TypedDict
1982+
1983+ class Keywords(TypedDict):
1984+ a: str
1985+ b: str
1986+
1987+ T = TypeVar("T")
1988+ Ts = TypeVarTuple("Ts")
1989+ def test(
1990+ x: int,
1991+ func: Callable[[Unpack[Ts]], T],
1992+ *args: Unpack[Ts],
1993+ other: Optional[str] = None,
1994+ **kwargs: Unpack[Keywords],
1995+ ) -> T:
1996+ if bool():
1997+ func(*args, **kwargs) # E: Extra argument "a" from **args
1998+ return func(*args)
1999+ def test2(
2000+ x: int,
2001+ func: Callable[[Unpack[Ts], Unpack[Keywords]], T],
2002+ *args: Unpack[Ts],
2003+ other: Optional[str] = None,
2004+ **kwargs: Unpack[Keywords],
2005+ ) -> T:
2006+ if bool():
2007+ func(*args) # E: Missing named argument "a" \
2008+ # E: Missing named argument "b"
2009+ return func(*args, **kwargs)
2010+ [builtins fixtures/tuple.pyi]
0 commit comments