Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit aa40ec5

Browse files
committed
Issue #131: as_completed() and wait() now raises a TypeError if the list of
futures is not a list but a Future, Task or coroutine object
1 parent 9d6147f commit aa40ec5

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

asyncio/tasks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED):
358358
Note: This does not raise TimeoutError! Futures that aren't done
359359
when the timeout occurs are returned in the second set.
360360
"""
361+
if isinstance(fs, futures.Future) or iscoroutine(fs):
362+
raise TypeError("expect a list of futures, not %s" % type(fs).__name__)
361363
if not fs:
362364
raise ValueError('Set of coroutines/Futures is empty.')
363365

@@ -474,6 +476,8 @@ def as_completed(fs, *, loop=None, timeout=None):
474476
475477
Note: The futures 'f' are not necessarily members of fs.
476478
"""
479+
if isinstance(fs, futures.Future) or iscoroutine(fs):
480+
raise TypeError("expect a list of futures, not %s" % type(fs).__name__)
477481
loop = loop if loop is not None else events.get_event_loop()
478482
deadline = None if timeout is None else loop.time() + timeout
479483
todo = {async(f, loop=loop) for f in set(fs)}

tests/test_tasks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
from asyncio import test_utils
88

99

10+
@asyncio.coroutine
11+
def coroutine_function():
12+
pass
13+
14+
1015
class Dummy:
1116

1217
def __repr__(self):
@@ -1338,6 +1343,27 @@ def test_gather_shield(self):
13381343
child2.set_result(2)
13391344
test_utils.run_briefly(self.loop)
13401345

1346+
def test_as_completed_invalid_args(self):
1347+
fut = asyncio.Future(loop=self.loop)
1348+
1349+
# as_completed() expects a list of futures, not a future instance
1350+
self.assertRaises(TypeError, self.loop.run_until_complete,
1351+
asyncio.as_completed(fut, loop=self.loop))
1352+
self.assertRaises(TypeError, self.loop.run_until_complete,
1353+
asyncio.as_completed(coroutine_function(), loop=self.loop))
1354+
1355+
def test_wait_invalid_args(self):
1356+
fut = asyncio.Future(loop=self.loop)
1357+
1358+
# wait() expects a list of futures, not a future instance
1359+
self.assertRaises(TypeError, self.loop.run_until_complete,
1360+
asyncio.wait(fut, loop=self.loop))
1361+
self.assertRaises(TypeError, self.loop.run_until_complete,
1362+
asyncio.wait(coroutine_function(), loop=self.loop))
1363+
1364+
# wait() expects at least a future
1365+
self.assertRaises(ValueError, self.loop.run_until_complete,
1366+
asyncio.wait([], loop=self.loop))
13411367

13421368
class GatherTestsBase:
13431369

0 commit comments

Comments
 (0)