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 doesn't properly parses models with custom root into custom root models #4883

Closed
5 of 15 tasks
gou177 opened this issue Dec 29, 2022 · 2 comments
Closed
5 of 15 tasks
Labels
bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable

Comments

@gou177
Copy link

gou177 commented Dec 29, 2022

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

This is because _enforce_dict_if_root() checks only for dicts with root key but not for models with root key.

Example Code

from pydantic import BaseModel

class Test(BaseModel):
    __root__: int

Test.parse_obj(dict(Test.parse_obj(1))) # works fine

Test.parse_obj(Test.parse_obj(1))

# Expected: Test(__root__=1)

# Actual: pydantic.error_wrappers.ValidationError: 1 validation error for Test
# __root__
#   value is not a valid integer (type=type_error.integer)

Python, Pydantic & OS Version

pydantic version: 1.10.2
            pydantic compiled: True
                 install path: C:\Users\gou17\AppData\Local\Programs\Python\Python310\Lib\site-packages\pydantic
               python version: 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]
                     platform: Windows-10-10.0.22621-SP0
     optional deps. installed: ['dotenv', 'email-validator', 'typing-extensions']

Affected Components

@gou177 gou177 added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Dec 29, 2022
gou177 added a commit to gou177/pydantic that referenced this issue Dec 29, 2022
gou177 added a commit to gou177/pydantic that referenced this issue Dec 29, 2022
gou177 added a commit to gou177/pydantic that referenced this issue Dec 29, 2022
@samuelcolvin
Copy link
Member

We're getting rid of custom root types in V2, and I doubt this can be changed in v1.10 now as I doubt it's a new bug.

Sorry.

@samuelcolvin
Copy link
Member

Changed my mind, see #4884.

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 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

No branches or pull requests

2 participants