forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pythoneval-asyncio.test
505 lines (426 loc) · 12.7 KB
/
pythoneval-asyncio.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
-- Test cases for type checking mypy programs using full stubs and running
-- using CPython.
--
-- These are mostly regression tests -- no attempt is made to make these
-- complete.
--
-- This test file check Asyncio and yield from interaction
[case testImportAsyncio]
import asyncio
print('Imported')
[out]
Imported
[case testSimpleCoroutineSleep]
from typing import Any, Generator
import asyncio
from asyncio import Future
@asyncio.coroutine
def greet_every_two_seconds() -> 'Generator[Any, None, None]':
n = 0
while n < 5:
print('Prev', n)
yield from asyncio.sleep(0.1)
print('After', n)
n += 1
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(greet_every_two_seconds())
finally:
loop.close()
[out]
Prev 0
After 0
Prev 1
After 1
Prev 2
After 2
Prev 3
After 3
Prev 4
After 4
[case testCoroutineCallingOtherCoroutine]
from typing import Generator, Any
import asyncio
from asyncio import Future
@asyncio.coroutine
def compute(x: int, y: int) -> 'Generator[Any, None, int]':
print("Compute %s + %s ..." % (x, y))
yield from asyncio.sleep(0.1)
return x + y # Here the int is wrapped in Future[int]
@asyncio.coroutine
def print_sum(x: int, y: int) -> 'Generator[Any, None, None]':
result = yield from compute(x, y) # The type of result will be int (is extracted from Future[int]
print("%s + %s = %s" % (x, y, result))
loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()
[out]
Compute 1 + 2 ...
1 + 2 = 3
[case testCoroutineChangingFuture]
from typing import Generator, Any
import asyncio
from asyncio import Future
@asyncio.coroutine
def slow_operation(future: 'Future[str]') -> 'Generator[Any, None, None]':
yield from asyncio.sleep(0.1)
future.set_result('Future is done!')
loop = asyncio.get_event_loop()
future = asyncio.Future() # type: Future[str]
asyncio.Task(slow_operation(future))
loop.run_until_complete(future)
print(future.result())
loop.close()
[out]
Future is done!
[case testFunctionAssignedAsCallback]
import typing
from typing import Generator, Any
import asyncio
from asyncio import Future, AbstractEventLoop
@asyncio.coroutine
def slow_operation(future: 'Future[str]') -> 'Generator[Any, None, None]':
yield from asyncio.sleep(1)
future.set_result('Callback works!')
def got_result(future: 'Future[str]') -> None:
print(future.result())
loop.stop()
loop = asyncio.get_event_loop() # type: AbstractEventLoop
future = asyncio.Future() # type: Future[str]
asyncio.Task(slow_operation(future)) # Here create a task with the function. (The Task need a Future[T] as first argument)
future.add_done_callback(got_result) # and assign the callback to the future
try:
loop.run_forever()
finally:
loop.close()
[out]
Callback works!
[case testMultipleTasks]
import typing
from typing import Generator, Any
import asyncio
from asyncio import Task, Future
@asyncio.coroutine
def factorial(name, number) -> 'Generator[Any, None, None]':
f = 1
for i in range(2, number+1):
print("Task %s: Compute factorial(%s)..." % (name, i))
yield from asyncio.sleep(0.1)
f *= i
print("Task %s: factorial(%s) = %s" % (name, number, f))
loop = asyncio.get_event_loop()
tasks = [
asyncio.Task(factorial("A", 2)),
asyncio.Task(factorial("B", 3)),
asyncio.Task(factorial("C", 4))]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
[out]
Task A: Compute factorial(2)...
Task B: Compute factorial(2)...
Task C: Compute factorial(2)...
Task A: factorial(2) = 2
Task B: Compute factorial(3)...
Task C: Compute factorial(3)...
Task B: factorial(3) = 6
Task C: Compute factorial(4)...
Task C: factorial(4) = 24
[case testConcatenatedCoroutines]
import typing
from typing import Generator, Any
import asyncio
from asyncio import Future
@asyncio.coroutine
def h4() -> 'Generator[Any, None, int]':
x = yield from future
return x
@asyncio.coroutine
def h3() -> 'Generator[Any, None, int]':
x = yield from h4()
print("h3: %s" % x)
return x
@asyncio.coroutine
def h2() -> 'Generator[Any, None, int]':
x = yield from h3()
print("h2: %s" % x)
return x
@asyncio.coroutine
def h() -> 'Generator[Any, None, None]':
x = yield from h2()
print("h: %s" % x)
loop = asyncio.get_event_loop()
future = asyncio.Future() # type: Future[int]
future.set_result(42)
loop.run_until_complete(h())
print("Outside %s" % future.result())
loop.close()
[out]
h3: 42
h2: 42
h: 42
Outside 42
[case testConcatenatedCoroutinesReturningFutures]
import typing
from typing import Generator, Any
import asyncio
from asyncio import Future
@asyncio.coroutine
def h4() -> 'Generator[Any, None, Future[int]]':
yield from asyncio.sleep(0.1)
f = asyncio.Future() #type: Future[int]
return f
@asyncio.coroutine
def h3() -> 'Generator[Any, None, Future[Future[int]]]':
x = yield from h4()
x.set_result(42)
f = asyncio.Future() #type: Future[Future[int]]
f.set_result(x)
return f
@asyncio.coroutine
def h() -> 'Generator[Any, None, None]':
print("Before")
x = yield from h3()
y = yield from x
z = yield from y
print(z)
def normalize(future):
# The str conversion seems inconsistent; not sure exactly why. Normalize
# the result.
return str(future).replace('<Future finished ', 'Future<')
print(normalize(y))
print(normalize(x))
loop = asyncio.get_event_loop()
loop.run_until_complete(h())
loop.close()
[out]
Before
42
Future<result=42>
Future<result=Future<result=42>>
[case testCoroutineWithOwnClass]
import typing
from typing import Generator, Any
import asyncio
from asyncio import Future
class A:
def __init__(self, x: int) -> None:
self.x = x
@asyncio.coroutine
def h() -> 'Generator[Any, None, None]':
x = yield from future
print("h: %s" % x.x)
loop = asyncio.get_event_loop()
future = asyncio.Future() # type: Future[A]
future.set_result(A(42))
loop.run_until_complete(h())
print("Outside %s" % future.result().x)
loop.close()
[out]
h: 42
Outside 42
-- Errors
[case testErrorAssigningCoroutineThatDontReturn]
from typing import Generator, Any
import asyncio
from asyncio import Future
@asyncio.coroutine
def greet() -> 'Generator[Any, None, None]':
yield from asyncio.sleep(0.2)
print('Hello World')
@asyncio.coroutine
def test() -> 'Generator[Any, None, None]':
yield from greet()
x = yield from greet() # Error
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(test())
finally:
loop.close()
[out]
_program.py:13: error: Function does not return a value
[case testErrorReturnIsNotTheSameType]
from typing import Generator, Any
import asyncio
from asyncio import Future
@asyncio.coroutine
def compute(x: int, y: int) -> 'Generator[Any, None, int]':
print("Compute %s + %s ..." % (x, y))
yield from asyncio.sleep(0.1)
return str(x + y) # Error
@asyncio.coroutine
def print_sum(x: int, y: int) -> 'Generator[Any, None, None]':
result = yield from compute(x, y)
print("%s + %s = %s" % (x, y, result))
loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()
[out]
_program.py:9: error: Incompatible return value type (got "str", expected "int")
[case testErrorSetFutureDifferentInternalType]
from typing import Generator, Any
import asyncio
from asyncio import Future
@asyncio.coroutine
def slow_operation(future: 'Future[str]') -> 'Generator[Any, None, None]':
yield from asyncio.sleep(1)
future.set_result(42) # Error
loop = asyncio.get_event_loop()
future = asyncio.Future() # type: Future[str]
asyncio.Task(slow_operation(future))
loop.run_until_complete(future)
print(future.result())
loop.close()
[out]
_program.py:8: error: Argument 1 to "set_result" of "Future" has incompatible type "int"; expected "str"
[case testErrorUsingDifferentFutureType]
from typing import Any, Generator
import asyncio
from asyncio import Future
@asyncio.coroutine
def slow_operation(future: 'Future[int]') -> 'Generator[Any, None, None]':
yield from asyncio.sleep(1)
future.set_result(42)
loop = asyncio.get_event_loop()
future = asyncio.Future() # type: Future[str]
asyncio.Task(slow_operation(future)) # Error
loop.run_until_complete(future)
print(future.result())
loop.close()
[out]
_program.py:12: error: Argument 1 to "slow_operation" has incompatible type "Future[str]"; expected "Future[int]"
[case testErrorUsingDifferentFutureTypeAndSetFutureDifferentInternalType]
from typing import Generator, Any
import asyncio
from asyncio import Future
@asyncio.coroutine
def slow_operation(future: 'Future[int]') -> 'Generator[Any, None, None]':
yield from asyncio.sleep(1)
future.set_result('42') #Try to set an str as result to a Future[int]
loop = asyncio.get_event_loop()
future = asyncio.Future() # type: Future[str]
asyncio.Task(slow_operation(future)) # Error
loop.run_until_complete(future)
print(future.result())
loop.close()
[out]
_program.py:8: error: Argument 1 to "set_result" of "Future" has incompatible type "str"; expected "int"
_program.py:12: error: Argument 1 to "slow_operation" has incompatible type "Future[str]"; expected "Future[int]"
[case testErrorSettingCallbackWithDifferentFutureType]
import typing
from typing import Generator, Any
import asyncio
from asyncio import Future, AbstractEventLoop
@asyncio.coroutine
def slow_operation(future: 'Future[str]') -> 'Generator[Any, None, None]':
yield from asyncio.sleep(1)
future.set_result('Future is done!')
def got_result(future: 'Future[int]') -> None:
print(future.result())
loop.stop()
loop = asyncio.get_event_loop() # type: AbstractEventLoop
future = asyncio.Future() # type: Future[str]
asyncio.Task(slow_operation(future))
future.add_done_callback(got_result) # Error
try:
loop.run_forever()
finally:
loop.close()
[out]
_program.py:18: error: Argument 1 to "add_done_callback" of "Future" has incompatible type "Callable[[Future[int]], None]"; expected "Callable[[Future[str]], Any]"
[case testErrorOneMoreFutureInReturnType]
import typing
from typing import Any, Generator
import asyncio
from asyncio import Future
@asyncio.coroutine
def h4() -> 'Generator[Any, None, Future[int]]':
yield from asyncio.sleep(1)
f = asyncio.Future() #type: Future[int]
return f
@asyncio.coroutine
def h3() -> 'Generator[Any, None, Future[Future[Future[int]]]]':
x = yield from h4()
x.set_result(42)
f = asyncio.Future() #type: Future[Future[int]]
f.set_result(x)
return f
@asyncio.coroutine
def h() -> 'Generator[Any, None, None]':
print("Before")
x = yield from h3()
y = yield from x
z = yield from y
print(z)
print(y)
print(x)
loop = asyncio.get_event_loop()
loop.run_until_complete(h())
loop.close()
[out]
_program.py:18: error: Incompatible return value type (got "Future[Future[int]]", expected "Future[Future[Future[int]]]")
[case testErrorOneLessFutureInReturnType]
import typing
from typing import Any, Generator
import asyncio
from asyncio import Future
@asyncio.coroutine
def h4() -> 'Generator[Any, None, Future[int]]':
yield from asyncio.sleep(1)
f = asyncio.Future() #type: Future[int]
return f
@asyncio.coroutine
def h3() -> 'Generator[Any, None, Future[int]]':
x = yield from h4()
x.set_result(42)
f = asyncio.Future() #type: Future[Future[int]]
f.set_result(x)
return f
@asyncio.coroutine
def h() -> 'Generator[Any, None, None]':
print("Before")
x = yield from h3()
y = yield from x
print(y)
print(x)
loop = asyncio.get_event_loop()
loop.run_until_complete(h())
loop.close()
[out]
_program.py:18: error: Incompatible return value type (got "Future[Future[int]]", expected "Future[int]")
[case testErrorAssignmentDifferentType]
import typing
from typing import Generator, Any
import asyncio
from asyncio import Future
class A:
def __init__(self, x: int) -> None:
self.x = x
class B:
def __init__(self, x: int) -> None:
self.x = x
@asyncio.coroutine
def h() -> 'Generator[Any, None, None]':
x = yield from future # type: B # Error
print("h: %s" % x.x)
loop = asyncio.get_event_loop()
future = asyncio.Future() # type: Future[A]
future.set_result(A(42))
loop.run_until_complete(h())
loop.close()
[out]
_program.py:16: error: Incompatible types in assignment (expression has type "A", variable has type "B")
[case testForwardRefToBadAsyncShouldNotCrash_newsemanal]
from typing import TypeVar
import asyncio
T = TypeVar('T')
P = whatever # type: ignore
def test() -> None:
reveal_type(bad)
bad(0)
@asyncio.coroutine
def bad(arg: P) -> T:
pass
[out]
_program.py:8: note: Revealed type is "def [T] (arg: P?) -> T`-1"
_program.py:12: error: Variable "_testForwardRefToBadAsyncShouldNotCrash_newsemanal.P" is not valid as a type
_program.py:12: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases