{"record":{"id":"4210431caa33c439","repo":"python/cpython","slug":"expects-an-iterable-of-awaitables-not-type-fs","errorCode":null,"errorMessage":"expects an iterable of awaitables, not {type(fs).__name__}","messagePattern":"expects an iterable of awaitables, not (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/tasks.py","lineNumber":671,"sourceCode":"    awaitable.  This pattern is compatible with Python versions older than\n    3.13:\n\n        ipv4_connect = create_task(open_connection(\"127.0.0.1\", 80))\n        ipv6_connect = create_task(open_connection(\"::1\", 80))\n        tasks = [ipv4_connect, ipv6_connect]\n\n        for next_connect in as_completed(tasks):\n            # next_connect is not one of the original task objects. It must\n            # be awaited to obtain the result value or raise the exception\n            # of the awaitable that finishes next.\n            reader, writer = await next_connect\n\n    A TimeoutError is raised if the timeout occurs before all awaitables\n    are done.  This is raised by the async for loop during asynchronous\n    iteration or by the coroutines yielded during plain iteration.\n    \"\"\"\n    if inspect.isawaitable(fs):\n        raise TypeError(\n            f\"expects an iterable of awaitables, not {type(fs).__name__}\"\n        )\n\n    return _AsCompletedIterator(fs, timeout)\n\n\n@types.coroutine\ndef __sleep0():\n    \"\"\"Skip one event loop run cycle.\n\n    This is a private helper for 'asyncio.sleep()', used\n    when the 'delay' is set to 0.  It uses a bare 'yield'\n    expression (which Task.__step knows how to handle)\n    instead of creating a Future object.\n    \"\"\"\n    yield\n\n","sourceCodeStart":653,"sourceCodeEnd":689,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/tasks.py#L653-L689","documentation":"Raised by asyncio.as_completed() when its first argument is a single awaitable instead of an iterable of awaitables. The guard `inspect.isawaitable(fs)` exists to catch the common mistake of passing one coroutine where a list is expected, which would otherwise iterate the object in confusing ways or silently do nothing.","triggerScenarios":"Calling `asyncio.as_completed(fetch(url))` instead of `asyncio.as_completed([fetch(url)])`; also passing a single Task or Future where the API expects a list/iterable of them.","commonSituations":"Refactoring code that previously awaited a single coroutine into an as_completed loop; copy-pasting a gather() call pattern into as_completed() without wrapping the single item in a list.","solutions":["Wrap the single awaitable in a list: `for coro in asyncio.as_completed([fetch(url)])`","If there is only one awaitable, just `await fetch(url)` directly — no iterator needed","When building from a loop, pass a list comprehension: `asyncio.as_completed([fetch(u) for u in urls])`"],"exampleFix":"// before\nfor nxt in asyncio.as_completed(fetch(url)):\n    ...\n\n// after\nfor nxt in asyncio.as_completed([fetch(url)]):\n    result = await nxt","handlingStrategy":"validation","validationCode":"import inspect, asyncio\n\narg = fetch(url)  # maybe a single awaitable\nif inspect.isawaitable(arg) and not hasattr(arg, '__iter__'):\n    arg = [arg]          # normalize single awaitable to a list\nfor nxt in asyncio.as_completed(arg):\n    result = await nxt","typeGuard":"import inspect\nfrom typing import Any, Iterable, Awaitable\n\ndef is_awaitable_iterable(obj: Any) -> bool:\n    \"\"\"True if obj is safe to pass to asyncio.as_completed.\"\"\"\n    return not inspect.isawaitable(obj) and isinstance(obj, Iterable) and all(\n        inspect.isawaitable(x) for x in obj\n    )","tryCatchPattern":"try:\n    it = asyncio.as_completed(fs)\nexcept TypeError:\n    it = asyncio.as_completed([fs])  # single awaitable was passed","preventionTips":["Always pass a list comprehension of awaitables to as_completed","If handling exactly one awaitable, await it directly instead","Wrap construction of awaitable collections in one helper so the shape is uniform"],"tags":["asyncio","python","as-completed","argument-validation"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}