python type_error ai_generated true

TypeError: NoneType 对象不能用于 'await' 表达式

TypeError: object NoneType can't be used in 'await' expression

ID: python/fastapi-await-none-coroutine

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-12-15首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

异步路由 await 了一个实际上是同步的调用(例如同步数据库驱动或返回 None 的函数)。

English

An async route awaits a call that is actually synchronous (e.g. a sync DB driver or a function returning None).

generic

解决方案

  1. 88% 成功率
    Use an async driver or run sync in a threadpool:
    from starlette.concurrency import run_in_threadpool
    result = await run_in_threadpool(sync_fn)
  2. 90% 成功率
    Remove await when calling a synchronous function.

无效尝试

常见但无效的做法:

  1. 60% 失败

    Fixes this call but blocks the event loop for other requests.

  2. 85% 失败

    Doesn't make the awaited object awaitable.