From 0739b106d95ffe0b4a54317cbc8df44eb6245e61 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 5 Mar 2026 06:03:09 -0800 Subject: [PATCH 1/2] 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. --- .../mypy/generics_defaults_referential.toml | 20 +++++++++---------- .../generics_defaults_referential.toml | 14 ++++++------- .../generics_defaults_referential.toml | 18 ++++++++--------- .../zuban/generics_defaults_referential.toml | 14 ++++++------- .../tests/generics_defaults_referential.py | 7 ++++--- 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/conformance/results/mypy/generics_defaults_referential.toml b/conformance/results/mypy/generics_defaults_referential.toml index bad589849..7821fc761 100644 --- a/conformance/results/mypy/generics_defaults_referential.toml +++ b/conformance/results/mypy/generics_defaults_referential.toml @@ -1,19 +1,19 @@ conformant = "Partial" output = """ generics_defaults_referential.py:23: error: Expression is of type "type[slice[StartT, StopT, StepT]]", not "type[slice[int, int, int | None]]" [assert-type] -generics_defaults_referential.py:37: error: Argument 1 to "Foo" has incompatible type "str"; expected "int" [arg-type] -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] -generics_defaults_referential.py:74: error: TypeVar default must be one of the constraint types [misc] -generics_defaults_referential.py:77: error: TypeVar default must be one of the constraint types [misc] +generics_defaults_referential.py:38: error: Argument 1 to "Foo" has incompatible type "str"; expected "int" [arg-type] +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] +generics_defaults_referential.py:75: error: TypeVar default must be one of the constraint types [misc] generics_defaults_referential.py:78: error: TypeVar default must be one of the constraint types [misc] -generics_defaults_referential.py:103: error: Expression is of type "Bar[int, list[Never]]", not "Bar[int, list[int]]" [assert-type] +generics_defaults_referential.py:79: error: TypeVar default must be one of the constraint types [misc] +generics_defaults_referential.py:104: error: Expression is of type "Bar[int, list[Never]]", not "Bar[int, list[int]]" [assert-type] """ conformance_automated = "Fail" errors_diff = """ -Line 36: Expected 1 errors -Line 60: Expected 1 errors -Line 68: Expected 1 errors +Line 37: Expected 1 errors +Line 61: Expected 1 errors +Line 69: Expected 1 errors 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]'] -Line 77: Unexpected errors ['generics_defaults_referential.py:77: error: TypeVar default must be one of the constraint types [misc]'] -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]'] +Line 78: Unexpected errors ['generics_defaults_referential.py:78: error: TypeVar default must be one of the constraint types [misc]'] +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]'] """ diff --git a/conformance/results/pyrefly/generics_defaults_referential.toml b/conformance/results/pyrefly/generics_defaults_referential.toml index 1e50b0e6c..138472455 100644 --- a/conformance/results/pyrefly/generics_defaults_referential.toml +++ b/conformance/results/pyrefly/generics_defaults_referential.toml @@ -3,11 +3,11 @@ conformance_automated = "Pass" errors_diff = """ """ output = """ -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] -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] -ERROR generics_defaults_referential.py:53:7-13: Default of type parameter `Start2T` refers to out-of-scope type parameter `StopT` [invalid-type-var] -ERROR generics_defaults_referential.py:60:11-15: Default of type parameter `S2` refers to out-of-scope type parameter `S1` [invalid-type-var] -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] -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] -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] +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] +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] +ERROR generics_defaults_referential.py:54:7-13: Default of type parameter `Start2T` refers to out-of-scope type parameter `StopT` [invalid-type-var] +ERROR generics_defaults_referential.py:61:11-15: Default of type parameter `S2` refers to out-of-scope type parameter `S1` [invalid-type-var] +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] +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] +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] """ diff --git a/conformance/results/pyright/generics_defaults_referential.toml b/conformance/results/pyright/generics_defaults_referential.toml index e616dc0a9..f05169557 100644 --- a/conformance/results/pyright/generics_defaults_referential.toml +++ b/conformance/results/pyright/generics_defaults_referential.toml @@ -1,16 +1,16 @@ conformant = "Pass" output = """ -generics_defaults_referential.py:36:13 - error: Argument of type "Literal['']" cannot be assigned to parameter "b" of type "int" in function "__init__" -  "Literal['']" is not assignable to "int" (reportArgumentType) -generics_defaults_referential.py:37:10 - error: Argument of type "Literal['']" cannot be assigned to parameter "a" of type "int" in function "__init__" -  "Literal['']" is not assignable to "int" (reportArgumentType) -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 +generics_defaults_referential.py:37:17 - error: Argument of type "str" cannot be assigned to parameter "b" of type "int" in function "__init__" +  "str" is not assignable to "int" (reportArgumentType) +generics_defaults_referential.py:38:14 - error: Argument of type "str" cannot be assigned to parameter "a" of type "int" in function "__init__" +  "str" is not assignable to "int" (reportArgumentType) +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   Type variable "StopT" is not in scope (reportGeneralTypeIssues) -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 +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   Type variable "S1" is not in scope (reportGeneralTypeIssues) -generics_defaults_referential.py:68:40 - error: TypeVar default type must be a subtype of the bound type (reportGeneralTypeIssues) -generics_defaults_referential.py:74:52 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues) -generics_defaults_referential.py:78:63 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues) +generics_defaults_referential.py:69:40 - error: TypeVar default type must be a subtype of the bound type (reportGeneralTypeIssues) +generics_defaults_referential.py:75:52 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues) +generics_defaults_referential.py:79:63 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues) """ conformance_automated = "Pass" errors_diff = """ diff --git a/conformance/results/zuban/generics_defaults_referential.toml b/conformance/results/zuban/generics_defaults_referential.toml index 874e32116..097f66531 100644 --- a/conformance/results/zuban/generics_defaults_referential.toml +++ b/conformance/results/zuban/generics_defaults_referential.toml @@ -2,11 +2,11 @@ conformance_automated = "Pass" errors_diff = """ """ output = """ -generics_defaults_referential.py:36: error: Argument 2 to "Foo" has incompatible type "str"; expected "int" [arg-type] -generics_defaults_referential.py:37: error: Argument 1 to "Foo" has incompatible type "str"; expected "int" [arg-type] -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] -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] -generics_defaults_referential.py:68: error: TypeVar default must be a subtype of the bound type [misc] -generics_defaults_referential.py:74: error: TypeVar default must be one of the constraint types [misc] -generics_defaults_referential.py:78: error: TypeVar default must be one of the constraint types [misc] +generics_defaults_referential.py:37: error: Argument 2 to "Foo" has incompatible type "str"; expected "int" [arg-type] +generics_defaults_referential.py:38: error: Argument 1 to "Foo" has incompatible type "str"; expected "int" [arg-type] +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] +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] +generics_defaults_referential.py:69: error: TypeVar default must be a subtype of the bound type [misc] +generics_defaults_referential.py:75: error: TypeVar default must be one of the constraint types [misc] +generics_defaults_referential.py:79: error: TypeVar default must be one of the constraint types [misc] """ diff --git a/conformance/tests/generics_defaults_referential.py b/conformance/tests/generics_defaults_referential.py index 3ff4f1c8b..30c0281f6 100644 --- a/conformance/tests/generics_defaults_referential.py +++ b/conformance/tests/generics_defaults_referential.py @@ -32,9 +32,10 @@ class Foo(Generic[DefaultStrT, T2]): def __init__(self, a: DefaultStrT, b: T2) -> None: ... -assert_type(Foo(1, ""), Foo[int, str]) -Foo[int](1, "") # E: Foo[int, str] cannot be assigned to self: Foo[int, int] in Foo.__init__ -Foo[int]("", 1) # E: Foo[str, int] cannot be assigned to self: Foo[int, int] in Foo.__init__ +def func1(i: int, s: str) -> None: + assert_type(Foo(i, s), Foo[int, str]) + Foo[int](i, s) # E: Foo[int, str] cannot be assigned to self: Foo[int, int] in Foo.__init__ + Foo[int](s, i) # E: Foo[str, int] cannot be assigned to self: Foo[int, int] in Foo.__init__ # > ``T1`` must be used before ``T2`` in the parameter list of the generic. From e6fc261cce2b61631c0e182861e7ef5f260a639b Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 7 Mar 2026 12:17:40 -0800 Subject: [PATCH 2/2] update --- .../results/ty/generics_defaults_referential.toml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/conformance/results/ty/generics_defaults_referential.toml b/conformance/results/ty/generics_defaults_referential.toml index c2ad31d1e..a26c4aa54 100644 --- a/conformance/results/ty/generics_defaults_referential.toml +++ b/conformance/results/ty/generics_defaults_referential.toml @@ -2,11 +2,11 @@ conformance_automated = "Pass" errors_diff = """ """ output = """ -generics_defaults_referential.py:36:13: error[invalid-argument-type] Argument to bound method `__init__` is incorrect: Expected `int`, found `Literal[""]` -generics_defaults_referential.py:37:10: error[invalid-argument-type] Argument to bound method `__init__` is incorrect: Expected `int`, found `Literal[""]` -generics_defaults_referential.py:53:7: error[invalid-generic-class] Default of `Start2T` cannot reference out-of-scope type variable `StopT` -generics_defaults_referential.py:60:11: error[invalid-generic-class] Default of `S2` cannot reference out-of-scope type variable `S1` -generics_defaults_referential.py:68:40: error[invalid-type-variable-default] Default `X1` of TypeVar `Invalid1` is not assignable to upper bound `str` of `Invalid1` because its upper bound `int` is not assignable to `str` -generics_defaults_referential.py:74:52: error[invalid-type-variable-default] TypeVar default is inconsistent with the TypeVar's constraints: Bounded TypeVar cannot be used as the default for a constrained TypeVar -generics_defaults_referential.py:78:63: error[invalid-type-variable-default] Default `Y2` of TypeVar `AlsoInvalid2` is inconsistent with its constraints `AlsoInvalid2` because constraint `int` of `Y2` is not one of the constraints of `AlsoInvalid2` +generics_defaults_referential.py:37:17: error[invalid-argument-type] Argument to bound method `__init__` is incorrect: Expected `int`, found `str` +generics_defaults_referential.py:38:14: error[invalid-argument-type] Argument to bound method `__init__` is incorrect: Expected `int`, found `str` +generics_defaults_referential.py:54:7: error[invalid-generic-class] Default of `Start2T` cannot reference out-of-scope type variable `StopT` +generics_defaults_referential.py:61:11: error[invalid-generic-class] Default of `S2` cannot reference out-of-scope type variable `S1` +generics_defaults_referential.py:69:40: error[invalid-type-variable-default] Default `X1` of TypeVar `Invalid1` is not assignable to upper bound `str` of `Invalid1` because its upper bound `int` is not assignable to `str` +generics_defaults_referential.py:75:52: error[invalid-type-variable-default] TypeVar default is inconsistent with the TypeVar's constraints: Bounded TypeVar cannot be used as the default for a constrained TypeVar +generics_defaults_referential.py:79:63: error[invalid-type-variable-default] Default `Y2` of TypeVar `AlsoInvalid2` is inconsistent with its constraints `AlsoInvalid2` because constraint `int` of `Y2` is not one of the constraints of `AlsoInvalid2` """