python
async_error
ai_generated
true
AttributeError: 'coroutine' object has no attribute 'X'
ID: python/attributeerror-coroutine-no-attribute
95%Fix Rate
95%Confidence
200Evidence
2018-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 311 | active | — | — | — |
Root Cause
Occurs when calling an async function without 'await'. The function returns a coroutine object instead of the actual result, and accessing attributes on that coroutine fails.
genericWorkarounds
-
97% success Add 'await' before the async function call
# Wrong: result = fetch_data() # returns coroutine result.name # AttributeError # Right: result = await fetch_data() # returns actual data result.name # works
Sources: https://docs.python.org/3/library/asyncio-task.html#coroutines
-
90% success If in synchronous code, use asyncio.run() to execute the coroutine
import asyncio result = asyncio.run(fetch_data()) result.name # works
Sources: https://docs.python.org/3/library/asyncio-runner.html
Dead Ends
Common approaches that don't work:
-
Checking if the attribute actually exists on the return type
85% fail
The attribute exists on the actual return value, not on the coroutine wrapper. The issue is the missing 'await', not a missing attribute.
-
Adding the missing attribute to the returned object's class
95% fail
The returned object is a coroutine, not the intended type. Adding attributes to the class won't help because the code never reaches the actual return value.
Error Chain
Frequently confused with: