@@ -25,11 +25,7 @@ async def greet_every_two_seconds() -> None:
25
25
print('After', n)
26
26
n += 1
27
27
28
- loop = asyncio.get_event_loop()
29
- try:
30
- loop.run_until_complete(greet_every_two_seconds())
31
- finally:
32
- loop.close()
28
+ asyncio.run(greet_every_two_seconds())
33
29
[out]
34
30
Prev 0
35
31
After 0
@@ -56,9 +52,7 @@ async def print_sum(x: int, y: int) -> None:
56
52
result = await compute(x, y) # The type of result will be int (is extracted from Future[int]
57
53
print("%s + %s = %s" % (x, y, result))
58
54
59
- loop = asyncio.get_event_loop()
60
- loop.run_until_complete(print_sum(1, 2))
61
- loop.close()
55
+ asyncio.run(print_sum(1, 2))
62
56
[out]
63
57
Compute 1 + 2 ...
64
58
1 + 2 = 3
@@ -72,12 +66,13 @@ async def slow_operation(future: 'Future[str]') -> None:
72
66
await asyncio.sleep(0.01)
73
67
future.set_result('Future is done!')
74
68
75
- loop = asyncio.get_event_loop()
76
- future = asyncio.Future() # type: Future[str]
77
- asyncio.Task(slow_operation(future))
78
- loop.run_until_complete(future)
79
- print(future.result())
80
- loop.close()
69
+ async def main() -> None:
70
+ future = asyncio.Future() # type: Future[str]
71
+ asyncio.Task(slow_operation(future))
72
+ await future
73
+ print(future.result())
74
+
75
+ asyncio.run(main())
81
76
[out]
82
77
Future is done!
83
78
@@ -95,10 +90,13 @@ def got_result(future: 'Future[str]') -> None:
95
90
print(future.result())
96
91
loop.stop()
97
92
98
- loop = asyncio.get_event_loop() # type: AbstractEventLoop
99
- future = asyncio.Future() # type: Future[str]
100
- asyncio.Task(slow_operation(future)) # Here create a task with the function. (The Task need a Future[T] as first argument)
101
- future.add_done_callback(got_result) # and assign the callback to the future
93
+ async def main() -> None:
94
+ future = asyncio.Future() # type: Future[str]
95
+ asyncio.Task(slow_operation(future)) # Here create a task with the function. (The Task need a Future[T] as first argument)
96
+ future.add_done_callback(got_result) # and assign the callback to the future
97
+
98
+ loop = asyncio.new_event_loop() # type: AbstractEventLoop
99
+ loop.run_until_complete(main())
102
100
try:
103
101
loop.run_forever()
104
102
finally:
@@ -119,13 +117,14 @@ async def factorial(name, number) -> None:
119
117
f *= i
120
118
print("Task %s: factorial(%s) = %s" % (name, number, f))
121
119
122
- loop = asyncio.get_event_loop()
123
- tasks = [
124
- asyncio.Task(factorial("A", 2)),
125
- asyncio.Task(factorial("B", 3)),
126
- asyncio.Task(factorial("C", 4))]
127
- loop.run_until_complete(asyncio.wait(tasks))
128
- loop.close()
120
+ async def main() -> None:
121
+ tasks = [
122
+ asyncio.Task(factorial("A", 2)),
123
+ asyncio.Task(factorial("B", 3)),
124
+ asyncio.Task(factorial("C", 4))]
125
+ await asyncio.wait(tasks)
126
+
127
+ asyncio.run(main())
129
128
[out]
130
129
Task A: Compute factorial(2)...
131
130
Task B: Compute factorial(2)...
@@ -144,6 +143,8 @@ from typing import Any
144
143
import asyncio
145
144
from asyncio import Future
146
145
146
+ future: Future[int]
147
+
147
148
async def h4() -> int:
148
149
x = await future
149
150
return x
@@ -162,12 +163,14 @@ async def h() -> None:
162
163
x = await h2()
163
164
print("h: %s" % x)
164
165
165
- loop = asyncio.get_event_loop()
166
- future = asyncio.Future() # type: Future[int]
167
- future.set_result(42)
168
- loop.run_until_complete(h())
169
- print("Outside %s" % future.result())
170
- loop.close()
166
+ async def main() -> None:
167
+ global future
168
+ future = asyncio.Future()
169
+ future.set_result(42)
170
+ await h()
171
+ print("Outside %s" % future.result())
172
+
173
+ asyncio.run(main())
171
174
[out]
172
175
h3: 42
173
176
h2: 42
@@ -182,13 +185,13 @@ from asyncio import Future
182
185
183
186
async def h4() -> "Future[int]":
184
187
await asyncio.sleep(0.01)
185
- f = asyncio.Future() # type: Future[int]
188
+ f = asyncio.Future() # type: Future[int]
186
189
return f
187
190
188
191
async def h3() -> "Future[Future[int]]":
189
192
x = await h4()
190
193
x.set_result(42)
191
- f = asyncio.Future() # type: Future[Future[int]]
194
+ f = asyncio.Future() # type: Future[Future[int]]
192
195
f.set_result(x)
193
196
return f
194
197
@@ -205,9 +208,7 @@ async def h() -> None:
205
208
print(normalize(y))
206
209
print(normalize(x))
207
210
208
- loop = asyncio.get_event_loop()
209
- loop.run_until_complete(h())
210
- loop.close()
211
+ asyncio.run(h())
211
212
[out]
212
213
Before
213
214
42
@@ -221,6 +222,8 @@ from typing import Any
221
222
import asyncio
222
223
from asyncio import Future
223
224
225
+ future: Future["A"]
226
+
224
227
class A:
225
228
def __init__(self, x: int) -> None:
226
229
self.x = x
@@ -229,12 +232,14 @@ async def h() -> None:
229
232
x = await future
230
233
print("h: %s" % x.x)
231
234
232
- loop = asyncio.get_event_loop()
233
- future = asyncio.Future() # type: Future[A]
234
- future.set_result(A(42))
235
- loop.run_until_complete(h())
236
- print("Outside %s" % future.result().x)
237
- loop.close()
235
+ async def main() -> None:
236
+ global future
237
+ future = asyncio.Future()
238
+ future.set_result(A(42))
239
+ await h()
240
+ print("Outside %s" % future.result().x)
241
+
242
+ asyncio.run(main())
238
243
[out]
239
244
h: 42
240
245
Outside 42
@@ -255,11 +260,7 @@ async def test() -> None:
255
260
await greet()
256
261
x = await greet() # Error
257
262
258
- loop = asyncio.get_event_loop()
259
- try:
260
- loop.run_until_complete(test())
261
- finally:
262
- loop.close()
263
+ asyncio.run(test())
263
264
[out]
264
265
_program.py:11: error: Function does not return a value (it only ever returns None)
265
266
@@ -277,10 +278,7 @@ async def print_sum(x: int, y: int) -> None:
277
278
result = await compute(x, y)
278
279
print("%s + %s = %s" % (x, y, result))
279
280
280
- loop = asyncio.get_event_loop()
281
- loop.run_until_complete(print_sum(1, 2))
282
- loop.close()
283
-
281
+ asyncio.run(print_sum(1, 2))
284
282
[out]
285
283
_program.py:8: error: Incompatible return value type (got "str", expected "int")
286
284
@@ -293,12 +291,13 @@ async def slow_operation(future: 'Future[str]') -> None:
293
291
await asyncio.sleep(1)
294
292
future.set_result(42) # Error
295
293
296
- loop = asyncio.get_event_loop()
297
- future = asyncio.Future() # type: Future[str]
298
- asyncio.Task(slow_operation(future))
299
- loop.run_until_complete(future)
300
- print(future.result())
301
- loop.close()
294
+ async def main() -> None:
295
+ future = asyncio.Future() # type: Future[str]
296
+ asyncio.Task(slow_operation(future))
297
+ await future
298
+ print(future.result())
299
+
300
+ asyncio.run(main())
302
301
[out]
303
302
_program.py:7: error: Argument 1 to "set_result" of "Future" has incompatible type "int"; expected "str"
304
303
@@ -312,12 +311,13 @@ async def slow_operation(future: 'Future[int]') -> None:
312
311
await asyncio.sleep(1)
313
312
future.set_result(42)
314
313
315
- loop = asyncio.get_event_loop()
316
- future = asyncio.Future() # type: Future[str]
317
- asyncio.Task(slow_operation(future)) # Error
318
- loop.run_until_complete(future)
319
- print(future.result())
320
- loop.close()
314
+ async def main() -> None:
315
+ future = asyncio.Future() # type: Future[str]
316
+ asyncio.Task(slow_operation(future)) # Error
317
+ await future
318
+ print(future.result())
319
+
320
+ asyncio.run(main())
321
321
[out]
322
322
_program.py:11: error: Argument 1 to "slow_operation" has incompatible type "Future[str]"; expected "Future[int]"
323
323
@@ -328,14 +328,15 @@ from asyncio import Future
328
328
329
329
async def slow_operation(future: 'Future[int]') -> None:
330
330
await asyncio.sleep(1)
331
- future.set_result('42') #Try to set an str as result to a Future[int]
332
-
333
- loop = asyncio.get_event_loop()
334
- future = asyncio.Future() # type: Future[str]
335
- asyncio.Task(slow_operation(future)) # Error
336
- loop.run_until_complete(future)
337
- print(future.result())
338
- loop.close()
331
+ future.set_result('42') # Try to set an str as result to a Future[int]
332
+
333
+ async def main() -> None:
334
+ future = asyncio.Future() # type: Future[str]
335
+ asyncio.Task(slow_operation(future)) # Error
336
+ await future
337
+ print(future.result())
338
+
339
+ asyncio.run(main())
339
340
[out]
340
341
_program.py:7: error: Argument 1 to "set_result" of "Future" has incompatible type "str"; expected "int"
341
342
_program.py:11: error: Argument 1 to "slow_operation" has incompatible type "Future[str]"; expected "Future[int]"
@@ -354,11 +355,13 @@ def got_result(future: 'Future[int]') -> None:
354
355
print(future.result())
355
356
loop.stop()
356
357
357
- loop = asyncio.get_event_loop () # type: AbstractEventLoop
358
- future = asyncio.Future() # type: Future[str]
359
- asyncio.Task(slow_operation(future))
360
- future.add_done_callback(got_result) # Error
358
+ async def main () -> None:
359
+ future = asyncio.Future() # type: Future[str]
360
+ asyncio.Task(slow_operation(future))
361
+ future.add_done_callback(got_result) # Error
361
362
363
+ loop = asyncio.new_event_loop()
364
+ loop.run_until_complete(main())
362
365
try:
363
366
loop.run_forever()
364
367
finally:
@@ -374,13 +377,13 @@ from asyncio import Future
374
377
375
378
async def h4() -> Future[int]:
376
379
await asyncio.sleep(1)
377
- f = asyncio.Future() # type: Future[int]
380
+ f = asyncio.Future() # type: Future[int]
378
381
return f
379
382
380
383
async def h3() -> Future[Future[Future[int]]]:
381
384
x = await h4()
382
385
x.set_result(42)
383
- f = asyncio.Future() # type: Future[Future[int]]
386
+ f = asyncio.Future() # type: Future[Future[int]]
384
387
f.set_result(x)
385
388
return f
386
389
@@ -393,9 +396,7 @@ async def h() -> None:
393
396
print(y)
394
397
print(x)
395
398
396
- loop = asyncio.get_event_loop()
397
- loop.run_until_complete(h())
398
- loop.close()
399
+ asyncio.run(h())
399
400
[out]
400
401
_program.py:16: error: Incompatible return value type (got "Future[Future[int]]", expected "Future[Future[Future[int]]]")
401
402
@@ -407,13 +408,13 @@ from asyncio import Future
407
408
408
409
async def h4() -> Future[int]:
409
410
await asyncio.sleep(1)
410
- f = asyncio.Future() # type: Future[int]
411
+ f = asyncio.Future() # type: Future[int]
411
412
return f
412
413
413
414
async def h3() -> Future[int]:
414
415
x = await h4()
415
416
x.set_result(42)
416
- f = asyncio.Future() # type: Future[Future[int]]
417
+ f = asyncio.Future() # type: Future[Future[int]]
417
418
f.set_result(x)
418
419
return f
419
420
@@ -424,9 +425,7 @@ async def h() -> None:
424
425
print(y)
425
426
print(x)
426
427
427
- loop = asyncio.get_event_loop()
428
- loop.run_until_complete(h())
429
- loop.close()
428
+ asyncio.run(h())
430
429
[out]
431
430
_program.py:16: error: Incompatible return value type (got "Future[Future[int]]", expected "Future[int]")
432
431
_program.py:16: note: Maybe you forgot to use "await"?
@@ -437,6 +436,8 @@ from typing import Any
437
436
import asyncio
438
437
from asyncio import Future
439
438
439
+ future: Future["A"]
440
+
440
441
class A:
441
442
def __init__(self, x: int) -> None:
442
443
self.x = x
@@ -446,16 +447,18 @@ class B:
446
447
self.x = x
447
448
448
449
async def h() -> None:
449
- x = await future # type: B # Error
450
+ x = await future # type: B # Error
450
451
print("h: %s" % x.x)
451
452
452
- loop = asyncio.get_event_loop()
453
- future = asyncio.Future() # type: Future[A]
454
- future.set_result(A(42))
455
- loop.run_until_complete(h())
456
- loop.close()
453
+ async def main() -> None:
454
+ global future
455
+ future = asyncio.Future()
456
+ future.set_result(A(42))
457
+ await h()
458
+
459
+ asyncio.run(main())
457
460
[out]
458
- _program.py:15 : error: Incompatible types in assignment (expression has type "A", variable has type "B")
461
+ _program.py:17 : error: Incompatible types in assignment (expression has type "A", variable has type "B")
459
462
460
463
[case testForwardRefToBadAsyncShouldNotCrash_newsemanal]
461
464
from typing import TypeVar
0 commit comments