forked from python/mypy
/
check-bound.test
227 lines (178 loc) · 4.71 KB
/
check-bound.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
-- Enforcement of upper bounds
-- ---------------------------
[case testBoundOnGenericFunction]
from typing import TypeVar
class A: pass
class B(A): pass
class C(A): pass
class D: pass
T = TypeVar('T', bound=A)
U = TypeVar('U')
def f(x: T) -> T: pass
def g(x: U) -> U:
return f(x) # E: Value of type variable "T" of "f" cannot be "U"
f(A())
f(B())
f(D()) # E: Value of type variable "T" of "f" cannot be "D"
b = B()
if int():
b = f(b)
if int():
b = f(C()) # E: Incompatible types in assignment (expression has type "C", variable has type "B")
[case testBoundOnGenericClass]
from typing import TypeVar, Generic
class A: pass
class B(A): pass
T = TypeVar('T', bound=A)
class G(Generic[T]):
def __init__(self, x: T) -> None: pass
v = None # type: G[A]
w = None # type: G[B]
x = None # type: G[str] # E: Type argument "str" of "G" must be a subtype of "A"
y = G('a') # E: Value of type variable "T" of "G" cannot be "str"
z = G(A())
z = G(B())
[case testBoundVoid]
from typing import TypeVar, Generic
T = TypeVar('T', bound=int)
class C(Generic[T]):
t = None # type: T
def get(self) -> T:
return self.t
c1 = None # type: C[None]
c1.get()
d = c1.get()
reveal_type(d) # N: Revealed type is "None"
[case testBoundAny]
from typing import TypeVar, Generic
T = TypeVar('T', bound=int)
class C(Generic[T]):
def __init__(self, x: T) -> None: pass
def f(x: T) -> T:
return x
def g(): pass
f(g())
C(g())
z = None # type: C
[case testBoundHigherOrderWithVoid]
from typing import TypeVar, Callable
class A: pass
T = TypeVar('T', bound=A)
def f(g: Callable[[], T]) -> T:
return g()
def h() -> None: pass
f(h)
a = f(h)
reveal_type(a) # N: Revealed type is "None"
[case testBoundInheritance]
from typing import TypeVar, Generic
class A: pass
T = TypeVar('T')
TA = TypeVar('TA', bound=A)
class C(Generic[TA]): pass
class D0(C[TA], Generic[TA]): pass
class D1(C[T], Generic[T]): pass # E: Type argument "T" of "C" must be a subtype of "A"
class D2(C[A]): pass
class D3(C[str]): pass # E: Type argument "str" of "C" must be a subtype of "A"
-- Using information from upper bounds
-- -----------------------------------
[case testBoundGenericFunctions]
from typing import TypeVar
class A: pass
class B(A): pass
T = TypeVar('T')
TA = TypeVar('TA', bound=A)
TB = TypeVar('TB', bound=B)
def f(x: T) -> T:
return x
def g(x: TA) -> TA:
return f(x)
def h(x: TB) -> TB:
return g(x)
def g2(x: TA) -> TA:
return h(x) # Fail
def j(x: TA) -> A:
return x
def k(x: TA) -> B:
return x # Fail
[out]
main:16: error: Value of type variable "TB" of "h" cannot be "TA"
main:21: error: Incompatible return value type (got "TA", expected "B")
[case testBoundMethodUsage]
from typing import TypeVar
class A0:
def foo(self) -> None: pass
class A(A0):
def bar(self) -> None: pass
a = 1
@property
def b(self) -> int:
return self.a
class B(A):
def baz(self) -> None: pass
T = TypeVar('T', bound=A)
def f(x: T) -> T:
x.foo()
x.bar()
x.baz() # E: "T" has no attribute "baz"
x.a
x.b
return x
b = f(B())
[builtins fixtures/property.pyi]
[out]
[case testBoundClassMethod]
from typing import TypeVar
class A0:
@classmethod
def foo(cls, x: int) -> int: pass
class A(A0): pass
T = TypeVar('T', bound=A)
def f(x: T) -> int:
return x.foo(22)
[builtins fixtures/classmethod.pyi]
[case testBoundClassMethodWithNamedTupleBase]
from typing import NamedTuple, Type, TypeVar
class A(NamedTuple):
@classmethod
def foo(cls) -> None: ...
T = TypeVar('T', bound=A)
def f(x: Type[T]) -> None:
reveal_type(x.foo) # N: Revealed type is "def ()"
x.foo()
[builtins fixtures/classmethod.pyi]
[case testBoundStaticMethod]
from typing import TypeVar
class A0:
@staticmethod
def foo(x: int) -> int: pass
class A(A0): pass
T = TypeVar('T', bound=A)
def f(x: T) -> int:
return x.foo(22)
[builtins fixtures/staticmethod.pyi]
[case testBoundOnDecorator]
from typing import TypeVar, Callable, Any, cast
T = TypeVar('T', bound=Callable[..., Any])
def twice(f: T) -> T:
def result(*args, **kwargs) -> Any:
f(*args, **kwargs)
return f(*args, **kwargs)
return cast(T, result)
@twice
def foo(x: int) -> int:
return x
a = 1
b = foo(a)
if int():
b = 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
twice(a) # E: Value of type variable "T" of "twice" cannot be "int"
[builtins fixtures/args.pyi]
[case testIterableBoundUnpacking]
from typing import Tuple, TypeVar
TupleT = TypeVar("TupleT", bound=Tuple[int, ...])
def f(t: TupleT) -> None:
a, *b = t
reveal_type(a) # N: Revealed type is "builtins.int"
reveal_type(b) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/tuple.pyi]