-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix(validate-assignment): do not validate extra fields when validate_assignment is on
#724
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
fix(validate-assignment): do not validate extra fields when validate_assignment is on
#724
Conversation
Codecov Report
@@ Coverage Diff @@
## v0.32 #724 +/- ##
====================================
Coverage 100% 100%
====================================
Files 15 15
Lines 2727 2729 +2
Branches 539 541 +2
====================================
+ Hits 2727 2729 +2 |
…assignment` is on
As I'm basing off of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please base of the v0.32 branch.
pydantic/main.py
Outdated
| self.__fields_set__.add(name) | ||
| known_field = self.fields.get(name, None) | ||
| if known_field: | ||
| value_, error_ = known_field.validate(value, self.dict(exclude={name}), loc=name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| value_, error_ = known_field.validate(value, self.dict(exclude={name}), loc=name) | |
| value, error_ = known_field.validate(value, self.dict(exclude={name}), loc=name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should have a test that would catch this, please add.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch with value instead of value_!
Mea culpa!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please can you add a test for this so similar problem doesn't happen again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should have a test that would catch this, please add.
@samuelcolvin What exactly do you mean by this?
We already have test_validating_assignment_ok() and test_validating_assignment_fail() in test_validators.py, and test_validate_assignment() and test_validate_assignment_error() in test_dataclasses.py to check known_field behaviour.
I only added tests for unknown extra values.
What scenario do we need to test additionally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see.
value vs. value_.
So we need some validator that changes the value. Like trimming the input, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I guess something like
def test_change_assignment():
class Model(BaseModel):
a: int
@validator('a')
def double_a(cls, v):
return v * 2
class Config:
validate_assignment = True
m = Model(a=1)
assert m.a == 2
m.a = 4
assert m.a == 8I know it's not technically part of the original change here, but I think if you catch a problem like this by chance it's imperative that you add a test that would automatically catch it next time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@samuelcolvin It is already based off of Is that enough? |
|
great, thanks. Since this isn't a new bug I assume it isn't critically urgent. I'll wait a few days in case anyone else finds problems with |
We a actually a little bit blocked by it. 😅 |
|
Okay, I'll release now. |
|
There's a minor typo there. 😅
It should be |
|
will fix. |
|
Cool! |
vaidate_assignment is onvalidate_assignment is on
|
#723? |
Change Summary
Do not validate extra fields when
validate_assignmentoption is set toTrue.This prevent
KeyErrorto be raised when trying to add an extra field.Also, it seems more logical to validate only those fields that are explicitly set for the model.
See related bug description for details.
Related issue number
#723
Checklist
changes/,see changes/README.md for details
on the format