8000 Missing downwards compatibility and bug with custom root types · Issue #2449 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Missing downwards compatibility and bug with custom root types #2449

@camold

Description

@camold
from typing import Generic
from typing import TypeVar
from typing import List
from pydantic.generics import GenericModel
from pydantic import BaseModel

T = TypeVar("T")
class BaseList(GenericModel, Generic[T]):
     __root__: List[T]

class Test(BaseModel):
     mylist: BaseList[int]

Test(mylist=[1,2,3,4])
# Test(mylist=BaseList[int](__root__=[1, 2, 3, 4]))

Test(mylist=BaseList[int](__root__=[1,2,3,4]))
# ---------------------------------------------------------------------------
# ValidationError                           Traceback (most recent call last)
# <ipython-input-10-c373af038c5b> in <module>
# ----> 1 Test(mylist=BaseList[int](__root__=[1,2,3,4]))
# 
# /usr/local/lib/python3.8/dist-packages/pydantic/main.cpython-38-x86_64-linux-gnu.so in pydantic.main.BaseModel.__init__()
# 
# ValidationError: 1 validation error for Test
# mylist -> __root__
#   value is not a valid list (type=type_error.list)

Certainly no error with pydantic 1.6.1 (I tested this locally).
Probably also no errors with versions <1.8 because we have automatic tests in place with the newest package versions and these tests only fired yesterday.

On a first glance the documentation does not seem to have changed - so I assume also the behaviour should not have changed with the 1.8 release. See https://pydantic-docs.helpmanual.io/usage/models/#generic-models and
https://pydantic-docs.helpmanual.io/usage/models/#custom-root-types

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.8
            pydantic compiled: True
                 install path: /usr/local/lib/python3.8/dist-packages/pydantic
               python version: 3.8.0 (default, Oct 28 2019, 16:14:01)  [GCC 8.3.0]
                     platform: Linux-5.4.0-62-generic-x86_64-with-glibc2.27
     optional deps. installed: ['devtools', 'email-validator', 'typing-extensions']
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0