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

v2.7.0 TypeError in model_construct where field has a validation_alias with AliasChoices #9216

Closed
1 task done
twispt opened this issue Apr 11, 2024 · 4 comments · Fixed by #9223
Closed
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@twispt
Copy link

twispt commented Apr 11, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Using model_construct on a model that has a field with AliasChoices for the validation_alias throws a TypeError exception. This issue is new to v2.7.0. Example code works with v2.6.4.

Traceback (most recent call last):
  File "/home/bryan/src/temp/pydantic-regression/my.py", line 6, in <module>
    FooBase.model_construct().dict()
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/twispt/src/temp/pydantic-regression/.venv/lib/python3.11/site-packages/pydantic/main.py", line 228, in model_construct
    elif field.validation_alias is not None and field.validation_alias in values:
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'AliasChoices'

Example Code

from pydantic import BaseModel, Field, AliasChoices

class FooBase(BaseModel):
  foo: str = Field(serialization_alias='bar', validation_alias=AliasChoices('bar', 'foo'))

FooBase.model_construct().dict()

Python, Pydantic & OS Version

pydantic version: 2.7.0
        pydantic-core version: 2.18.1
          pydantic-core build: profile=release pgo=true
                 install path: /home/twispt/src/temp/pydantic-regression/.venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.8 (main, Feb 12 2024, 14:50:05) [GCC 13.2.1 20230801]
                     platform: Linux-6.6.25-1-MANJARO-x86_64-with-glibc2.39
             related packages: typing_extensions-4.11.0
                       commit: unknown
@twispt twispt added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 11, 2024
@twispt twispt changed the title TypeError in model_construct where field has a validation_alias with AliasChoices v2.7.0 TypeError in model_construct where field has a validation_alias with AliasChoices Apr 11, 2024
@kevanpigott
Copy link

I am running into the same issue, and can confirm the code works on v2.6.4 but not on v2.7.0
in v2.6.4 i do not see a reference to field.validation_alias in model_consruct whereas the check occurs in v2.7.0

a potential solution would be to replace the line

elif field.validation_alias is not None and field.validation_alias in values:

with

any(alias in values for alias in field.validation_alias.choices)

This checks if any alias choice is in the values field, but does not check if multiple choices are defined in the value field.

If that isnt checked elsewhere than i would use

sum(alias in values for alias in field.validation_alias.choices) == 1:

@sydney-runkle
Copy link
Member

@twispt,

Thanks for reporting this! We'll work on a fix and get a patch release out early next week!

@sydney-runkle
Copy link
Member

I believe this was introduced with #9144

@sydney-runkle
Copy link
Member

Working on a fix now and will have a PR up shortly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants