Skip to content

Commit 476cf34

Browse files
committed
Use assert_type with tuples in functions so types cannot be inferred as literals
1 parent c1ffe96 commit 476cf34

File tree

7 files changed

+50
-56
lines changed

7 files changed

+50
-56
lines changed

conformance/results/mypy/generics_typevartuple_args.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ notes = """
33
Does not enforce that tuples captured by TypeVarTuple are same type.
44
"""
55
output = """
6-
generics_typevartuple_args.py:37: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
7-
generics_typevartuple_args.py:38: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
8-
generics_typevartuple_args.py:52: error: Argument 2 to "func1" has incompatible type "str"; expected "int" [arg-type]
9-
generics_typevartuple_args.py:61: error: Argument 2 to "func2" has incompatible type "int"; expected "*tuple[*tuple[str, ...], str]" [arg-type]
10-
generics_typevartuple_args.py:62: error: Too few arguments for "func2" [call-arg]
11-
generics_typevartuple_args.py:63: error: Too few arguments for "func2" [call-arg]
12-
generics_typevartuple_args.py:63: error: Argument 1 to "func2" has incompatible type "str"; expected "int" [arg-type]
13-
generics_typevartuple_args.py:71: error: Too few arguments for "func3" [call-arg]
6+
generics_typevartuple_args.py:33: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
7+
generics_typevartuple_args.py:34: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
8+
generics_typevartuple_args.py:48: error: Argument 2 to "func1" has incompatible type "str"; expected "int" [arg-type]
9+
generics_typevartuple_args.py:57: error: Argument 2 to "func2" has incompatible type "int"; expected "*tuple[*tuple[str, ...], str]" [arg-type]
10+
generics_typevartuple_args.py:58: error: Too few arguments for "func2" [call-arg]
11+
generics_typevartuple_args.py:59: error: Too few arguments for "func2" [call-arg]
12+
generics_typevartuple_args.py:59: error: Argument 1 to "func2" has incompatible type "str"; expected "int" [arg-type]
13+
generics_typevartuple_args.py:67: error: Too few arguments for "func3" [call-arg]
1414
"""
1515
conformance_automated = "Fail"
1616
errors_diff = """
17-
Line 79: Expected 1 errors
18-
Line 80: Expected 1 errors
17+
Line 75: Expected 1 errors
18+
Line 76: Expected 1 errors
1919
"""

conformance/results/mypy/generics_typevartuple_callable.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
conformant = "Pass"
12
output = """
23
generics_typevartuple_callable.py:26: error: Argument "target" to "Process" has incompatible type "Callable[[int, str], None]"; expected "Callable[[str, int], None]" [arg-type]
34
"""

conformance/results/pyrefly/generics_typevartuple_args.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ conformance_automated = "Pass"
33
errors_diff = """
44
"""
55
output = """
6-
ERROR generics_typevartuple_args.py:37:8-19: Unpacked argument `tuple[Literal[0], Literal['']]` is not assignable to parameter `*args` with type `tuple[*@_, Env]` in function `exec_le` [bad-argument-type]
7-
ERROR generics_typevartuple_args.py:38:8-30: Unpacked argument `tuple[Literal[0], Literal['']]` is not assignable to parameter `*args` with type `tuple[*@_, Env]` in function `exec_le` [bad-argument-type]
8-
ERROR generics_typevartuple_args.py:52:6-17: Unpacked argument `tuple[Literal[1], Literal['2'], Literal[3]]` is not assignable to parameter `*args` with type `tuple[int, ...]` in function `func1` [bad-argument-type]
9-
ERROR generics_typevartuple_args.py:61:6-16: Unpacked argument `tuple[Literal[1], Literal[1], Literal['']]` is not assignable to parameter `*args` with type `tuple[int, *tuple[str, ...], str]` in function `func2` [bad-argument-type]
10-
ERROR generics_typevartuple_args.py:62:6-9: Unpacked argument `tuple[Literal[1]]` is not assignable to parameter `*args` with type `tuple[int, *tuple[str, ...], str]` in function `func2` [bad-argument-type]
11-
ERROR generics_typevartuple_args.py:63:6-10: Unpacked argument `tuple[Literal['']]` is not assignable to parameter `*args` with type `tuple[int, *tuple[str, ...], str]` in function `func2` [bad-argument-type]
12-
ERROR generics_typevartuple_args.py:71:6-9: Unpacked argument `tuple[Literal[1]]` is not assignable to parameter `*args` with type `tuple[int, str]` in function `func3` [bad-argument-type]
13-
ERROR generics_typevartuple_args.py:79:13-19: Argument `tuple[Literal[1], Literal[2]]` is not assignable to parameter `*args` with type `tuple[int]` in function `func4` [bad-argument-type]
14-
ERROR generics_typevartuple_args.py:80:13-19: Argument `tuple[Literal['1']]` is not assignable to parameter `*args` with type `tuple[int]` in function `func4` [bad-argument-type]
6+
ERROR generics_typevartuple_args.py:33:12-23: Unpacked argument `tuple[Literal[0], Literal['']]` is not assignable to parameter `*args` with type `tuple[*@_, Env]` in function `exec_le` [bad-argument-type]
7+
ERROR generics_typevartuple_args.py:34:12-34: Unpacked argument `tuple[Literal[0], Literal['']]` is not assignable to parameter `*args` with type `tuple[*@_, Env]` in function `exec_le` [bad-argument-type]
8+
ERROR generics_typevartuple_args.py:48:6-17: Unpacked argument `tuple[Literal[1], Literal['2'], Literal[3]]` is not assignable to parameter `*args` with type `tuple[int, ...]` in function `func1` [bad-argument-type]
9+
ERROR generics_typevartuple_args.py:57:6-16: Unpacked argument `tuple[Literal[1], Literal[1], Literal['']]` is not assignable to parameter `*args` with type `tuple[int, *tuple[str, ...], str]` in function `func2` [bad-argument-type]
10+
ERROR generics_typevartuple_args.py:58:6-9: Unpacked argument `tuple[Literal[1]]` is not assignable to parameter `*args` with type `tuple[int, *tuple[str, ...], str]` in function `func2` [bad-argument-type]
11+
ERROR generics_typevartuple_args.py:59:6-10: Unpacked argument `tuple[Literal['']]` is not assignable to parameter `*args` with type `tuple[int, *tuple[str, ...], str]` in function `func2` [bad-argument-type]
12+
ERROR generics_typevartuple_args.py:67:6-9: Unpacked argument `tuple[Literal[1]]` is not assignable to parameter `*args` with type `tuple[int, str]` in function `func3` [bad-argument-type]
13+
ERROR generics_typevartuple_args.py:75:13-19: Argument `tuple[Literal[1], Literal[2]]` is not assignable to parameter `*args` with type `tuple[int]` in function `func4` [bad-argument-type]
14+
ERROR generics_typevartuple_args.py:76:13-19: Argument `tuple[Literal['1']]` is not assignable to parameter `*args` with type `tuple[int]` in function `func4` [bad-argument-type]
1515
"""

conformance/results/pyright/generics_typevartuple_args.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
conformant = "Pass"
22
output = """
3-
generics_typevartuple_args.py:37:16 - error: Argument of type "Literal['']" cannot be assigned to parameter of type "Env" in function "exec_le"
3+
generics_typevartuple_args.py:33:20 - error: Argument of type "Literal['']" cannot be assigned to parameter of type "Env" in function "exec_le"
44
  "Literal['']" is not assignable to "Env" (reportArgumentType)
5-
generics_typevartuple_args.py:38:16 - error: Argument of type "Literal['']" cannot be assigned to parameter of type "Env" in function "exec_le"
5+
generics_typevartuple_args.py:34:20 - error: Argument of type "Literal['']" cannot be assigned to parameter of type "Env" in function "exec_le"
66
  "Literal['']" is not assignable to "Env" (reportArgumentType)
7-
generics_typevartuple_args.py:52:10 - error: Argument of type "Literal['2']" cannot be assigned to parameter of type "int" in function "func1"
7+
generics_typevartuple_args.py:48:10 - error: Argument of type "Literal['2']" cannot be assigned to parameter of type "int" in function "func1"
88
  "Literal['2']" is not assignable to "int" (reportArgumentType)
9-
generics_typevartuple_args.py:61:10 - error: Argument of type "Literal[1]" cannot be assigned to parameter of type "str" in function "func2"
9+
generics_typevartuple_args.py:57:10 - error: Argument of type "Literal[1]" cannot be assigned to parameter of type "str" in function "func2"
1010
  "Literal[1]" is not assignable to "str" (reportArgumentType)
11-
generics_typevartuple_args.py:62:1 - error: Argument missing for parameter "args[2]" (reportCallIssue)
12-
generics_typevartuple_args.py:63:1 - error: Argument missing for parameter "args[2]" (reportCallIssue)
13-
generics_typevartuple_args.py:71:1 - error: Argument missing for parameter "args[1]" (reportCallIssue)
14-
generics_typevartuple_args.py:79:13 - error: Argument of type "tuple[Literal[1], Literal[2]]" cannot be assigned to parameter "args" of type "tuple[*Ts@func4]" in function "func4"
11+
generics_typevartuple_args.py:58:1 - error: Argument missing for parameter "args[2]" (reportCallIssue)
12+
generics_typevartuple_args.py:59:1 - error: Argument missing for parameter "args[2]" (reportCallIssue)
13+
generics_typevartuple_args.py:67:1 - error: Argument missing for parameter "args[1]" (reportCallIssue)
14+
generics_typevartuple_args.py:75:13 - error: Argument of type "tuple[Literal[1], Literal[2]]" cannot be assigned to parameter "args" of type "tuple[*Ts@func4]" in function "func4"
1515
  "tuple[Literal[1], Literal[2]]" is not assignable to "tuple[int]"
1616
    Tuple size mismatch; expected 1 but received 2 (reportArgumentType)
17-
generics_typevartuple_args.py:80:14 - error: Argument of type "tuple[Literal['1']]" cannot be assigned to parameter "args" of type "tuple[*Ts@func4]" in function "func4"
17+
generics_typevartuple_args.py:76:14 - error: Argument of type "tuple[Literal['1']]" cannot be assigned to parameter "args" of type "tuple[*Ts@func4]" in function "func4"
1818
  "Literal['1']" is not assignable to "int" (reportArgumentType)
1919
"""
2020
conformance_automated = "Pass"

conformance/results/zuban/generics_typevartuple_args.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ conformance_automated = "Pass"
22
errors_diff = """
33
"""
44
output = """
5-
generics_typevartuple_args.py:37: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
6-
generics_typevartuple_args.py:38: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
7-
generics_typevartuple_args.py:52: error: Argument 2 to "func1" has incompatible type "str"; expected "int" [arg-type]
8-
generics_typevartuple_args.py:61: error: Argument 2 to "func2" has incompatible type "int"; expected "str" [arg-type]
9-
generics_typevartuple_args.py:62: error: Too few arguments for "func2" [call-arg]
10-
generics_typevartuple_args.py:63: error: Too few arguments for "func2" [call-arg]
11-
generics_typevartuple_args.py:63: error: Argument 1 to "func2" has incompatible type "str"; expected "int" [arg-type]
12-
generics_typevartuple_args.py:71: error: Too few arguments for "func3" [call-arg]
13-
generics_typevartuple_args.py:79: error: Argument 2 to "func4" has incompatible type "tuple[Literal[1], int]"; expected "tuple[int]" [arg-type]
14-
generics_typevartuple_args.py:80: error: Argument 2 to "func4" has incompatible type "tuple[Literal['1']]"; expected "tuple[int]" [arg-type]
5+
generics_typevartuple_args.py:33: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
6+
generics_typevartuple_args.py:34: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
7+
generics_typevartuple_args.py:48: error: Argument 2 to "func1" has incompatible type "str"; expected "int" [arg-type]
8+
generics_typevartuple_args.py:57: error: Argument 2 to "func2" has incompatible type "int"; expected "str" [arg-type]
9+
generics_typevartuple_args.py:58: error: Too few arguments for "func2" [call-arg]
10+
generics_typevartuple_args.py:59: error: Too few arguments for "func2" [call-arg]
11+
generics_typevartuple_args.py:59: error: Argument 1 to "func2" has incompatible type "str"; expected "int" [arg-type]
12+
generics_typevartuple_args.py:67: error: Too few arguments for "func3" [call-arg]
13+
generics_typevartuple_args.py:75: error: Argument 2 to "func4" has incompatible type "tuple[Literal[1], int]"; expected "tuple[int]" [arg-type]
14+
generics_typevartuple_args.py:76: error: Argument 2 to "func4" has incompatible type "tuple[Literal['1']]"; expected "tuple[int]" [arg-type]
1515
"""

conformance/tests/generics_typevartuple_args.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# > If *args is annotated as a type variable tuple, the types of the individual
88
# > arguments become the types in the type variable tuple.
99

10-
from typing import TypeVarTuple, assert_type, Literal
10+
from typing import TypeVarTuple, assert_type
1111

1212

1313
Ts = TypeVarTuple("Ts")
@@ -17,13 +17,6 @@ def args_to_tuple(*args: *Ts) -> tuple[*Ts]:
1717
raise NotImplementedError
1818

1919

20-
def takes_int_and_str(tup: tuple[int, str]) -> None:
21-
raise NotImplementedError
22-
23-
24-
takes_int_and_str(args_to_tuple(1, "a"))
25-
26-
2720
class Env:
2821
...
2922

@@ -32,10 +25,13 @@ def exec_le(path: str, *args: * tuple[*Ts, Env], env: Env | None = None) -> tupl
3225
raise NotImplementedError
3326

3427

35-
assert_type(exec_le("", Env()), tuple[()]) # OK
36-
takes_int_and_str(exec_le("", 0, "", Env()))
37-
exec_le("", 0, "") # E
38-
exec_le("", 0, "", env=Env()) # E
28+
def has_int_and_str(x: int, y: str):
29+
assert_type(args_to_tuple(x, y), tuple[int, str])
30+
31+
assert_type(exec_le("", Env()), tuple[()]) # OK
32+
assert_type(exec_le(y, x, y, Env()), tuple[int, str]) # OK
33+
exec_le("", 0, "") # E
34+
exec_le("", 0, "", env=Env()) # E
3935

4036

4137
# > Using an unpacked unbounded tuple is equivalent to the

conformance/tests/generics_typevartuple_callable.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# > Type variable tuples can also be used in the arguments section of a Callable.
88

99

10-
from typing import Callable, TypeVar, TypeVarTuple, assert_type, Literal
10+
from typing import Callable, TypeVar, TypeVarTuple, assert_type
1111

1212
Ts = TypeVarTuple("Ts")
1313
T = TypeVar("T")
@@ -46,8 +46,5 @@ def func3(*args: * tuple[int, *Ts, T]) -> tuple[T, *Ts]:
4646
raise NotImplementedError
4747

4848

49-
def takes_float_str_complex(tup: tuple[float, str, complex]) -> None:
50-
raise NotImplementedError
51-
52-
53-
takes_float_str_complex(func3(1, "", 3j, 3.4))
49+
def has_int_and_str(x: int, y: str):
50+
assert_type(func3(x, y, 3j, 3.4), tuple[float, str, complex])

0 commit comments

Comments
 (0)