python
type_error
ai_generated
true
TypeError: 'async with' 从 __aenter__ 收到的对象未实现 __await__:Timeout
TypeError: 'async with' received an object from __aenter__ that does not implement __await__: Timeout
ID: python/asyncio-timeout-context-manager-not-async
80%修复率
85%置信度
0证据数
2024-11-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
在 asyncio.timeout 仅作为普通上下文管理器存在的 Python 版本上使用 async with asyncio.timeout()(或在根本不支持它的旧版本上),或返回了自定义 Timeout 对象而非协程。
English
Code uses async with asyncio.timeout() on a Python version where asyncio.timeout exists only as a plain context manager (or on an older version where it does not exist at all), or a custom Timeout object is returned instead of a coroutine.
解决方案
-
92% 成功率
try: result = await asyncio.wait_for(coro(), timeout=5.0) except asyncio.TimeoutError: result = None -
88% 成功率
async with asyncio.timeout(5.0): await coro() # note: 'async with', but NOT 'async with await'
无效尝试
常见但无效的做法:
-
90% 失败
asyncio.timeout is not a coroutine; awaiting it raises TypeError.
-
80% 失败
Does not actually cancel the inner coroutine, so the timeout never triggers.