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 #4192 bug with BaseModel.construct and aliased Fields #4191
Fix #4192 bug with BaseModel.construct and aliased Fields #4191
Conversation
[True, True, 'bar', does_not_raise()], | ||
[True, True, 'bar_', does_not_raise()], | ||
[True, False, 'bar', does_not_raise()], | ||
[True, False, 'bar_', does_not_raise()], |
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.
If construct were to behave consistently with respect to allow_population_by_field_name_config
, this would raise a ValueError
, which is how I had the behavior in my initial commit, but that would be a breaking change, so I decided against it.
please review |
Thanks @kylebamos for your patch 👍 Here is my understanding. from pydantic import BaseConfig, BaseModel, Field
class Foo1(BaseModel):
class Config(BaseConfig):
allow_population_by_field_name = True
bar_: int = Field(..., alias="bar")
class Foo2(BaseModel):
class Config(BaseConfig):
allow_population_by_field_name = False
bar_: int = Field(..., alias="bar")
expected_value = 2 Here is the current state:
# 1 - OK
assert Foo1(bar=expected_value).bar_ == expected_value
# 2 - OK
assert Foo2(bar=expected_value).bar_ == expected_value
# 3 - AttributeError: 'Foo' object has no attribute 'bar_' -- Has to be fixed
assert Foo1.construct(bar=expected_value).bar_ == expected_value
# 4 - AttributeError: 'Foo' object has no attribute 'bar_' -- Ok
assert Foo2.construct(bar=expected_value).bar_ == expected_value
# 5 - OK
assert Foo1(bar_=expected_value).bar_ == expected_value
# 6 - ValidationError: 1 validation error for Foo2 bar -- OK
assert Foo2(bar_=expected_value).bar_ == expected_value
# 7 - OK
assert Foo1.construct(bar_=expected_value).bar_ == expected_value
# 8 - OK
assert Foo2.construct(bar_=expected_value).bar_ == expected_value And your patch will fix the Regarding |
Please update |
Regarding case 8 you highlighted above, I addressed that in my comment as well because I initially had it fixed to accommodate that scenario, but it does break other tests. https://github.com/samuelcolvin/pydantic/pull/4191/files#r909024428 It's as simple as changing line 592 in and adding |
please review |
@kylebamos I have to say I find your avatar very confusing, I keep thinking there are lots of negative comments and reviews. I'm marginally pro this change, my concern is whether it could be a breaking change to anyone. @PrettyWood do you have any thoughts? Also conflicts needs solving, please update. |
Conflict resolved. Avatar fixed to your liking, I hope. |
Please review |
Much better 👍 I'm sure I'm not the only one who will prefer it 😏. |
LGTM! Good for v1.10! |
@kylebamos thanks so much for this. |
Change Summary
Update BaseModel.construct to consider field aliases when constructing.
Related issue number
fix #4192
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)