python
type_error
ai_generated
true
pydantic.errors.PydanticUserError: @field_validator 'check_email' should be a classmethod or a staticmethod. Wrap the function with @classmethod or @staticmethod.
ID: python/pydantic-field-validator-signature-error
80%Fix Rate
90%Confidence
0Evidence
2024-03-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
Root Cause
In Pydantic v2, @field_validator must decorate a classmethod (or staticmethod). Missing the @classmethod decorator causes a PydanticUserError at class definition time.
generic中文
在 Pydantic v2 中,@field_validator 必须装饰 classmethod(或 staticmethod)。缺少 @classmethod 装饰器会在类定义时引发 PydanticUserError。
Workarounds
-
98% success
from pydantic import BaseModel, field_validator class User(BaseModel): email: str @field_validator('email') @classmethod def check_email(cls, v): assert '@' in v return v -
95% success
from pydantic import BaseModel, field_validator class User(BaseModel): email: str @field_validator('email') @staticmethod def check_email(v): return v.lower()
Dead Ends
Common approaches that don't work:
-
80% fail
Decorator order matters: @field_validator must be the outermost decorator, i.e., listed first.
-
60% fail
@validator is deprecated in v2 and raises PydanticDeprecatedSince20 warnings; it also still requires classmethod semantics.