python
module_error
ai_generated
true
pydantic.errors.PydanticUserError: `@validator` 不能应用于实例方法。请改用 `@field_validator`。
pydantic.errors.PydanticUserError: `@validator` cannot be applied to instance methods. Use `@field_validator` instead.
ID: python/pydantic-deprecated-validator-v1
80%修复率
90%置信度
0证据数
2024-08-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 2.x | active | — | — | — |
根因分析
Pydantic v2 移除了 v1 的 `@validator` 装饰器;使用它(或从 `pydantic` 顶层导入)会触发用户错误。
English
Pydantic v2 removed the v1 `@validator` decorator; using it (or importing from `pydantic` top-level) raises a user error.
解决方案
-
95% 成功率
Replace `@validator('x')` with `@field_validator('x')` and `@classmethod`; note `values` dict becomes a second positional arg `info`. -
90% 成功率
Use `@model_validator(mode='after')` for cross-field validation.
无效尝试
常见但无效的做法:
-
30% 失败
Pinning to Pydantic v1 avoids the error but loses v2 performance and features.
-
50% 失败
Importing `validator` from `pydantic.deprecated` works temporarily but emits DeprecationWarning and will be removed.