Skip to content

Commit 46cbe41

Browse files
committed
Conformance suite: rewrite several tests regarding type-parameter defaults to not assume that type checkers will infer a class object as having a type[] type
1 parent 5b5f2f8 commit 46cbe41

File tree

2 files changed

+99
-41
lines changed

2 files changed

+99
-41
lines changed

conformance/tests/generics_defaults.py

Lines changed: 90 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,87 @@
2424
class NonDefaultFollowsDefault(Generic[DefaultStrT, T]): ... # E: non-default TypeVars cannot follow ones with defaults
2525

2626

27-
class NoNonDefaults(Generic[DefaultStrT, DefaultIntT]): ...
27+
class NoNonDefaults(Generic[DefaultStrT, DefaultIntT]):
28+
x: DefaultStrT
29+
y: DefaultIntT
2830

2931

30-
assert_type(NoNonDefaults, type[NoNonDefaults[str, int]])
31-
assert_type(NoNonDefaults[str], type[NoNonDefaults[str, int]])
32-
assert_type(NoNonDefaults[str, int], type[NoNonDefaults[str, int]])
32+
def test_no_non_defaults(a: NoNonDefaults, b: NoNonDefaults[str], c: NoNonDefaults[str, int]):
33+
assert_type(a.x, str)
34+
assert_type(a.y, int)
3335

36+
assert_type(b.x, str)
37+
assert_type(b.y, int)
3438

35-
class OneDefault(Generic[T, DefaultBoolT]): ...
39+
assert_type(c.x, str)
40+
assert_type(c.y, int)
3641

3742

38-
assert_type(OneDefault[float], type[OneDefault[float, bool]])
39-
assert_type(OneDefault[float](), OneDefault[float, bool])
43+
class OneDefault(Generic[T, DefaultBoolT]):
44+
x: T
45+
y: DefaultBoolT
4046

4147

42-
class AllTheDefaults(Generic[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]): ...
48+
def test_one_default(a: OneDefault[float], b: OneDefault[float, bool]):
49+
assert_type(a.x, float)
50+
assert_type(a.y, bool)
4351

52+
assert_type(b.x, float)
53+
assert_type(b.y, bool)
4454

45-
assert_type(AllTheDefaults, type[AllTheDefaults[Any, Any, str, int, bool]])
46-
assert_type(
47-
AllTheDefaults[int, complex], type[AllTheDefaults[int, complex, str, int, bool]]
48-
)
4955

50-
AllTheDefaults[int] # E: expected 2 arguments to AllTheDefaults
56+
class AllTheDefaults(Generic[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]):
57+
x1: T1
58+
x2: T2
59+
x3: DefaultStrT
60+
x4: DefaultIntT
61+
x5: DefaultBoolT
5162

52-
assert_type(
53-
AllTheDefaults[int, complex], type[AllTheDefaults[int, complex, str, int, bool]]
54-
)
55-
assert_type(
56-
AllTheDefaults[int, complex, str],
57-
type[AllTheDefaults[int, complex, str, int, bool]],
58-
)
59-
assert_type(
60-
AllTheDefaults[int, complex, str, int],
61-
type[AllTheDefaults[int, complex, str, int, bool]],
62-
)
63-
assert_type(
64-
AllTheDefaults[int, complex, str, int, bool],
65-
type[AllTheDefaults[int, complex, str, int, bool]],
66-
)
63+
64+
def test_all_the_defaults(
65+
a: AllTheDefaults,
66+
b: AllTheDefaults[int], # E: expected 2 arguments to AllTheDefaults
67+
c: AllTheDefaults[int, complex],
68+
d: AllTheDefaults[int, complex, str, int, bool],
69+
e: AllTheDefaults[int, complex, str],
70+
f: AllTheDefaults[int, complex, str, int],
71+
g: AllTheDefaults[int, complex, str, int, bool],
72+
):
73+
assert_type(a.x1, Any)
74+
assert_type(a.x2, Any)
75+
assert_type(a.x3, str)
76+
assert_type(a.x4, int)
77+
assert_type(a.x5, bool)
78+
79+
assert_type(c.x1, int)
80+
assert_type(c.x2, complex)
81+
assert_type(c.x3, str)
82+
assert_type(c.x4, int)
83+
assert_type(c.x5, bool)
84+
85+
assert_type(d.x1, int)
86+
assert_type(d.x2, complex)
87+
assert_type(d.x3, str)
88+
assert_type(d.x4, int)
89+
assert_type(d.x5, bool)
90+
91+
assert_type(e.x1, int)
92+
assert_type(e.x2, complex)
93+
assert_type(e.x3, str)
94+
assert_type(e.x4, int)
95+
assert_type(e.x5, bool)
96+
97+
assert_type(f.x1, int)
98+
assert_type(f.x2, complex)
99+
assert_type(f.x3, str)
100+
assert_type(f.x4, int)
101+
assert_type(f.x5, bool)
102+
103+
assert_type(g.x1, int)
104+
assert_type(g.x2, complex)
105+
assert_type(g.x3, str)
106+
assert_type(g.x4, int)
107+
assert_type(g.x5, bool)
67108

68109

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

75116

76-
class Class_ParamSpec(Generic[DefaultP]): ...
117+
class Class_ParamSpec(Generic[DefaultP]):
118+
x: Callable[DefaultP, None]
77119

78120

79-
assert_type(Class_ParamSpec, type[Class_ParamSpec[str, int]])
80-
assert_type(Class_ParamSpec(), Class_ParamSpec[str, int])
81-
assert_type(Class_ParamSpec[[bool, bool]](), Class_ParamSpec[bool, bool])
121+
def test_param_spec_defaults(a: Class_ParamSpec):
122+
assert_type(a.x, Callable[[str, int], None])
123+
assert_type(Class_ParamSpec(), Class_ParamSpec[str, int])
124+
assert_type(Class_ParamSpec[[bool, bool]](), Class_ParamSpec[bool, bool])
82125

83126

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

90133

91-
class Class_TypeVarTuple(Generic[*DefaultTs]): ...
134+
class Class_TypeVarTuple(Generic[*DefaultTs]):
135+
x: tuple[*DefaultTs]
92136

93137

94-
assert_type(Class_TypeVarTuple, type[Class_TypeVarTuple[*tuple[str, int]]])
95-
assert_type(Class_TypeVarTuple(), Class_TypeVarTuple[str, int])
96-
assert_type(Class_TypeVarTuple[int, bool](), Class_TypeVarTuple[int, bool])
138+
def test_type_var_tuple_defaults(a: Class_TypeVarTuple):
139+
assert_type(a.x, tuple[str, int])
140+
assert_type(Class_TypeVarTuple(), Class_TypeVarTuple[str, int])
141+
assert_type(Class_TypeVarTuple[int, bool](), Class_TypeVarTuple[int, bool])
97142

98143

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

152197

153-
class Foo6(Generic[*Ts, P]): ... # OK
198+
class Foo6(Generic[*Ts, P]):
199+
x: tuple[*Ts]
200+
y: Callable[P, None]
201+
154202

203+
def test_foo6(a: Foo6[int, str], b: Foo6[int, str, [bytes]]):
204+
assert_type(a.x, tuple[int, str])
205+
assert_type(a.y, Callable[[float, bytes], None])
155206

156-
assert_type(Foo6[int, str], type[Foo6[int, str, [float, bool]]])
157-
assert_type(Foo6[int, str, [bytes]], type[Foo6[int, str, [bytes]]])
207+
assert_type(b.x, tuple[int, str])
208+
assert_type(b.y, Callable[[bytes], None])
158209

159210

160211
# > Type parameter defaults should be bound by attribute access

conformance/tests/generics_defaults_referential.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,18 @@ class Bar2(Generic[S2]): ... # E
8888

8989

9090
class Bar(Generic[Z1, ListDefaultT]): # OK
91+
x: Z1
92+
y: ListDefaultT
9193
def __init__(self, x: Z1, y: ListDefaultT): ...
9294

9395

94-
assert_type(Bar, type[Bar[Any, list[Any]]])
95-
assert_type(Bar[int], type[Bar[int, list[int]]])
96+
def f(b1: Bar, b2: Bar[int]):
97+
assert_type(b1.x, Any)
98+
assert_type(b1.y, list[Any])
99+
assert_type(b2.x, int)
100+
assert_type(b2.y, list[int])
101+
102+
96103
assert_type(Bar[int](0, []), Bar[int, list[int]])
97104
assert_type(Bar[int, list[str]](0, []), Bar[int, list[str]])
98105
assert_type(Bar[int, str](0, ""), Bar[int, str])

0 commit comments

Comments
 (0)