Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 13 additions & 31 deletions conformance/results/mypy/generics_defaults.toml
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
conformant = "Partial"
output = """
generics_defaults.py:24: error: "T" cannot appear after "DefaultStrT" in type parameter list because it has no default type [misc]
generics_defaults.py:30: error: Expression is of type "type[NoNonDefaults[DefaultStrT, DefaultIntT]]", not "type[NoNonDefaults[str, int]]" [assert-type]
generics_defaults.py:31: error: Expression is of type "type[NoNonDefaults[str, DefaultIntT]]", not "type[NoNonDefaults[str, int]]" [assert-type]
generics_defaults.py:38: error: Expression is of type "type[OneDefault[float, DefaultBoolT]]", not "type[OneDefault[float, bool]]" [assert-type]
generics_defaults.py:45: error: Expression is of type "type[AllTheDefaults[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]]", not "type[AllTheDefaults[Any, Any, str, int, bool]]" [assert-type]
generics_defaults.py:46: error: Expression is of type "type[AllTheDefaults[int, complex, DefaultStrT, DefaultIntT, DefaultBoolT]]", not "type[AllTheDefaults[int, complex, str, int, bool]]" [assert-type]
generics_defaults.py:50: error: Type application has too few types (expected between 2 and 5) [misc]
generics_defaults.py:52: error: Expression is of type "type[AllTheDefaults[int, complex, DefaultStrT, DefaultIntT, DefaultBoolT]]", not "type[AllTheDefaults[int, complex, str, int, bool]]" [assert-type]
generics_defaults.py:55: error: Expression is of type "type[AllTheDefaults[int, complex, str, DefaultIntT, DefaultBoolT]]", not "type[AllTheDefaults[int, complex, str, int, bool]]" [assert-type]
generics_defaults.py:59: error: Expression is of type "type[AllTheDefaults[int, complex, str, int, DefaultBoolT]]", not "type[AllTheDefaults[int, complex, str, int, bool]]" [assert-type]
generics_defaults.py:79: error: Expression is of type "type[Class_ParamSpec[DefaultP]]", not "type[Class_ParamSpec[[str, int]]]" [assert-type]
generics_defaults.py:94: error: Expression is of type "type[Class_TypeVarTuple[*DefaultTs]]", not "type[Class_TypeVarTuple[str, int]]" [assert-type]
generics_defaults.py:107: error: TypeVar default must be a subtype of the bound type [misc]
generics_defaults.py:114: error: TypeVar default must be one of the constraint types [misc]
generics_defaults.py:132: error: Expression is of type "int", not "Any" [assert-type]
generics_defaults.py:156: error: Expression is of type "type[Foo6[*tuple[Any, ...], Any]]", not "type[Foo6[int, str, [float, bool]]]" [assert-type]
generics_defaults.py:156: error: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "str" [misc]
generics_defaults.py:157: error: Expression is of type "type[Foo6[*tuple[Any, ...], Any]]", not "type[Foo6[int, str, [bytes]]]" [assert-type]
generics_defaults.py:157: error: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "str" [misc]
generics_defaults.py:66: error: "AllTheDefaults" expects between 2 and 5 type arguments, but 1 given [type-arg]
generics_defaults.py:139: error: Expression is of type "tuple[*tuple[str, int], ...]", not "tuple[str, int]" [assert-type]
generics_defaults.py:152: error: TypeVar default must be a subtype of the bound type [misc]
generics_defaults.py:159: error: TypeVar default must be one of the constraint types [misc]
generics_defaults.py:177: error: Expression is of type "int", not "Any" [assert-type]
generics_defaults.py:203: error: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "str" [valid-type]
generics_defaults.py:204: error: Expression is of type "tuple[Any, ...]", not "tuple[int, str]" [assert-type]
generics_defaults.py:205: error: Expression is of type "def (*Any, **Any) -> None", not "Callable[[float, bool], None]" [assert-type]
"""
conformance_automated = "Fail"
errors_diff = """
Line 143: Expected 1 errors
Line 30: Unexpected errors ['generics_defaults.py:30: error: Expression is of type "type[NoNonDefaults[DefaultStrT, DefaultIntT]]", not "type[NoNonDefaults[str, int]]" [assert-type]']
Line 31: Unexpected errors ['generics_defaults.py:31: error: Expression is of type "type[NoNonDefaults[str, DefaultIntT]]", not "type[NoNonDefaults[str, int]]" [assert-type]']
Line 38: Unexpected errors ['generics_defaults.py:38: error: Expression is of type "type[OneDefault[float, DefaultBoolT]]", not "type[OneDefault[float, bool]]" [assert-type]']
Line 45: Unexpected errors ['generics_defaults.py:45: error: Expression is of type "type[AllTheDefaults[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]]", not "type[AllTheDefaults[Any, Any, str, int, bool]]" [assert-type]']
Line 46: Unexpected errors ['generics_defaults.py:46: error: Expression is of type "type[AllTheDefaults[int, complex, DefaultStrT, DefaultIntT, DefaultBoolT]]", not "type[AllTheDefaults[int, complex, str, int, bool]]" [assert-type]']
Line 52: Unexpected errors ['generics_defaults.py:52: error: Expression is of type "type[AllTheDefaults[int, complex, DefaultStrT, DefaultIntT, DefaultBoolT]]", not "type[AllTheDefaults[int, complex, str, int, bool]]" [assert-type]']
Line 55: Unexpected errors ['generics_defaults.py:55: error: Expression is of type "type[AllTheDefaults[int, complex, str, DefaultIntT, DefaultBoolT]]", not "type[AllTheDefaults[int, complex, str, int, bool]]" [assert-type]']
Line 59: Unexpected errors ['generics_defaults.py:59: error: Expression is of type "type[AllTheDefaults[int, complex, str, int, DefaultBoolT]]", not "type[AllTheDefaults[int, complex, str, int, bool]]" [assert-type]']
Line 79: Unexpected errors ['generics_defaults.py:79: error: Expression is of type "type[Class_ParamSpec[DefaultP]]", not "type[Class_ParamSpec[[str, int]]]" [assert-type]']
Line 94: Unexpected errors ['generics_defaults.py:94: error: Expression is of type "type[Class_TypeVarTuple[*DefaultTs]]", not "type[Class_TypeVarTuple[str, int]]" [assert-type]']
Line 156: Unexpected errors ['generics_defaults.py:156: error: Expression is of type "type[Foo6[*tuple[Any, ...], Any]]", not "type[Foo6[int, str, [float, bool]]]" [assert-type]', 'generics_defaults.py:156: error: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "str" [misc]']
Line 157: Unexpected errors ['generics_defaults.py:157: error: Expression is of type "type[Foo6[*tuple[Any, ...], Any]]", not "type[Foo6[int, str, [bytes]]]" [assert-type]', 'generics_defaults.py:157: error: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "str" [misc]']
Line 188: Expected 1 errors
Line 139: Unexpected errors ['generics_defaults.py:139: error: Expression is of type "tuple[*tuple[str, int], ...]", not "tuple[str, int]" [assert-type]']
Line 203: Unexpected errors ['generics_defaults.py:203: error: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "str" [valid-type]']
Line 204: Unexpected errors ['generics_defaults.py:204: error: Expression is of type "tuple[Any, ...]", not "tuple[int, str]" [assert-type]']
Line 205: Unexpected errors ['generics_defaults.py:205: error: Expression is of type "def (*Any, **Any) -> None", not "Callable[[float, bool], None]" [assert-type]']
"""
8 changes: 2 additions & 6 deletions conformance/results/mypy/generics_defaults_referential.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ generics_defaults_referential.py:53: error: Type parameter "Start2T" has a defau
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:78: error: TypeVar default must be one of the constraint types [misc]
generics_defaults_referential.py:94: error: Expression is of type "type[Bar[Z1, ListDefaultT]]", not "type[Bar[Any, list[Any]]]" [assert-type]
generics_defaults_referential.py:95: error: Expression is of type "type[Bar[int, ListDefaultT]]", not "type[Bar[int, list[int]]]" [assert-type]
generics_defaults_referential.py:96: error: Expression is of type "Bar[int, list[Never]]", not "Bar[int, list[int]]" [assert-type]
generics_defaults_referential.py:103: error: Expression is of type "Bar[int, list[Never]]", not "Bar[int, list[int]]" [assert-type]
"""
conformance_automated = "Fail"
errors_diff = """
Expand All @@ -17,7 +15,5 @@ Line 60: Expected 1 errors
Line 68: 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 94: Unexpected errors ['generics_defaults_referential.py:94: error: Expression is of type "type[Bar[Z1, ListDefaultT]]", not "type[Bar[Any, list[Any]]]" [assert-type]']
Line 95: Unexpected errors ['generics_defaults_referential.py:95: error: Expression is of type "type[Bar[int, ListDefaultT]]", not "type[Bar[int, list[int]]]" [assert-type]']
Line 96: Unexpected errors ['generics_defaults_referential.py:96: error: Expression is of type "Bar[int, list[Never]]", not "Bar[int, list[int]]" [assert-type]']
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]']
"""
10 changes: 5 additions & 5 deletions conformance/results/pyrefly/generics_defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ errors_diff = """
"""
output = """
ERROR generics_defaults.py:24:7-31: Type parameter `T` without a default cannot follow type parameter `DefaultStrT` with a default [invalid-type-var]
ERROR generics_defaults.py:50:1-20: Expected 5 type arguments for `AllTheDefaults`, got 1 [bad-specialization]
ERROR generics_defaults.py:107:51-54: Expected default `int` of `Invalid1` to be assignable to the upper bound of `str` [invalid-type-var]
ERROR generics_defaults.py:114:52-55: Expected default `int` of `Invalid2` to be one of the following constraints: `float`, `str` [invalid-type-var]
ERROR generics_defaults.py:132:12-27: assert_type(int, Any) failed [assert-type]
ERROR generics_defaults.py:143:7-11: TypeVar `T5` with a default cannot follow TypeVarTuple `Ts` [invalid-type-var]
ERROR generics_defaults.py:66:8-27: Expected 5 type arguments for `AllTheDefaults`, got 1 [bad-specialization]
ERROR generics_defaults.py:152:51-54: Expected default `int` of `Invalid1` to be assignable to the upper bound of `str` [invalid-type-var]
ERROR generics_defaults.py:159:52-55: Expected default `int` of `Invalid2` to be one of the following constraints: `float`, `str` [invalid-type-var]
ERROR generics_defaults.py:177:12-27: assert_type(int, Any) failed [assert-type]
ERROR generics_defaults.py:188:7-11: TypeVar `T5` with a default cannot follow TypeVarTuple `Ts` [invalid-type-var]
"""
10 changes: 5 additions & 5 deletions conformance/results/pyright/generics_defaults.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
conformant = "Pass"
output = """
generics_defaults.py:24:7 - error: "T" cannot appear after "DefaultStrT" in type parameter list because it has no default type (reportGeneralTypeIssues)
generics_defaults.py:50:16 - error: Too few type arguments provided for "AllTheDefaults"; expected 2 but received 1 (reportInvalidTypeArguments)
generics_defaults.py:107:51 - error: TypeVar default type must be a subtype of the bound type (reportGeneralTypeIssues)
generics_defaults.py:114:52 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues)
generics_defaults.py:132:13 - error: "assert_type" mismatch: expected "Any" but received "int" (reportAssertTypeFailure)
generics_defaults.py:143:7 - error: TypeVar "T5" has a default value and cannot follow TypeVarTuple "Ts" (reportGeneralTypeIssues)
generics_defaults.py:66:23 - error: Too few type arguments provided for "AllTheDefaults"; expected 2 but received 1 (reportInvalidTypeArguments)
generics_defaults.py:152:51 - error: TypeVar default type must be a subtype of the bound type (reportGeneralTypeIssues)
generics_defaults.py:159:52 - error: TypeVar default type must be one of the constrained types (reportGeneralTypeIssues)
generics_defaults.py:177:13 - error: "assert_type" mismatch: expected "Any" but received "int" (reportAssertTypeFailure)
generics_defaults.py:188:7 - error: TypeVar "T5" has a default value and cannot follow TypeVarTuple "Ts" (reportGeneralTypeIssues)
"""
conformance_automated = "Pass"
errors_diff = """
Expand Down
10 changes: 5 additions & 5 deletions conformance/results/zuban/generics_defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ errors_diff = """
"""
output = """
generics_defaults.py:24: error: "T" cannot appear after "DefaultStrT" in type parameter list because it has no default type [misc]
generics_defaults.py:50: error: "AllTheDefaults" expects between 2 and 5 type arguments, but 1 given [type-arg]
generics_defaults.py:107: error: TypeVar default must be a subtype of the bound type [misc]
generics_defaults.py:114: error: TypeVar default must be one of the constraint types [misc]
generics_defaults.py:132: error: Expression is of type "int", not "Any" [misc]
generics_defaults.py:143: error: TypeVar defaults are ambiguous after a TypeVarTuple [misc]
generics_defaults.py:66: error: "AllTheDefaults" expects between 2 and 5 type arguments, but 1 given [type-arg]
generics_defaults.py:152: error: TypeVar default must be a subtype of the bound type [misc]
generics_defaults.py:159: error: TypeVar default must be one of the constraint types [misc]
generics_defaults.py:177: error: Expression is of type "int", not "Any" [misc]
generics_defaults.py:188: error: TypeVar defaults are ambiguous after a TypeVarTuple [misc]
"""
129 changes: 90 additions & 39 deletions conformance/tests/generics_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,87 @@
class NonDefaultFollowsDefault(Generic[DefaultStrT, T]): ... # E: non-default TypeVars cannot follow ones with defaults


class NoNonDefaults(Generic[DefaultStrT, DefaultIntT]): ...
class NoNonDefaults(Generic[DefaultStrT, DefaultIntT]):
x: DefaultStrT
y: DefaultIntT


assert_type(NoNonDefaults, type[NoNonDefaults[str, int]])
assert_type(NoNonDefaults[str], type[NoNonDefaults[str, int]])
assert_type(NoNonDefaults[str, int], type[NoNonDefaults[str, int]])
def test_no_non_defaults(a: NoNonDefaults, b: NoNonDefaults[str], c: NoNonDefaults[str, int]):
assert_type(a.x, str)
assert_type(a.y, int)

assert_type(b.x, str)
assert_type(b.y, int)

class OneDefault(Generic[T, DefaultBoolT]): ...
assert_type(c.x, str)
assert_type(c.y, int)


assert_type(OneDefault[float], type[OneDefault[float, bool]])
assert_type(OneDefault[float](), OneDefault[float, bool])
class OneDefault(Generic[T, DefaultBoolT]):
x: T
y: DefaultBoolT


class AllTheDefaults(Generic[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]): ...
def test_one_default(a: OneDefault[float], b: OneDefault[float, bool]):
assert_type(a.x, float)
assert_type(a.y, bool)

assert_type(b.x, float)
assert_type(b.y, bool)

assert_type(AllTheDefaults, type[AllTheDefaults[Any, Any, str, int, bool]])
assert_type(
AllTheDefaults[int, complex], type[AllTheDefaults[int, complex, str, int, bool]]
)

AllTheDefaults[int] # E: expected 2 arguments to AllTheDefaults
class AllTheDefaults(Generic[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]):
x1: T1
x2: T2
x3: DefaultStrT
x4: DefaultIntT
x5: DefaultBoolT

assert_type(
AllTheDefaults[int, complex], type[AllTheDefaults[int, complex, str, int, bool]]
)
assert_type(
AllTheDefaults[int, complex, str],
type[AllTheDefaults[int, complex, str, int, bool]],
)
assert_type(
AllTheDefaults[int, complex, str, int],
type[AllTheDefaults[int, complex, str, int, bool]],
)
assert_type(
AllTheDefaults[int, complex, str, int, bool],
type[AllTheDefaults[int, complex, str, int, bool]],
)

def test_all_the_defaults(
a: AllTheDefaults,
b: AllTheDefaults[int], # E: expected 2 arguments to AllTheDefaults
c: AllTheDefaults[int, complex],
d: AllTheDefaults[int, complex, str, int, bool],
e: AllTheDefaults[int, complex, str],
f: AllTheDefaults[int, complex, str, int],
g: AllTheDefaults[int, complex, str, int, bool],
):
assert_type(a.x1, Any)
assert_type(a.x2, Any)
assert_type(a.x3, str)
assert_type(a.x4, int)
assert_type(a.x5, bool)

assert_type(c.x1, int)
assert_type(c.x2, complex)
assert_type(c.x3, str)
assert_type(c.x4, int)
assert_type(c.x5, bool)

assert_type(d.x1, int)
assert_type(d.x2, complex)
assert_type(d.x3, str)
assert_type(d.x4, int)
assert_type(d.x5, bool)

assert_type(e.x1, int)
assert_type(e.x2, complex)
assert_type(e.x3, str)
assert_type(e.x4, int)
assert_type(e.x5, bool)

assert_type(f.x1, int)
assert_type(f.x2, complex)
assert_type(f.x3, str)
assert_type(f.x4, int)
assert_type(f.x5, bool)

assert_type(g.x1, int)
assert_type(g.x2, complex)
assert_type(g.x3, str)
assert_type(g.x4, int)
assert_type(g.x5, bool)


# > ``ParamSpec`` defaults are defined using the same syntax as
Expand All @@ -73,12 +114,14 @@ class AllTheDefaults(Generic[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]): .
DefaultP = ParamSpec("DefaultP", default=[str, int])


class Class_ParamSpec(Generic[DefaultP]): ...
class Class_ParamSpec(Generic[DefaultP]):
x: Callable[DefaultP, None]


assert_type(Class_ParamSpec, type[Class_ParamSpec[str, int]])
assert_type(Class_ParamSpec(), Class_ParamSpec[str, int])
assert_type(Class_ParamSpec[[bool, bool]](), Class_ParamSpec[bool, bool])
def test_param_spec_defaults(a: Class_ParamSpec):
assert_type(a.x, Callable[[str, int], None])
assert_type(Class_ParamSpec(), Class_ParamSpec[str, int])
assert_type(Class_ParamSpec[[bool, bool]](), Class_ParamSpec[bool, bool])


# > ``TypeVarTuple`` defaults are defined using the same syntax as
Expand All @@ -88,12 +131,14 @@ class Class_ParamSpec(Generic[DefaultP]): ...
DefaultTs = TypeVarTuple("DefaultTs", default=Unpack[tuple[str, int]])


class Class_TypeVarTuple(Generic[*DefaultTs]): ...
class Class_TypeVarTuple(Generic[*DefaultTs]):
x: tuple[*DefaultTs]


assert_type(Class_TypeVarTuple, type[Class_TypeVarTuple[*tuple[str, int]]])
assert_type(Class_TypeVarTuple(), Class_TypeVarTuple[str, int])
assert_type(Class_TypeVarTuple[int, bool](), Class_TypeVarTuple[int, bool])
def test_type_var_tuple_defaults(a: Class_TypeVarTuple):
assert_type(a.x, tuple[str, int])
assert_type(Class_TypeVarTuple(), Class_TypeVarTuple[str, int])
assert_type(Class_TypeVarTuple[int, bool](), Class_TypeVarTuple[int, bool])


AnotherDefaultTs = TypeVarTuple("AnotherDefaultTs", default=Unpack[DefaultTs])
Expand Down Expand Up @@ -150,11 +195,17 @@ class Foo5(Generic[*Ts, T5]): ... # E
P = ParamSpec("P", default=[float, bool])


class Foo6(Generic[*Ts, P]): ... # OK
class Foo6(Generic[*Ts, P]):
x: tuple[*Ts]
y: Callable[P, None]


def test_foo6(a: Foo6[int, str], b: Foo6[int, str, [bytes]]):
assert_type(a.x, tuple[int, str])
assert_type(a.y, Callable[[float, bool], None])

assert_type(Foo6[int, str], type[Foo6[int, str, [float, bool]]])
assert_type(Foo6[int, str, [bytes]], type[Foo6[int, str, [bytes]]])
assert_type(b.x, tuple[int, str])
assert_type(b.y, Callable[[bytes], None])


# > Type parameter defaults should be bound by attribute access
Expand Down
11 changes: 9 additions & 2 deletions conformance/tests/generics_defaults_referential.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,18 @@ class Bar2(Generic[S2]): ... # E


class Bar(Generic[Z1, ListDefaultT]): # OK
x: Z1
y: ListDefaultT
def __init__(self, x: Z1, y: ListDefaultT): ...


assert_type(Bar, type[Bar[Any, list[Any]]])
assert_type(Bar[int], type[Bar[int, list[int]]])
def f(b1: Bar, b2: Bar[int]):
assert_type(b1.x, Any)
assert_type(b1.y, list[Any])
assert_type(b2.x, int)
assert_type(b2.y, list[int])


assert_type(Bar[int](0, []), Bar[int, list[int]])
assert_type(Bar[int, list[str]](0, []), Bar[int, list[str]])
assert_type(Bar[int, str](0, ""), Bar[int, str])