Skip to content

Commit 0739b10

Browse files
committed
conformance: Do not mandate Literal promotion in generics
The current test suite mandates that type checkers promote Literal types (Literal[0]) to class types like int in inferring type arguments for generics. I don't think the spec requires that.
1 parent 56b7944 commit 0739b10

File tree

5 files changed

+37
-36
lines changed

5 files changed

+37
-36
lines changed
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
conformant = "Partial"
22
output = """
33
generics_defaults_referential.py:23: error: Expression is of type "type[slice[StartT, StopT, StepT]]", not "type[slice[int, int, int | None]]" [assert-type]
4-
generics_defaults_referential.py:37: error: Argument 1 to "Foo" has incompatible type "str"; expected "int" [arg-type]
5-
generics_defaults_referential.py:53: error: Type parameter "Start2T" has a default type that refers to one or more type variables that are out of scope [misc]
6-
generics_defaults_referential.py:74: error: TypeVar default must be one of the constraint types [misc]
7-
generics_defaults_referential.py:77: error: TypeVar default must be one of the constraint types [misc]
4+
generics_defaults_referential.py:38: error: Argument 1 to "Foo" has incompatible type "str"; expected "int" [arg-type]
5+
generics_defaults_referential.py:54: error: Type parameter "Start2T" has a default type that refers to one or more type variables that are out of scope [misc]
6+
generics_defaults_referential.py:75: error: TypeVar default must be one of the constraint types [misc]
87
generics_defaults_referential.py:78: error: TypeVar default must be one of the constraint types [misc]
9-
generics_defaults_referential.py:103: error: Expression is of type "Bar[int, list[Never]]", not "Bar[int, list[int]]" [assert-type]
8+
generics_defaults_referential.py:79: error: TypeVar default must be one of the constraint types [misc]
9+
generics_defaults_referential.py:104: error: Expression is of type "Bar[int, list[Never]]", not "Bar[int, list[int]]" [assert-type]
1010
"""
1111
conformance_automated = "Fail"
1212
errors_diff = """
13-
Line 36: Expected 1 errors
14-
Line 60: Expected 1 errors
15-
Line 68: Expected 1 errors
13+
Line 37: Expected 1 errors
14+
Line 61: Expected 1 errors
15+
Line 69: Expected 1 errors
1616
Line 23: Unexpected errors ['generics_defaults_referential.py:23: error: Expression is of type "type[slice[StartT, StopT, StepT]]", not "type[slice[int, int, int | None]]" [assert-type]']
17-
Line 77: Unexpected errors ['generics_defaults_referential.py:77: error: TypeVar default must be one of the constraint types [misc]']
18-
Line 103: Unexpected errors ['generics_defaults_referential.py:103: error: Expression is of type "Bar[int, list[Never]]", not "Bar[int, list[int]]" [assert-type]']
17+
Line 78: Unexpected errors ['generics_defaults_referential.py:78: error: TypeVar default must be one of the constraint types [misc]']
18+
Line 104: Unexpected errors ['generics_defaults_referential.py:104: error: Expression is of type "Bar[int, list[Never]]", not "Bar[int, list[int]]" [assert-type]']
1919
"""

conformance/results/pyrefly/generics_defaults_referential.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ conformance_automated = "Pass"
33
errors_diff = """
44
"""
55
output = """
6-
ERROR generics_defaults_referential.py:36:13-15: Argument `Literal['']` is not assignable to parameter `b` with type `int` in function `Foo.__init__` [bad-argument-type]
7-
ERROR generics_defaults_referential.py:37:10-12: Argument `Literal['']` is not assignable to parameter `a` with type `int` in function `Foo.__init__` [bad-argument-type]
8-
ERROR generics_defaults_referential.py:53:7-13: Default of type parameter `Start2T` refers to out-of-scope type parameter `StopT` [invalid-type-var]
9-
ERROR generics_defaults_referential.py:60:11-15: Default of type parameter `S2` refers to out-of-scope type parameter `S1` [invalid-type-var]
10-
ERROR generics_defaults_referential.py:68:40-42: Expected default `TypeVar[X1]` of `Invalid1` to be assignable to the upper bound of `str` [invalid-type-var]
11-
ERROR generics_defaults_referential.py:74:52-54: Expected default `TypeVar[Y1]` of `Invalid2` to be one of the following constraints: `float`, `str` [invalid-type-var]
12-
ERROR generics_defaults_referential.py:78:63-65: Expected default `TypeVar[Y2]` of `AlsoInvalid2` to be one of the following constraints: `bool`, `complex` [invalid-type-var]
6+
ERROR generics_defaults_referential.py:37:17-18: Argument `str` is not assignable to parameter `b` with type `int` in function `Foo.__init__` [bad-argument-type]
7+
ERROR generics_defaults_referential.py:38:14-15: Argument `str` is not assignable to parameter `a` with type `int` in function `Foo.__init__` [bad-argument-type]
8+
ERROR generics_defaults_referential.py:54:7-13: Default of type parameter `Start2T` refers to out-of-scope type parameter `StopT` [invalid-type-var]
9+
ERROR generics_defaults_referential.py:61:11-15: Default of type parameter `S2` refers to out-of-scope type parameter `S1` [invalid-type-var]
10+
ERROR generics_defaults_referential.py:69:40-42: Expected default `TypeVar[X1]` of `Invalid1` to be assignable to the upper bound of `str` [invalid-type-var]
11+
ERROR generics_defaults_referential.py:75:52-54: Expected default `TypeVar[Y1]` of `Invalid2` to be one of the following constraints: `float`, `str` [invalid-type-var]
12+
ERROR generics_defaults_referential.py:79:63-65: Expected default `TypeVar[Y2]` of `AlsoInvalid2` to be one of the following constraints: `bool`, `complex` [invalid-type-var]
1313
"""

conformance/results/pyright/generics_defaults_referential.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
conformant = "Pass"
22
output = """
3-
generics_defaults_referential.py:36:13 - error: Argument of type "Literal['']" cannot be assigned to parameter "b" of type "int" in function "__init__"
4-
  "Literal['']" is not assignable to "int" (reportArgumentType)
5-
generics_defaults_referential.py:37:10 - error: Argument of type "Literal['']" cannot be assigned to parameter "a" of type "int" in function "__init__"
6-
  "Literal['']" is not assignable to "int" (reportArgumentType)
7-
generics_defaults_referential.py:53:7 - error: Type parameter "Start2T" has a default type that refers to one or more type variables that are out of scope
3+
generics_defaults_referential.py:37:17 - error: Argument of type "str" cannot be assigned to parameter "b" of type "int" in function "__init__"
4+
  "str" is not assignable to "int" (reportArgumentType)
5+
generics_defaults_referential.py:38:14 - error: Argument of type "str" cannot be assigned to parameter "a" of type "int" in function "__init__"
6+
  "str" is not assignable to "int" (reportArgumentType)
7+
generics_defaults_referential.py:54:7 - error: Type parameter "Start2T" has a default type that refers to one or more type variables that are out of scope
88
  Type variable "StopT" is not in scope (reportGeneralTypeIssues)
9-
generics_defaults_referential.py:60:11 - error: Type parameter "S2" has a default type that refers to one or more type variables that are out of scope
9+
generics_defaults_referential.py:61:11 - error: Type parameter "S2" has a default type that refers to one or more type variables that are out of scope
1010
  Type variable "S1" is not in scope (reportGeneralTypeIssues)
11-
generics_defaults_referential.py:68:40 - error: TypeVar default type must be a subtype of the bound type (reportGeneralTypeIssues)
12-
generics_defaults_referential.py:74:52 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues)
13-
generics_defaults_referential.py:78:63 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues)
11+
generics_defaults_referential.py:69:40 - error: TypeVar default type must be a subtype of the bound type (reportGeneralTypeIssues)
12+
generics_defaults_referential.py:75:52 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues)
13+
generics_defaults_referential.py:79:63 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues)
1414
"""
1515
conformance_automated = "Pass"
1616
errors_diff = """

conformance/results/zuban/generics_defaults_referential.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ conformance_automated = "Pass"
22
errors_diff = """
33
"""
44
output = """
5-
generics_defaults_referential.py:36: error: Argument 2 to "Foo" has incompatible type "str"; expected "int" [arg-type]
6-
generics_defaults_referential.py:37: error: Argument 1 to "Foo" has incompatible type "str"; expected "int" [arg-type]
7-
generics_defaults_referential.py:53: error: Type parameter "Start2T" has a default type that refers to one or more type variables that are out of scope [misc]
8-
generics_defaults_referential.py:60: error: Type parameter "S2" has a default type that refers to one or more type variables that are out of scope [misc]
9-
generics_defaults_referential.py:68: error: TypeVar default must be a subtype of the bound type [misc]
10-
generics_defaults_referential.py:74: error: TypeVar default must be one of the constraint types [misc]
11-
generics_defaults_referential.py:78: error: TypeVar default must be one of the constraint types [misc]
5+
generics_defaults_referential.py:37: error: Argument 2 to "Foo" has incompatible type "str"; expected "int" [arg-type]
6+
generics_defaults_referential.py:38: error: Argument 1 to "Foo" has incompatible type "str"; expected "int" [arg-type]
7+
generics_defaults_referential.py:54: error: Type parameter "Start2T" has a default type that refers to one or more type variables that are out of scope [misc]
8+
generics_defaults_referential.py:61: error: Type parameter "S2" has a default type that refers to one or more type variables that are out of scope [misc]
9+
generics_defaults_referential.py:69: error: TypeVar default must be a subtype of the bound type [misc]
10+
generics_defaults_referential.py:75: error: TypeVar default must be one of the constraint types [misc]
11+
generics_defaults_referential.py:79: error: TypeVar default must be one of the constraint types [misc]
1212
"""

conformance/tests/generics_defaults_referential.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class Foo(Generic[DefaultStrT, T2]):
3232
def __init__(self, a: DefaultStrT, b: T2) -> None: ...
3333

3434

35-
assert_type(Foo(1, ""), Foo[int, str])
36-
Foo[int](1, "") # E: Foo[int, str] cannot be assigned to self: Foo[int, int] in Foo.__init__
37-
Foo[int]("", 1) # E: Foo[str, int] cannot be assigned to self: Foo[int, int] in Foo.__init__
35+
def func1(i: int, s: str) -> None:
36+
assert_type(Foo(i, s), Foo[int, str])
37+
Foo[int](i, s) # E: Foo[int, str] cannot be assigned to self: Foo[int, int] in Foo.__init__
38+
Foo[int](s, i) # E: Foo[str, int] cannot be assigned to self: Foo[int, int] in Foo.__init__
3839

3940

4041
# > ``T1`` must be used before ``T2`` in the parameter list of the generic.

0 commit comments

Comments
 (0)