Python "TypeError: object X can't be used in await expression" in CI
The await keyword requires an awaitable (a coroutine, Task, or Future). Awaiting a plain value, a sync function's return, or a coroutine function reference (not its call) raises this TypeError.
What this error means
A test fails with "TypeError: object dict can't be used in 'await' expression" (or function, int, ...), pointing at an await on something that is not awaitable.
pytest
TypeError: object dict can't be used in 'await' expressionCommon causes
Awaiting a synchronous result
A function that returns a plain value (not a coroutine) was awaited, which is not allowed.
Awaiting the function instead of its call
Writing await fn instead of await fn() awaits the function object itself, which is not awaitable.
How to fix it
Await only awaitables
- Confirm the target is
async defor returns a coroutine/Future. - Call the coroutine function so it returns a coroutine, then await it.
- Drop
awaitfrom synchronous calls.
Python
# wrong: await sync result / await function object
# right:
result = await fetch() # fetch is async def, called here
value = compute() # sync: no awaitWrap blocking calls for the loop
To await a synchronous, blocking function, run it in a thread executor.
Python
import asyncio
result = await asyncio.to_thread(blocking_call, arg)How to prevent it
- Await only
async defresults, Tasks, or Futures. - Call coroutine functions (
fn()) before awaiting them. - Use
asyncio.to_threadto await blocking sync work.
Related guides
Python "asyncio.run() cannot be called from a running event loop" in CIFix "RuntimeError: asyncio.run() cannot be called from a running event loop" in CI - nesting asyncio.run() in…
Python "DeprecationWarning: There is no current event loop" in CIFix "DeprecationWarning: There is no current event loop" in CI - get_event_loop() with no running loop is dep…
pytest-asyncio "RuntimeError: Event loop is closed" in CIFix pytest-asyncio "RuntimeError: Event loop is closed" in CI - an async test or teardown used a loop that wa…