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

Instantiating a nested model with dict raises TypeError #8183

Closed
1 task done
dvnatanael opened this issue Nov 21, 2023 · 1 comment · Fixed by pydantic/pydantic-core#1089
Closed
1 task done

Instantiating a nested model with dict raises TypeError #8183

dvnatanael opened this issue Nov 21, 2023 · 1 comment · Fixed by pydantic/pydantic-core#1089
Assignees
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@dvnatanael
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When upgrading from pydantic 2.4.2 to 2.5.1, model fields of type BaseModel | Literal[True] or BaseModel | Literal[False] can no longer be instantiated using a model dict.

Traceback (most recent call last):
  File ".../test.py", line 10, in <module>
    Bar(foo={"x": 42})
  File ".../.pyenv/versions/test-pydantic25/lib/python3.11/site-packages/pydantic/main.py", line 164, in __init__
    __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
TypeError: unhashable type: 'dict'

Example Code

from pydantic import BaseModel
from typing import Literal

class Foo(BaseModel):
    x: int

class Bar(BaseModel):
    foo: Foo | Literal[True]

Bar(foo={"x": 42})  # TypeError: unhashable type: 'dict'

Python, Pydantic & OS Version

pydantic version: 2.5.1
        pydantic-core version: 2.14.3
          pydantic-core build: profile=release pgo=true
                 install path: .../.pyenv/versions/3.11.6/envs/test-pydantic25/lib/python3.11/site-packages/pydantic
               python version: 3.11.6 (main, Nov 21 2023, 10:15:54) [Clang 14.0.3 (clang-1403.0.22.14.1)]
                     platform: macOS-14.1.1-arm64-arm-64bit
             related packages: typing_extensions-4.8.0
@dvnatanael dvnatanael added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Nov 21, 2023
@sydney-runkle
Copy link
Member

Hi @dvnatanael,

This does look like a bug introduced by our most recent minor release. Thanks for reporting this. We'll look into a fix to get out with our next patch release.

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.

2 participants