python
config_error
ai_generated
true
pydantic_core._pydantic_core.ValidationError:User 出现 2 个验证错误 foo 不允许额外输入 [type=extra_forbidden, input_value='bar', input_type=str]
pydantic_core._pydantic_core.ValidationError: 2 validation errors for User foo Extra inputs are not permitted [type=extra_forbidden, input_value='bar', input_type=str]
ID: python/pydantic-extra-forbidden
80%修复率
88%置信度
0证据数
2024-02-27首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 2.x | active | — | — | — |
根因分析
model_config 设置了 extra='forbid'。传入的字典包含模型未声明的键。
English
model_config has extra='forbid'. The incoming dict contains keys not declared on the model.
解决方案
-
90% 成功率
class User(BaseModel): model_config = ConfigDict(extra='forbid') foo: str | None = None -
85% 成功率
class User(BaseModel): model_config = ConfigDict(extra='allow')
无效尝试
常见但无效的做法:
-
55% 失败
Silently drops fields users expect to be persisted, causing data loss bugs.
-
50% 失败
Hides schema drift; clients keep sending wrong fields and you never notice.
-
70% 失败
Pydantic v2 does not accept **kwargs in BaseModel subclasses; raises PydanticUserError.