Skip to content

Pydantic doesn't properly parses models with custom root into custom root models #4883

@gou177

Description

@gou177

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.XpendingIs unconfirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions