Skip to content

Broken loading list of tuples. #1190

@DMantis

Description

@DMantis

Bug

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

pydantic version: 1.4
pydantic compiled: True
install path: /home/**removed**/venv/lib/python3.7/site-packages/pydantic
python version: 3.7.3 (default, Apr  3 2019, 19:16:38)  [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
platform: Linux-4.19.86-041986-generic-x86_64-with-Ubuntu-18.04-bionic
     optional deps. installed: []

There is pretty strange behaviour on loading nested list of tuples. I firstly think that this might be intended, but then found out that parse_obj and parse_obj_as give different execution flow which frustrates me.

from pydantic import BaseModel

class OperationData(BaseModel):
    id: str

class Operation(BaseModel):
    __root__: Tuple[int, OperationData]

data = [0, {'id': '1.11.0'}]
# this one works as expected
print(Operation.parse_obj(data))
# printed: __root__=(0, OperationData(id='1.11.0'))

# However, this one doesn't
print(parse_obj_as(Operation, data))
# Traceback (most recent call last):
#  File "/home/**removed**/protocol/base.py", line 238, in <module>
#    print(parse_obj_as(Operation, data))
#  File "pydantic/tools.py", line 35, in pydantic.tools.parse_obj_as
#  File "pydantic/main.py", line 283, in pydantic.main.BaseModel.__init__
# pydantic.error_wrappers.ValidationError: 1 validation error for ParsingModel[Operation]
#__root__
# value is not a valid dict (type=type_error.dict)

# Which is not a big problem. The problem is that I have nested class

class OperationsBatch(BaseModel):
    batch_desc: str
    operations: List[Operation]

# and it produces same exception on
print(OperationsBatch.parse_obj({'batch_desc': '123', 'operations': [data, data]}))

# Traceback (most recent call last):
# File "/home/**removed**/protocol/base.py", line 243, in <module>
#    OperationsBatch.parse_obj({'batch_desc': '123', 'operations': [data, data]})
#  File "pydantic/main.py", line 402, in pydantic.main.BaseModel.parse_obj
#  File "pydantic/main.py", line 283, in pydantic.main.BaseModel.__init__
# pydantic.error_wrappers.ValidationError: 2 validation errors for OperationsBatch
# operations -> 0
# value is not a valid dict (type=type_error.dict)
# operations -> 1
# value is not a valid dict (type=type_error.dict)

It doesn't look like a right behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions