python type_error ai_generated true

类型错误:'NoneType' 对象不可迭代。

TypeError: 'NoneType' object is not iterable

ID: python/fastapi-query-param-type-conflict

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

版本兼容性

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

根因分析

查询参数定义为默认 None,但在未检查的情况下用于迭代。

English

Query parameter defined with default None but used in iteration without checking.

generic

解决方案

  1. 95% 成功率
    Use Optional[List[str]] = Query(None) and then if items: for item in items:
  2. 90% 成功率
    Use list = Query(default_factory=list) to ensure always iterable

无效尝试

常见但无效的做法:

  1. 60% 失败

    May still miss cases where param is empty list.

  2. 80% 失败

    Mutable default arguments are shared across requests, causing bugs.