Python 3.10 compatibility — deprecated loop argument for asnycio.sleep/gather calls #5905
Closed
Description
Describe the bug
Hi,
Tests currently fail with Python 3.10 beta 4 because the loop attribute was removed.
To Reproduce
Run tests with Python 3.10.
Expected behavior
Tests pass.
Logs/tracebacks
to_cancel = {<Task pending name='Task-1' coro=<_run_app() running at /var/tmp/portage/dev-python/aiohttp-3.7.4-r2/work/aiohttp-3.7.4-python3_10/lib/aiohttp/web.py:429> wait_for=<Future cancelled>>}
loop = <_UnixSelectorEventLoop running=False closed=False debug=False>
def _cancel_tasks(
to_cancel: Set["asyncio.Task[Any]"], loop: asyncio.AbstractEventLoop
) -> None:
if not to_cancel:
return
for task in to_cancel:
task.cancel()
loop.run_until_complete(
> asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
)
E TypeError: gather() got an unexpected keyword argument 'loop'
self = <test_locks.TestEventResultOrError object at 0x7f49d37cfd00>, loop = <_UnixSelectorEventLoop running=False closed=False debug=False>
async def test_cancel_waiters(self, loop) -> None:
ev = EventResultOrError(loop=loop)
async def c():
await ev.wait()
t1 = loop.create_task(c())
t2 = loop.create_task(c())
> await asyncio.sleep(0, loop=loop)
E TypeError: sleep() got an unexpected keyword argument 'loop' Python Version
$ python --version
Python 3.10.0b4aiohttp Version
$ python -m pip show aiohttp
Name: aiohttp
Version: 3.7.4
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91@gmail.com
License: Apache 2
Location: /usr/lib/python3.9/site-packages
Requires: attrs, chardet, multidict, async-timeout, yarl, typing-extensions
Required-by: Electrum, aiohttp-socksmultidict Version
$ python -m pip show multidict
Name: multidict
Version: 5.1.0
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /usr/lib/python3.10/site-packages
Requires:
Required-by: yarl, aiohttpyarl Version
$ python -m pip show yarl
Name: yarl
Version: 1.6.3
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /usr/lib/python3.9/site-packages
Requires: multidict, idna
Required-by: aiohttpOS
Gentoo unstable amd64.
Related component
Server, Client
Additional context
This seems to fix the tests but I don't know asyncio well enough to be sure that this is the right fix. It also lacks fixes for examples/legacy/crawl.py which also uses the deprecated argument.
commit ec87d9f2b6541599dd7fc8aaebf0fdfbb812ade7
Author: Louis Sautier <sautier.louis@gmail.com>
Date: Tue Jul 20 23:37:27 2021 +0200
Remove deprecated loop argument from asyncio.sleep/gather calls
diff --git a/aiohttp/web.py b/aiohttp/web.py
index 557e3c3b..52dfdf93 100644
--- a/aiohttp/web.py
+++ b/aiohttp/web.py
@@ -441,7 +441,7 @@ def _cancel_tasks(
task.cancel()
loop.run_until_complete(
- asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
+ asyncio.gather(*to_cancel, return_exceptions=True)
)
for task in to_cancel:
diff --git a/tests/test_locks.py b/tests/test_locks.py
index 55fd2330..5f434eac 100644
--- a/tests/test_locks.py
+++ b/tests/test_locks.py
@@ -18,7 +18,7 @@ class TestEventResultOrError:
return 1
t = loop.create_task(c())
- await asyncio.sleep(0, loop=loop)
+ await asyncio.sleep(0)
e = Exception()
ev.set(exc=e)
assert (await t) == e
@@ -31,7 +31,7 @@ class TestEventResultOrError:
return 1
t = loop.create_task(c())
- await asyncio.sleep(0, loop=loop)
+ await asyncio.sleep(0)
ev.set()
assert (await t) == 1
@@ -43,7 +43,7 @@ class TestEventResultOrError:
t1 = loop.create_task(c())
t2 = loop.create_task(c())
- await asyncio.sleep(0, loop=loop)
+ await asyncio.sleep(0)
ev.cancel()
ev.set()
diff --git a/tests/test_proxy_functional.py b/tests/test_proxy_functional.py
index 68763cd4..65c773a1 100644
--- a/tests/test_proxy_functional.py
+++ b/tests/test_proxy_functional.py
@@ -238,7 +238,7 @@ async def test_proxy_http_multi_conn_limit(proxy_test_server, loop) -> None:
resp = await sess.get(url, proxy=proxy.url)
current_pid = pid
- await asyncio.sleep(0.2, loop=loop)
+ await asyncio.sleep(0.2)
assert current_pid == pid
await resp.release()
@@ -443,7 +443,7 @@ async def xtest_proxy_https_multi_conn_limit(proxy_test_server, loop):
resp = await sess.get(url, proxy=proxy.url)
current_pid = pid
- await asyncio.sleep(0.2, loop=loop)
+ await asyncio.sleep(0.2)
assert current_pid == pid
await resp.release()Code of Conduct
- I agree to follow the aio-libs Code of Conduct