-
Couldn't load subscription status.
- Fork 311
fix: model validator assignment consistency #1798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
CodSpeed Performance Report
Merging #1798 will not alter performance
Comparing mot3:fix/model-validator-assignment-dict-issue (da2af49) with main (ed0d1ca)
Summary
✅ 163 untouched
Codecov Report
✅ All modified and coverable lines are covered by tests.
📢 Thoughts on this report? Let us know!
AI slop.
@mot3 Please avoid generating whole PR descriptions and comments with AI. I want to interact with humans, not LLMs. It also unfortunately conveys that you may have generated the contribution with AI as well (which is actually the case almost every time when description are fully generated like this).
I also closed this directly as it does not fix the actual issue when I tested locally. My original comment mentioned:
I'm not entirely sure why the model before validator has the dict form.
And not that the before validator received the updated model value, which looks like a separate issue. I'll discuss with the team today and report.
Thanks @Viicos
I have to mention something
The model before validator should receive x='foo' (the old value), but it was getting {'x': 'bar'} (the new value).
The line I removed in model.rs was incorrectly adding the new field value to the dict passed to before validators. This was the root cause.
Here's my debug output showing the fix works:
model wrap validator (before): * <main.DummyModel object at 0x7c37075ddd30> <class 'main.DummyModel'>
RUST: old_dict from model.dict: {'x': 'foo', 'pydantic_extra': None, 'pydantic_private': None, 'pydantic_fields_set': {'x'}}
model before validator: {'x': 'foo', 'pydantic_extra': None, 'pydantic_private': None, 'pydantic_fields_set': {'x'}} <class 'dict'>
As you can see, the before validator now correctly receives x='foo' (the old value), matching the expected behavior from the issue.
...te during assignment
6791004 to
da2af49
Compare
which looks like a separate issue.
You're right - I think I got sidetracked while working on this.
I started from issue #11823 about the type inconsistency (before validators getting dict vs wrap validators getting instance), but I ended up fixing a different problem.
So my fix makes the values consistent between validator types, but doesn't solve the original dict vs instance type issue.
I force changed my commit because I referenced them to that issue.
Uh oh!
There was an error while loading. Please reload this page.
Fix: Model validator assignment consistency
Summary
Fixes inconsistency in model-level validator behavior during field assignment validation when
validate_assignment=True.Problem: During assignment operations,
@model_validator(mode='before')and@model_validator(mode='wrap')validators received different states:Solution: Modified assignment validation to ensure both validator modes consistently see the pre-update state.
Changes Made
Core Changes
src/validators/model.rs:input_dict.set_item(field_name, field_value)?inModelValidator::validate_assignmentBehavior Changes
Before this fix:
After this fix:
Testing
Manual Testing:
Regression Prevention:
Fixes
Closes #11823
Checklist
Type of change: 🐛 Bug fix
Affects: Model validators, assignment validation
Breaking: No