Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf0e1d04

Browse files
authored
Do not implicitly convert after model validators to class methods (#11957)
1 parentde217f4 commitf0e1d04

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

‎pydantic/functional_validators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,9 @@ def verify_square(self) -> Self:
716716
"""
717717

718718
defdec(f:Any)->_decorators.PydanticDescriptorProxy[Any]:
719-
# auto apply the @classmethod decorator
720-
f=_decorators.ensure_classmethod_based_on_signature(f)
719+
# auto apply the @classmethod decorator (except for *after* validators, which should be instance methods):
720+
ifmode!='after':
721+
f=_decorators.ensure_classmethod_based_on_signature(f)
721722
dec_info=_decorators.ModelValidatorDecoratorInfo(mode=mode)
722723
return_decorators.PydanticDescriptorProxy(f,dec_info)
723724

‎tests/test_model_validator.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
importpytest
66

7-
frompydanticimportBaseModel,ValidationInfo,ValidatorFunctionWrapHandler,model_validator
7+
frompydanticimportBaseModel,PydanticUserError,ValidationInfo,ValidatorFunctionWrapHandler,model_validator
88

99

1010
deftest_model_validator_wrap()->None:
@@ -136,3 +136,13 @@ def validate_model_after(self) -> Model:
136136
Model.model_validate({'inner': {'inner': {'inner':None}}})
137137
assertcalls== ['before']*3+ ['after']*3
138138
calls.clear()
139+
140+
141+
deftest_after_validator_wrong_signature()->None:
142+
withpytest.raises(PydanticUserError):
143+
144+
classModel(BaseModel):
145+
@model_validator(mode='after')
146+
# This used to be converted into a classmethod, resulting
147+
# in this inconsistent signature still accepted:
148+
defvalidator(cls,model,info): ...

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp