Debian Codegraph @

mypyc/test-data/run-traits.test

raw
1 [case testTraitBasic1]
2 from mypy_extensions import trait
3
4 class A:
5 line: int
6 def foo(self) -> None:
7 print("foo")
8
9 @trait
10 class T:
11 def bar(self) -> None:
12 print("bar")
13 def baz(self) -> object:
14 return None
15
16 class C(A, T):
17 def baz(self) -> int:
18 return 10
19
20 def use_t(t: T) -> object:
21 t.bar()
22 return t.baz()
23
24 def use_c(c: C) -> int:
25 use_t(c)
26 c.foo()
27 c.bar()
28 return c.baz()
29
30 use_t(C())
31
32 # This trait is dead code but there's no reason it shouldn't compile
33 @trait
34 class ChildlessTrait:
35 def __init__(self) -> None:
36 pass
37
38 [file driver.py]
39 from native import A, T, C, use_c, use_t
40 c = C()
41 c.foo()
42 c.bar()
43 assert c.baz() == 10
44 assert use_c(c) == 10
45 assert use_t(c) == 10
46 [out]
47 bar
48 foo
49 bar
50 bar
51 foo
52 bar
53 bar
54
55 [case testTraitBasic2]
56 from mypy_extensions import trait
57
58 class A:
59 line: int
60 def foo(self) -> None:
61 print("foo")
62
63 @trait
64 class T:
65 def bar(self) -> None:
66 print("bar", self.baz())
67 def baz(self) -> int:
68 return -1
69
70 @trait
71 class T2:
72 line: int
73 def baz(self) -> int:
74 return -2
75
76 class C(A, T):
77 def __init__(self) -> None:
78 self.line = 1337
79 self.x = 12
80 def baz(self) -> int:
81 return self.x
82
83 class D(C, T2):
84 def __init__(self) -> None:
85 self.line = 1337
86 self.x = 13
87
88 @trait
89 class T3:
90 def baz(self) -> int:
91 return -2
92
93 class E(T3):
94 def __init__(self) -> None:
95 pass
96
97
98 def use_t(t: T) -> None:
99 t.bar()
100 def use_t2(t: T2) -> int:
101 t.line = t.line
102 return t.line
103
104 def use_c(c: C) -> int:
105 use_t(c)
106 c.foo()
107 c.bar()
108 return c.line
109
110 def use_d(d: D) -> int:
111 return d.baz()
112
113 [file driver.py]
114 from native import A, T, C, D, use_c, use_t, use_d, use_t2
115 c = C()
116 d = D()
117 c.foo()
118 c.bar()
119 print("baz", c.baz())
120 print("baz", d.baz())
121 use_c(c)
122 use_t(c)
123 use_c(d)
124 use_t(d)
125 assert use_d(d) == 13
126 print(d.line)
127 assert d.line == 1337
128 assert use_t2(d) == 1337
129 [out]
130 foo
131 bar 12
132 baz 12
133 baz 13
134 bar 12
135 foo
136 bar 12
137 bar 12
138 bar 13
139 foo
140 bar 13
141 bar 13
142 1337
143
144 [case testTrait3]
145 from mypy_extensions import trait
146 from typing import Generic, TypeVar
147
148 @trait
149 class T1: pass
150 @trait
151 class T2: pass
152
153 T = TypeVar('T')
154
155 class C(Generic[T], T1, T2):
156 pass
157
158 @trait
159 class S1(Generic[T]):
160 def foo(self) -> None: pass
161 def bar(self, x: T) -> T: raise Exception
162
163 @trait
164 class S2(S1[T]):
165 def bar(self, x: T) -> T: return x
166
167 @trait
168 class S3(S2[T]):
169 def bar(self, x: T) -> T: return x
170
171 class D(S3[bool]):
172 def bar(self, x: bool) -> bool: return x
173
174
175 [file driver.py]
176 import native
177
178 [case testTrait4]
179 from mypy_extensions import trait
180 from typing import Generic, TypeVar
181
182 T = TypeVar('T')
183
184
185 @trait
186 class S1(Generic[T]):
187 def bar(self) -> T: raise Exception
188
189 class S2(S1[bool]):
190 def bar(self) -> bool: return False
191
192 class D(S2):
193 pass
194
195 def lol(x: S1) -> None:
196 x.bar()
197
198 [file driver.py]
199 import native
200 native.lol(native.D())
201
202 [case testTraitOrdering]
203 import other_b
204 # Regression test for a bug where inheriting from a class that
205 # inherited from a trait that got processed later on the command line
206 # filed to compile.
207 [file other_b.py]
208 from other_c import Plugin
209
210 class Whatever(Plugin):
211 pass
212
213 [file other_c.py]
214 from mypy_extensions import trait
215
216 @trait
217 class Base:
218 x: int
219
220 class Plugin(Base):
221 def __init__(self) -> None:
222 self.x = 10
223
224 [file driver.py]
225 from native import *
226
227 [case testDiamond]
228 from mypy_extensions import trait
229
230 @trait
231 class Base:
232 def get_value(self) -> str:
233 return "Base"
234
235 @trait
236 class Trait(Base):
237 def get_value(self) -> str:
238 return "Trait"
239
240 class Message(Base):
241 def show_message(self) -> None:
242 print("I am a " + self.get_value())
243
244 class DerivedMessage(Message, Trait):
245 pass
246
247 [file driver.py]
248 from native import *
249 a = Message()
250 a.show_message()
251 b = DerivedMessage()
252 b.show_message()
253
254 [out]
255 I am a Base
256 I am a Trait
257
258 [case testTraitAttrsSubTrait]
259 from mypy_extensions import trait
260
261 class A:
262 a: int
263
264 @trait
265 class T1:
266 x: int
267
268 @trait
269 class T2(T1):
270 y: int
271
272 class C(A, T2):
273 c: int
274
275 def f(t1: T1, t2: T2) -> None:
276 t1.x, t2.x = t2.x, t1.x
277
278 def g(t1: T1, t2: T2) -> None:
279 t2.y = t1.x
280
281 def get_x(c: C) -> int:
282 return c.x
283
284 def get_y(c: C) -> int:
285 return c.y
286
287 [file driver.py]
288 from native import C, f, g, get_x, get_y
289
290 c1 = C()
291 c2 = C()
292
293 c1.x = 1
294 c1.y = 0
295 c2.x = 2
296 c2.y = 0
297
298 f(c1, c2)
299 assert c1.x == 2
300 assert c2.x == 1
301 assert get_x(c1) == 2
302 assert get_x(c2) == 1
303
304 assert get_y(c2) == 0
305 g(c1, c2)
306 assert get_y(c2) == 2
307 [out]
308
309 [case testTraitAttrsTriangle]
310 from mypy_extensions import trait
311
312 class A:
313 a: int
314
315 @trait
316 class T(A):
317 x: int
318 def meth(self) -> int:
319 return self.a
320
321 class B(A):
322 b: int
323
324 class C(B, T):
325 pass
326
327 def take_t(t: T) -> int:
328 return t.x + t.meth()
329
330 def take_c(c: C) -> int:
331 return c.x + c.meth()
332
333 [file driver.py]
334 from native import C, take_t, take_c
335
336 c = C()
337 c.a = 1
338 c.x = 10
339
340 assert take_t(c) == take_c(c) == 11
341 [out]
342
343 [case testTraitAttrsTree]
344 from mypy_extensions import trait
345
346 class A:
347 a: int
348
349 @trait
350 class T1:
351 x: int
352
353 class B(A, T1):
354 b: int
355
356 @trait
357 class T2:
358 x: int
359
360 class C(B, T2):
361 pass
362
363 def f(t1: T1, t2: T2) -> None:
364 t1.x, t2.x = t2.x, t1.x
365
366 def g(c1: C, c2: C) -> None:
367 c1.x, c2.x = c2.x, c1.x
368
369 [file driver.py]
370 from native import C, f, g
371
372 c1 = C()
373 c2 = C()
374
375 c1.x = 1
376 c2.x = 2
377
378 f(c1, c2)
379 assert c1.x == 2
380 assert c2.x == 1
381
382 g(c1, c2)
383 assert c1.x == 1
384 assert c2.x == 2
385 [out]
386
387 [case testTraitErrorMessages]
388 from mypy_extensions import trait
389
390 @trait
391 class Trait:
392 pass
393
394 def create() -> Trait:
395 return Trait()
396
397 [file driver.py]
398 from native import Trait, create
399 from testutil import assertRaises
400
401 with assertRaises(TypeError, "traits may not be directly created"):
402 Trait()
403
404 with assertRaises(TypeError, "traits may not be directly created"):
405 create()
406
407 class Sub(Trait):
408 pass
409
410 with assertRaises(TypeError, "interpreted classes cannot inherit from compiled traits"):
411 Sub()