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

Pydantic can't parse literal values from json via (model_)validate_json method #8133

Closed
1 task done
ThirVondukr opened this issue Nov 15, 2023 · 4 comments
Closed
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@ThirVondukr
Copy link

ThirVondukr commented Nov 15, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Pydantic fails when trying to parse literal values from dict/mapping in json

Example Code

from typing import Literal

from pydantic import TypeAdapter

literal = Literal["a", "b"]


dict_adapter = TypeAdapter(dict[literal, int])
dict_adapter.validate_python({"a": 42})  # Ok
dict_adapter.validate_json('{"a": 42}')  # Raises an error

Python, Pydantic & OS Version

$ pdm show pydantic
Name:                  pydantic
Latest version:        2.5.0
Latest stable version: 2.5.0
Installed version:     2.5.0
$ pdm show pydantic-core
Name:                  pydantic_core                                      
Latest version:        2.14.3
Latest stable version: 2.14.3
Installed version:     2.14.1
@ThirVondukr ThirVondukr added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Nov 15, 2023
@Viicos
Copy link
Contributor

Viicos commented Nov 15, 2023

{42: "a"} is not valid JSON (keys can only be strings).

But I'm able to reproduce the second example (this is a regression from 2.4.2)

@ThirVondukr
Copy link
Author

@Viicos My bad, I thought it fails with literal values too, but that only seems to be the case for keys, I adjusted the MRE

@sydney-runkle sydney-runkle added this to the v2.5.2 milestone Nov 15, 2023
@sydney-runkle
Copy link
Member

@ThirVondukr,

I can confirm this is a bug introduced by 2.5 as well. We'll work on a fix for this for our 2.5.2 patch release which we'll get out ASAP!

@sydney-runkle
Copy link
Member

Closing this as we've merged the related PR in pydantic-core.

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

3 participants