Skip to content

Commit

Permalink
bpo-34790: Implement deprecation of passing coroutines to asyncio.wai…
Browse files Browse the repository at this point in the history
  • Loading branch information
aeros authored and shihai1991 committed Jan 31, 2020
1 parent 3f04bd4 commit 93b076d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,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
=======
Expand Down
6 changes: 6 additions & 0 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ 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("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)}

return await _wait(fs, timeout, return_when, loop)
Expand Down
24 changes: 19 additions & 5 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -3307,6 +3310,17 @@ 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
with self.assertWarns(DeprecationWarning):
self.loop.run_until_complete(
asyncio.wait([coroutine_function()]))

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
Expand Down

0 comments on commit 93b076d

Please sign in to comment.