python type_error ai_generated true

TypeError: BaseModel.__init__() 只接受 1 个位置参数,但给出了 2 个

TypeError: BaseModel.__init__() takes 1 positional argument but 2 were given

ID: python/fastapi-pydantic-v1-v2-migration

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

版本兼容性

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

根因分析

Pydantic v1 代码以位置参数调用 v2 模型,或使用了仅 v1 支持的 API(如 .dict() 而 v2 应为 .model_dump())。

English

Pydantic v1 code calling a v2 model with positional args, or using v1-only APIs like .dict() vs .model_dump().

generic

解决方案

  1. 94% 成功率
    item = Item(name='x')
    data = item.model_dump()
  2. 85% 成功率
    from pydantic.v1 import BaseModel

无效尝试

常见但无效的做法:

  1. 85% 失败

    FastAPI 0.100+ requires pydantic v2; other deps break.

  2. 80% 失败

    Does not address .dict/.json API changes elsewhere.