Python RuntimeWarning: coroutine was never awaited
An async coroutine was created but never awaited, so it never executed.
Explanation
RuntimeWarning: coroutine was never awaited occurs when you create an async function call (coroutine) but forget to await it. In Python, calling an async function doesn't execute it - it creates a coroutine object that must be awaited. If the coroutine is never awaited, the code inside it never runs, and Python issues a RuntimeWarning. Common causes include forgetting the await keyword, calling an async function from a synchronous context, or storing a coroutine without awaiting it later.
Common Causes
- Missing await keyword
- Calling async function from sync context
- Storing coroutine without awaiting
- Forgetting to await in list comprehension
- Async callback not properly awaited
Solution
Always use await when calling async functions. Use asyncio.run() for top-level async code. Check that you're not accidentally calling an async function from synchronous code without awaiting. Use type hints (async def) to clarify which functions are coroutines. Use linters like mypy or pylint to detect unawaited coroutines. For sync-to-async calls, use asyncio.create_task() or loop.run_until_complete(). Check that callback functions in frameworks are properly marked async if they call async functions.
Code Example
# Warning: coroutine was never awaited
def fetch_data():
return fetch_async_data() # missing await!
# Fix: use async/await
async def fetch_data():
return await fetch_async_data()
# Or use asyncio.run for top-level
import asyncio
result = asyncio.run(fetch_async_data())
# Common mistake: calling async from sync
import asyncio
async def get_user():
return await db.fetch_user()
user = get_user() # Warning! coroutine never awaited
# Fix 1: await in async context
async def main():
user = await get_user()
asyncio.run(main())
# Fix 2: create task for concurrent execution
async def main():
task = asyncio.create_task(get_user())
# do other work
user = await task
# Type hint to catch issues
async def get_user() -> dict: # clearly async
return await db.fetch_user()
Error Information
Language
python
Difficulty
Intermediate
Views
14
Related Errors
Python
Python SyntaxError
Python detected invalid syntax in the source code.
Python
Python ValueError
A function received an argument of correct type but inappropriate value.
Python
Python TypeError: argument of type is not iterable
The code tries to iterate over a value that is not iterable.
Python
Python UnicodeDecodeError
Python failed to decode bytes into a string using the specified encoding.