Skip to content

Commit 0c7ca83

Browse files
committed
Allow type checkers to infer more precise types for tuples
This is consisent with the latest changes where f(1) could either be an int or a Literal[1] def f[T](x: T) -> T: ... assert_type(f(1), int) # or assert_type(f(1), Literal[1]) The same is possible with tuples: def f[*Ts](*x: *Ts) -> tuple[*Ts]: ... assert_type(f(1), tuple[int]) # or assert_type(f(1), tuple[Literal[1]])
1 parent 4617cb9 commit 0c7ca83

19 files changed

+86
-73
lines changed

conformance/results/mypy/generics_typevartuple_args.toml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ notes = """
33
Does not enforce that tuples captured by TypeVarTuple are same type.
44
"""
55
output = """
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]
6+
generics_typevartuple_args.py:35: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
7+
generics_typevartuple_args.py:36: error: Argument 3 to "exec_le" has incompatible type "str"; expected "Env" [arg-type]
8+
generics_typevartuple_args.py:50: error: Argument 2 to "func1" has incompatible type "str"; expected "int" [arg-type]
9+
generics_typevartuple_args.py:59: error: Argument 2 to "func2" has incompatible type "int"; expected "*tuple[*tuple[str, ...], str]" [arg-type]
10+
generics_typevartuple_args.py:60: error: Too few arguments for "func2" [call-arg]
11+
generics_typevartuple_args.py:61: error: Too few arguments for "func2" [call-arg]
12+
generics_typevartuple_args.py:61: error: Argument 1 to "func2" has incompatible type "str"; expected "int" [arg-type]
13+
generics_typevartuple_args.py:69: error: Too few arguments for "func3" [call-arg]
1414
"""
1515
conformance_automated = "Fail"
1616
errors_diff = """
17-
Line 75: Expected 1 errors
18-
Line 76: Expected 1 errors
17+
Line 77: Expected 1 errors
18+
Line 78: Expected 1 errors
19+
Lines 20, 21: Expected error (tag 'args_to_tuple')
20+
Lines 33, 34: Expected error (tag 'exec_le')
1921
"""
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
conformant = "Pass"
1+
conformant = "Partial"
2+
notes = "Does use assert_type in two different ways"
23
output = """
34
generics_typevartuple_callable.py:26: error: Argument "target" to "Process" has incompatible type "Callable[[int, str], None]"; expected "Callable[[str, int], None]" [arg-type]
45
"""
5-
conformance_automated = "Pass"
6+
conformance_automated = "Fail"
67
errors_diff = """
8+
Lines 49, 50: Expected error (tag 'fun3')
79
"""

conformance/results/pyrefly/aliases_explicit.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ errors_diff = """
44
"""
55
output = """
66
ERROR aliases_explicit.py:67:9-28: `TypeAlias[GoodTypeAlias2, type[int | None]]` is not subscriptable [unsupported-operation]
7-
ERROR aliases_explicit.py:68:9-28: `TypeAlias[GoodTypeAlias3, type[list[GoodTypeAlias2]]]` is not subscriptable [unsupported-operation]
7+
ERROR aliases_explicit.py:68:9-28: `TypeAlias[GoodTypeAlias3, type[list[int | None]]]` is not subscriptable [unsupported-operation]
88
ERROR aliases_explicit.py:69:9-33: Expected 1 type argument for `GoodTypeAlias4`, got 2 [bad-specialization]
99
ERROR aliases_explicit.py:70:9-33: Expected 1 type argument for `GoodTypeAlias8`, got 2 [bad-specialization]
1010
ERROR aliases_explicit.py:71:9-33: Expected a valid ParamSpec expression, got `int` [invalid-param-spec]

conformance/results/pyrefly/aliases_recursive.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ ERROR aliases_recursive.py:63:30-43: `list[list[float] | str | GenericTypeAlias1
1414
ERROR aliases_recursive.py:69:35-64: `list[int | list[int | list[int | list[float] | str | GenericTypeAlias2[str, int]] | str | GenericTypeAlias2[str, int]] | str | GenericTypeAlias2[str, int]]` is not assignable to `list[int | str | GenericTypeAlias2[str, int]]` [bad-assignment]
1515
ERROR aliases_recursive.py:72:29-57: Found cyclic self-reference in `RecursiveUnion` [invalid-type-alias]
1616
ERROR aliases_recursive.py:75:31-61: Found cyclic self-reference in `MutualReference1` [invalid-type-alias]
17+
ERROR aliases_recursive.py:75:93-123: Found cyclic self-reference in `MutualReference2` [invalid-type-alias]
1718
"""

conformance/results/pyrefly/aliases_type_statement.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ ERROR aliases_type_statement.py:77:7-41: `str` is not assignable to upper bound
3131
ERROR aliases_type_statement.py:79:7-41: `int` is not assignable to upper bound `str` of type variable `T` [bad-specialization]
3232
ERROR aliases_type_statement.py:82:28-47: Found cyclic self-reference in `RecursiveTypeAlias3` [invalid-type-alias]
3333
ERROR aliases_type_statement.py:84:31-59: Found cyclic self-reference in `RecursiveTypeAlias4` [invalid-type-alias]
34+
ERROR aliases_type_statement.py:88:28-47: Found cyclic self-reference in `RecursiveTypeAlias6` [invalid-type-alias]
3435
ERROR aliases_type_statement.py:89:28-47: Found cyclic self-reference in `RecursiveTypeAlias7` [invalid-type-alias]
3536
"""
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
conformant = "Partial"
2-
notes = """
3-
Does not detect circular definitions.
4-
"""
5-
conformance_automated = "Fail"
1+
conformance_automated = "Pass"
62
errors_diff = """
7-
Line 48: Expected 1 errors
8-
Line 66: Expected 1 errors
93
"""
104
output = """
115
ERROR aliases_typealiastype.py:32:7-30: Object of class `TypeAliasType` has no attribute `other_attrib` [missing-attribute]
126
ERROR aliases_typealiastype.py:40:5-30: `int` is not assignable to upper bound `str` of type variable `TStr` [bad-specialization]
137
ERROR aliases_typealiastype.py:43:13-66: Type variable `S` is out of scope for this `TypeAliasType` [invalid-type-alias]
148
ERROR aliases_typealiastype.py:44:13-48: Type variable `S` is out of scope for this `TypeAliasType` [invalid-type-alias]
159
ERROR aliases_typealiastype.py:45:45-65: Value for argument `type_params` must be a tuple literal [invalid-type-alias]
16-
ERROR aliases_typealiastype.py:46:41-50: Found cyclic self-reference in `BadAlias4` [invalid-type-alias]
17-
ERROR aliases_typealiastype.py:47:40-60: Found cyclic self-reference in `BadAlias5` [invalid-type-alias]
18-
ERROR aliases_typealiastype.py:49:40-49: Found cyclic self-reference in `BadAlias7` [invalid-type-alias]
10+
ERROR aliases_typealiastype.py:46:13-52: Found cyclic self-reference in `BadAlias4` [invalid-type-alias]
11+
ERROR aliases_typealiastype.py:47:13-79: Found cyclic self-reference in `BadAlias5` [invalid-type-alias]
12+
ERROR aliases_typealiastype.py:48:13-52: Found cyclic self-reference in `BadAlias6` [invalid-type-alias]
13+
ERROR aliases_typealiastype.py:49:13-50: Found cyclic self-reference in `BadAlias7` [invalid-type-alias]
1914
ERROR aliases_typealiastype.py:52:40-80: Function call cannot be used in annotations [invalid-annotation]
2015
ERROR aliases_typealiastype.py:53:40-50: List literal cannot be used in annotations [invalid-annotation]
2116
ERROR aliases_typealiastype.py:54:42-55: Tuple literal cannot be used in annotations [invalid-annotation]
@@ -31,4 +26,5 @@ ERROR aliases_typealiastype.py:61:42-46: Bool literal cannot be used in annotati
3126
ERROR aliases_typealiastype.py:62:42-43: Number literal cannot be used in annotations [invalid-annotation]
3227
ERROR aliases_typealiastype.py:63:42-53: Boolean operation cannot be used in annotations [invalid-annotation]
3328
ERROR aliases_typealiastype.py:64:42-52: F-string cannot be used in annotations [invalid-annotation]
29+
ERROR aliases_typealiastype.py:66:14-59: Found cyclic self-reference in `BadAlias21` [invalid-type-alias]
3430
"""

conformance/results/pyrefly/generics_scoping.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ Does not implement several scoping checks/restrictions for generics
44
"""
55
conformance_automated = "Fail"
66
errors_diff = """
7-
Line 61: Expected 1 errors
8-
Line 65: Expected 1 errors
97
Line 86: Expected 1 errors
108
Line 89: Expected 1 errors
119
Line 98: Expected 1 errors
12-
Line 105: Expected 1 errors
13-
Line 106: Expected 1 errors
1410
Line 107: Expected 1 errors
1511
"""
1612
output = """
@@ -19,5 +15,9 @@ ERROR generics_scoping.py:20:12-38: assert_type(str, Literal['a']) failed [asser
1915
ERROR generics_scoping.py:34:10-13: Argument `Literal['a']` is not assignable to parameter `x` with type `int` in function `MyClass.meth_2` [bad-argument-type]
2016
ERROR generics_scoping.py:50:12-48: assert_type(str, Literal['abc']) failed [assert-type]
2117
ERROR generics_scoping.py:54:12-50: assert_type(bytes, Literal[b'abc']) failed [assert-type]
18+
ERROR generics_scoping.py:61:8-15: Type variable `S` is not in scope [invalid-type-var]
19+
ERROR generics_scoping.py:65:14-21: Type variable `S` is not in scope [invalid-type-var]
2220
ERROR generics_scoping.py:76:11-20: Redundant type parameter declaration [invalid-type-var]
21+
ERROR generics_scoping.py:105:14-15: Type variable `T` is not in scope [invalid-type-var]
22+
ERROR generics_scoping.py:106:14-21: Type variable `T` is not in scope [invalid-type-var]
2323
"""

conformance/results/pyrefly/generics_typevartuple_args.toml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ conformance_automated = "Pass"
33
errors_diff = """
44
"""
55
output = """
6-
ERROR generics_typevartuple_args.py:33: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:34: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: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]
6+
ERROR generics_typevartuple_args.py:21:12-68: assert_type(tuple[int, str], tuple[Literal[1], Literal['a']]) failed [assert-type]
7+
ERROR generics_typevartuple_args.py:34:12-71: assert_type(tuple[int, str], tuple[Literal[0], Literal['']]) failed [assert-type]
8+
ERROR generics_typevartuple_args.py:35: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]
9+
ERROR generics_typevartuple_args.py:36: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]
10+
ERROR generics_typevartuple_args.py:50: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]
11+
ERROR generics_typevartuple_args.py:59: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]
12+
ERROR generics_typevartuple_args.py:60: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]
13+
ERROR generics_typevartuple_args.py:61: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]
14+
ERROR generics_typevartuple_args.py:69:6-9: Unpacked argument `tuple[Literal[1]]` is not assignable to parameter `*args` with type `tuple[int, str]` in function `func3` [bad-argument-type]
15+
ERROR generics_typevartuple_args.py:77:13-19: Argument `tuple[Literal[1], Literal[2]]` is not assignable to parameter `*args` with type `tuple[int]` in function `func4` [bad-argument-type]
16+
ERROR generics_typevartuple_args.py:78:13-19: Argument `tuple[Literal['1']]` is not assignable to parameter `*args` with type `tuple[int]` in function `func4` [bad-argument-type]
1517
"""

conformance/results/pyrefly/generics_typevartuple_callable.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ errors_diff = """
44
"""
55
output = """
66
ERROR generics_typevartuple_callable.py:26:28-35: Argument `tuple[Literal[''], Literal[0]]` is not assignable to parameter `args` with type `tuple[int, str]` in function `Process.__init__` [bad-argument-type]
7+
ERROR generics_typevartuple_callable.py:50:12-71: assert_type(tuple[float, str, complex], tuple[float, Literal[''], complex]) failed [assert-type]
78
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "pyrefly 0.53.0"
1+
version = "pyrefly 0.54.0"

0 commit comments

Comments
 (0)