Skip to content

Commit d1fc024

Browse files
committed
fix: define "after" model validator correctly
https://docs.pydantic.dev/latest/concepts/validators/#model-validators says that "after" model validators are instance methods and should return the validated instance. The previous test code produced a warning from pydantic >= 2.9.0: ``` UserWarning: A custom validator is returning a value other than `self`. Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`. See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details. ```
1 parent 808a681 commit d1fc024

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

tests/test_decorators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ def _validate_x_before(cls, v):
120120
return v
121121

122122
@model_validator(mode="after")
123-
def _validate_x_after(cls, v):
124-
mock_after(v)
125-
return v
123+
def _validate_x_after(self):
124+
mock_after()
125+
return self
126126

127127
# this also needs to work
128128
@model_validator(mode="after")
@@ -133,6 +133,6 @@ def _validate_x_after_cm(cls, v):
133133

134134
m = Model(x="2")
135135
mock_before.assert_called_once_with({"x": "2"})
136-
mock_after.assert_called_once_with(m)
136+
mock_after.assert_called_once_with()
137137
mock_after_cm.assert_called_once_with(m)
138138
assert m.x == 2

0 commit comments

Comments
 (0)