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

Unable to Deserialize Model with BaseModel as Dictionary Keys #9453

Open
1 task done
gvariable opened this issue May 19, 2024 · 1 comment
Open
1 task done

Unable to Deserialize Model with BaseModel as Dictionary Keys #9453

gvariable opened this issue May 19, 2024 · 1 comment
Labels
bug V2 Bug related to Pydantic V2

Comments

@gvariable
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I am encountering an issue when trying to serialize and deserialize a Pydantic model that uses another BaseModel as dictionary keys. The deserialization process fails. Below is the code that reproduces the problem

Example Code

from pydantic import BaseModel, ConfigDict

class Cell(BaseModel):
    value: str

    model_config = ConfigDict(
        # make it hashable
        frozen=True,
    )

class WeightedSeeds(BaseModel):
    seeds: dict[Cell, int]

c = Cell(value="Hello World")
seed = WeightedSeeds(seeds={c: 100})
print(WeightedSeeds.model_validate_json(seed.model_dump_json())) # Fail

Python, Pydantic & OS Version

pydantic version: 2.7.1
        pydantic-core version: 2.18.2
          pydantic-core build: profile=release pgo=true
                 install path: /opt/homebrew/Caskroom/miniconda/base/envs/synthesis/lib/python3.12/site-packages/pydantic
               python version: 3.12.0 | packaged by conda-forge | (main, Oct  3 2023, 08:36:57) [Clang 15.0.7 ]
                     platform: macOS-14.1.1-arm64-arm-64bit
             related packages: typing_extensions-4.9.0 mypy-1.7.1 fastapi-0.111.0
                       commit: unknown
@gvariable gvariable added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 19, 2024
@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label May 21, 2024
@sydney-runkle
Copy link
Member

@gvariable,

Looks like a bug, will probably require some fixes in core. Thanks for reporting this!

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

No branches or pull requests

2 participants