From 6e30ac407e4cb618e4bc883f857380366e8fb682 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 28 Oct 2019 20:58:11 -0400 Subject: [PATCH 1/5] Implement deprecation of passing coros to asyncio.wait() --- Lib/asyncio/tasks.py | 5 +++++ Lib/test/test_asyncio/test_tasks.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 38d982716d46ad..a659be3f961bab 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -421,6 +421,11 @@ async def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED): "and scheduled for removal in Python 3.10.", DeprecationWarning, stacklevel=2) + if any(coroutines.iscoroutine(f) for f in set(fs)): + warnings.warn("Passing coroutines objects to asyncio.wait() is " + "deprecated since Python 3.8, and scheduled for removal " + "in Python 3.11", DeprecationWarning) + fs = {ensure_future(f, loop=loop) for f in set(fs)} return await _wait(fs, timeout, return_when, loop) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index dde84b84b103e9..1451ef504f5f03 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -3307,6 +3307,19 @@ def test_loop_argument_is_deprecated_in_wait_for(self): self.loop.run_until_complete( asyncio.wait_for(coroutine_function(), 0.01, loop=self.loop)) + def test_coro_is_deprecated_in_wait(self): + # Remove test when passing coros to asyncio.wait() is removed in 3.11 + # Test with coro as first arg + with self.assertWarns(DeprecationWarning): + self.loop.run_until_complete( + asyncio.wait([coroutine_function()])) + + # Test with coro as second arg + task = self.loop.create_task(coroutine_function()) + with self.assertWarns(DeprecationWarning): + self.loop.run_until_complete( + asyncio.wait([task, coroutine_function()])) + class CompatibilityTests(test_utils.TestCase): # Tests for checking a bridge between old-styled coroutines From b441b27fc1d3753b9f3b2a6e8d6b5537f27572e2 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 28 Oct 2019 21:37:06 -0400 Subject: [PATCH 2/5] Add 3.9 whatsnew entry --- Doc/whatsnew/3.9.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index bca222955668d5..51d2f9144d0ae5 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -216,6 +216,9 @@ Deprecated predicable behavior. (Contributed by Serhiy Storchaka in :issue:`38371`.) +* The explicit passing of coroutine objects to :func:`asyncio.wait` has been + deprecated and will be removed in version 3.11. + (Contributed by Yury Selivanov and Kyle Stanley in :issue:`34790`.) Removed ======= From 42caba2594593ca1f8e64f9e951a9291ed7fcd5b Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 28 Oct 2019 21:50:42 -0400 Subject: [PATCH 3/5] Fix deprecation message --- Lib/asyncio/tasks.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index a659be3f961bab..190bc84d804af6 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -422,9 +422,10 @@ async def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED): DeprecationWarning, stacklevel=2) if any(coroutines.iscoroutine(f) for f in set(fs)): - warnings.warn("Passing coroutines objects to asyncio.wait() is " - "deprecated since Python 3.8, and scheduled for removal " - "in Python 3.11", DeprecationWarning) + warnings.warn("The explicit passing of coroutine objects to " + "asyncio.wait() is deprecated since Python 3.8, and " + "scheduled for removal in Python 3.11.", + DeprecationWarning, stacklevel=2) fs = {ensure_future(f, loop=loop) for f in set(fs)} From 0e8997849b3ba3babbeb6872c8613af63c2ffc8c Mon Sep 17 00:00:00 2001 From: aeros Date: Tue, 29 Oct 2019 02:28:47 -0400 Subject: [PATCH 4/5] Add self.assertWarns() --- Lib/test/test_asyncio/test_tasks.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 1451ef504f5f03..15dea18ed2aa7b 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -979,12 +979,12 @@ def test_wait_duplicate_coroutines(self): def coro(s): return s c = coro('test') - - task =self.new_task( + task = self.new_task( self.loop, asyncio.wait([c, c, coro('spam')])) - done, pending = self.loop.run_until_complete(task) + with self.assertWarns(DeprecationWarning): + done, pending = self.loop.run_until_complete(task) self.assertFalse(pending) self.assertEqual(set(f.result() for f in done), {'test', 'spam'}) @@ -1346,7 +1346,9 @@ def gen(): futs = list(asyncio.as_completed(fs, loop=loop)) self.assertEqual(len(futs), 2) waiter = asyncio.wait(futs) - done, pending = loop.run_until_complete(waiter) + # Deprecation from passing coros in futs to asyncio.wait() + with self.assertWarns(DeprecationWarning): + done, pending = loop.run_until_complete(waiter) self.assertEqual(set(f.result() for f in done), {'a', 'b'}) def test_as_completed_duplicate_coroutines(self): @@ -1751,7 +1753,8 @@ async def inner(): async def outer(): nonlocal proof - d, p = await asyncio.wait([inner()]) + with self.assertWarns(DeprecationWarning): + d, p = await asyncio.wait([inner()]) proof += 100 f = asyncio.ensure_future(outer(), loop=self.loop) From 9a1d2bbbc2f4b11897ff04aaf3453a31f227bb12 Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Tue, 29 Oct 2019 03:34:11 -0400 Subject: [PATCH 5/5] Trim comments --- Lib/test/test_asyncio/test_tasks.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 15dea18ed2aa7b..68f3b8cce9f65d 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -3312,12 +3312,10 @@ def test_loop_argument_is_deprecated_in_wait_for(self): def test_coro_is_deprecated_in_wait(self): # Remove test when passing coros to asyncio.wait() is removed in 3.11 - # Test with coro as first arg with self.assertWarns(DeprecationWarning): self.loop.run_until_complete( asyncio.wait([coroutine_function()])) - # Test with coro as second arg task = self.loop.create_task(coroutine_function()) with self.assertWarns(DeprecationWarning): self.loop.run_until_complete(