python type_error ai_generated true

TypeError: 'NoneType' object is not iterable

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-06-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 60% fail

    May still miss cases where param is empty list.

  2. 80% fail

    Mutable default arguments are shared across requests, causing bugs.