Skip to content

Commit dde5015

Browse files
committed
Do not pass event loop to asyncio.sleep() and wait() on Python 3.8+
Fixes The loop argument is deprecated since Python 3.8, and scheduled for removal in Python 3.10. See https://docs.python.org/3.8/library/asyncio-task.html#asyncio.sleep and https://docs.python.org/3.8/library/asyncio-task.html#asyncio.wait
1 parent 72dcbf9 commit dde5015

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

tests/ext/aiohttp/test_middleware.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Expects pytest-aiohttp
55
"""
66
import asyncio
7+
import sys
78
from aws_xray_sdk import global_sdk_config
89
try:
910
from unittest.mock import patch
@@ -84,7 +85,10 @@ async def handle_delay(self, request: web.Request) -> web.Response:
8485
"""
8586
Handle /delay request
8687
"""
87-
await asyncio.sleep(0.3, loop=self._loop)
88+
if sys.version_info >= (3, 8):
89+
await asyncio.sleep(0.3)
90+
else:
91+
await asyncio.sleep(0.3, loop=self._loop)
8892
return web.Response(text="ok")
8993

9094
def get_app(self) -> web.Application:
@@ -282,10 +286,15 @@ async def get_delay():
282286
resp = await client.get('/delay')
283287
assert resp.status == 200
284288

285-
await asyncio.wait([get_delay(), get_delay(), get_delay(),
286-
get_delay(), get_delay(), get_delay(),
287-
get_delay(), get_delay(), get_delay()],
288-
loop=loop)
289+
if sys.version_info >= (3, 8):
290+
await asyncio.wait([get_delay(), get_delay(), get_delay(),
291+
get_delay(), get_delay(), get_delay(),
292+
get_delay(), get_delay(), get_delay()])
293+
else:
294+
await asyncio.wait([get_delay(), get_delay(), get_delay(),
295+
get_delay(), get_delay(), get_delay(),
296+
get_delay(), get_delay(), get_delay()],
297+
loop=loop)
289298

290299
# Ensure all ID's are different
291300
ids = [item.id for item in recorder.emitter.local]

tests/test_async_local_storage.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import random
3+
import sys
34

45
from aws_xray_sdk.core.async_context import TaskLocalStorage
56

@@ -19,7 +20,10 @@ async def _test():
1920
random_int = random.random()
2021
local_storage.randint = random_int
2122

22-
await asyncio.sleep(0.0, loop=event_loop)
23+
if sys.version_info >= (3, 8):
24+
await asyncio.sleep(0.0)
25+
else:
26+
await asyncio.sleep(0.0, loop=event_loop)
2327

2428
current_random_int = local_storage.randint
2529
assert random_int == current_random_int
@@ -29,9 +33,14 @@ async def _test():
2933
return False
3034

3135
# Run loads of concurrent tasks
32-
results = event_loop.run_until_complete(
33-
asyncio.wait([_test() for _ in range(0, 100)], loop=event_loop)
34-
)
36+
if sys.version_info >= (3, 8):
37+
results = event_loop.run_until_complete(
38+
asyncio.wait([_test() for _ in range(0, 100)])
39+
)
40+
else:
41+
results = event_loop.run_until_complete(
42+
asyncio.wait([_test() for _ in range(0, 100)], loop=event_loop)
43+
)
3544
results = [item.result() for item in results[0]]
3645

3746
# Double check all is good

0 commit comments

Comments
 (0)