Skip to content

Commit c9ed867

Browse files
authored
Update asyncio eval tests for 3.14 (#18468)
Starting in Python 3.14, `asyncio.get_event_loop` will raise a RuntimeError if no current loop exists in the current thread. Update the eval tests to use `asyncio.run` instead.
1 parent fb7b254 commit c9ed867

File tree

1 file changed

+95
-92
lines changed

1 file changed

+95
-92
lines changed

test-data/unit/pythoneval-asyncio.test

+95-92
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ async def greet_every_two_seconds() -> None:
2525
print('After', n)
2626
n += 1
2727

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())
3329
[out]
3430
Prev 0
3531
After 0
@@ -56,9 +52,7 @@ async def print_sum(x: int, y: int) -> None:
5652
result = await compute(x, y) # The type of result will be int (is extracted from Future[int]
5753
print("%s + %s = %s" % (x, y, result))
5854

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))
6256
[out]
6357
Compute 1 + 2 ...
6458
1 + 2 = 3
@@ -72,12 +66,13 @@ async def slow_operation(future: 'Future[str]') -> None:
7266
await asyncio.sleep(0.01)
7367
future.set_result('Future is done!')
7468

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())
8176
[out]
8277
Future is done!
8378

@@ -95,10 +90,13 @@ def got_result(future: 'Future[str]') -> None:
9590
print(future.result())
9691
loop.stop()
9792

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())
102100
try:
103101
loop.run_forever()
104102
finally:
@@ -119,13 +117,14 @@ async def factorial(name, number) -> None:
119117
f *= i
120118
print("Task %s: factorial(%s) = %s" % (name, number, f))
121119

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())
129128
[out]
130129
Task A: Compute factorial(2)...
131130
Task B: Compute factorial(2)...
@@ -144,6 +143,8 @@ from typing import Any
144143
import asyncio
145144
from asyncio import Future
146145

146+
future: Future[int]
147+
147148
async def h4() -> int:
148149
x = await future
149150
return x
@@ -162,12 +163,14 @@ async def h() -> None:
162163
x = await h2()
163164
print("h: %s" % x)
164165

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())
171174
[out]
172175
h3: 42
173176
h2: 42
@@ -182,13 +185,13 @@ from asyncio import Future
182185

183186
async def h4() -> "Future[int]":
184187
await asyncio.sleep(0.01)
185-
f = asyncio.Future() #type: Future[int]
188+
f = asyncio.Future() # type: Future[int]
186189
return f
187190

188191
async def h3() -> "Future[Future[int]]":
189192
x = await h4()
190193
x.set_result(42)
191-
f = asyncio.Future() #type: Future[Future[int]]
194+
f = asyncio.Future() # type: Future[Future[int]]
192195
f.set_result(x)
193196
return f
194197

@@ -205,9 +208,7 @@ async def h() -> None:
205208
print(normalize(y))
206209
print(normalize(x))
207210

208-
loop = asyncio.get_event_loop()
209-
loop.run_until_complete(h())
210-
loop.close()
211+
asyncio.run(h())
211212
[out]
212213
Before
213214
42
@@ -221,6 +222,8 @@ from typing import Any
221222
import asyncio
222223
from asyncio import Future
223224

225+
future: Future["A"]
226+
224227
class A:
225228
def __init__(self, x: int) -> None:
226229
self.x = x
@@ -229,12 +232,14 @@ async def h() -> None:
229232
x = await future
230233
print("h: %s" % x.x)
231234

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())
238243
[out]
239244
h: 42
240245
Outside 42
@@ -255,11 +260,7 @@ async def test() -> None:
255260
await greet()
256261
x = await greet() # Error
257262

258-
loop = asyncio.get_event_loop()
259-
try:
260-
loop.run_until_complete(test())
261-
finally:
262-
loop.close()
263+
asyncio.run(test())
263264
[out]
264265
_program.py:11: error: Function does not return a value (it only ever returns None)
265266

@@ -277,10 +278,7 @@ async def print_sum(x: int, y: int) -> None:
277278
result = await compute(x, y)
278279
print("%s + %s = %s" % (x, y, result))
279280

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))
284282
[out]
285283
_program.py:8: error: Incompatible return value type (got "str", expected "int")
286284

@@ -293,12 +291,13 @@ async def slow_operation(future: 'Future[str]') -> None:
293291
await asyncio.sleep(1)
294292
future.set_result(42) # Error
295293

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())
302301
[out]
303302
_program.py:7: error: Argument 1 to "set_result" of "Future" has incompatible type "int"; expected "str"
304303

@@ -312,12 +311,13 @@ async def slow_operation(future: 'Future[int]') -> None:
312311
await asyncio.sleep(1)
313312
future.set_result(42)
314313

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())
321321
[out]
322322
_program.py:11: error: Argument 1 to "slow_operation" has incompatible type "Future[str]"; expected "Future[int]"
323323

@@ -328,14 +328,15 @@ from asyncio import Future
328328

329329
async def slow_operation(future: 'Future[int]') -> None:
330330
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())
339340
[out]
340341
_program.py:7: error: Argument 1 to "set_result" of "Future" has incompatible type "str"; expected "int"
341342
_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:
354355
print(future.result())
355356
loop.stop()
356357

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
361362

363+
loop = asyncio.new_event_loop()
364+
loop.run_until_complete(main())
362365
try:
363366
loop.run_forever()
364367
finally:
@@ -374,13 +377,13 @@ from asyncio import Future
374377

375378
async def h4() -> Future[int]:
376379
await asyncio.sleep(1)
377-
f = asyncio.Future() #type: Future[int]
380+
f = asyncio.Future() # type: Future[int]
378381
return f
379382

380383
async def h3() -> Future[Future[Future[int]]]:
381384
x = await h4()
382385
x.set_result(42)
383-
f = asyncio.Future() #type: Future[Future[int]]
386+
f = asyncio.Future() # type: Future[Future[int]]
384387
f.set_result(x)
385388
return f
386389

@@ -393,9 +396,7 @@ async def h() -> None:
393396
print(y)
394397
print(x)
395398

396-
loop = asyncio.get_event_loop()
397-
loop.run_until_complete(h())
398-
loop.close()
399+
asyncio.run(h())
399400
[out]
400401
_program.py:16: error: Incompatible return value type (got "Future[Future[int]]", expected "Future[Future[Future[int]]]")
401402

@@ -407,13 +408,13 @@ from asyncio import Future
407408

408409
async def h4() -> Future[int]:
409410
await asyncio.sleep(1)
410-
f = asyncio.Future() #type: Future[int]
411+
f = asyncio.Future() # type: Future[int]
411412
return f
412413

413414
async def h3() -> Future[int]:
414415
x = await h4()
415416
x.set_result(42)
416-
f = asyncio.Future() #type: Future[Future[int]]
417+
f = asyncio.Future() # type: Future[Future[int]]
417418
f.set_result(x)
418419
return f
419420

@@ -424,9 +425,7 @@ async def h() -> None:
424425
print(y)
425426
print(x)
426427

427-
loop = asyncio.get_event_loop()
428-
loop.run_until_complete(h())
429-
loop.close()
428+
asyncio.run(h())
430429
[out]
431430
_program.py:16: error: Incompatible return value type (got "Future[Future[int]]", expected "Future[int]")
432431
_program.py:16: note: Maybe you forgot to use "await"?
@@ -437,6 +436,8 @@ from typing import Any
437436
import asyncio
438437
from asyncio import Future
439438

439+
future: Future["A"]
440+
440441
class A:
441442
def __init__(self, x: int) -> None:
442443
self.x = x
@@ -446,16 +447,18 @@ class B:
446447
self.x = x
447448

448449
async def h() -> None:
449-
x = await future # type: B # Error
450+
x = await future # type: B # Error
450451
print("h: %s" % x.x)
451452

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())
457460
[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")
459462

460463
[case testForwardRefToBadAsyncShouldNotCrash_newsemanal]
461464
from typing import TypeVar

0 commit comments

Comments
 (0)