Skip to content
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

BaseModel.construct does not set aliased fields consistently with standard instantiation #4192

Closed
3 tasks done
kylebamos opened this issue Jun 28, 2022 · 0 comments · Fixed by #4191
Closed
3 tasks done
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@kylebamos
Copy link
Contributor

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.8.2
            pydantic compiled: False
                 install path: /Users/kyleamos/Library/Caches/pypoetry/virtualenvs/api-KwVDv1SR-py3.9/lib/python3.9/site-packages/pydantic
               python version: 3.9.1 (default, Jan 18 2022, 12:54:24)  [Clang 13.0.0 (clang-1300.0.29.30)]
                     platform: macOS-12.1-arm64-arm-64bit
     optional deps. installed: ['email-validator', 'typing-extensions']
from pydantic import BaseConfig, BaseModel, Field


class Foo(BaseModel):
    class Config(BaseConfig):
        allow_population_by_field_name = True

    bar_: int = Field(..., alias="bar")


expected_value = 2
assert Foo(bar=expected_value).bar_ == expected_value
assert Foo.construct(bar=expected_value).bar_ == expected_value  # AttributeError: 'Foo' object has no attribute 'bar_' 
...
@kylebamos kylebamos added the bug V1 Bug related to Pydantic V1.X label Jun 28, 2022
samuelcolvin pushed a commit that referenced this issue Aug 11, 2022
* Fix a bug where BaseModel.construct would not appropriately respect field aliases

* Perhaps do not raise on construct and just apply the fix

* Fix quotes and remove check on allow_population_by_field_name

* Fix lint

* Fix lint, remove bad arg

* Black formatted

* Mmmm black formatter and single quotes linting, what a world

* Added change file

* PR feedback

Co-authored-by: Kyle Amos <kamos@seatgeek.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant