Skip to content

Model's dict method that contains set of models. #1090

@AlbertMukhammadiev

Description

@AlbertMukhammadiev

Bug

Please complete:

  • OS: 19.10
  • Python version import sys; print(sys.version): 3.6.8 [GCC 9.2.1 20191008]
  • Pydantic version import pydantic; print(pydantic.VERSION): 1.2

I want to create model Foo, that contains collection of Bars:

from typing import Set, Tuple, List
from pydantic import BaseModel


class Bar(BaseModel):
    bar: str = 'bar_value'

    def __hash__(self):
        return self.bar.__hash__()


class FooTuple(BaseModel):
    bars: Tuple[Bar]


class FooSet(BaseModel):
    bars: Set[Bar]


class FooList(BaseModel):
    bars: List[Bar]


if __name__ == '__main__':
    FooList(bars=[Bar()]).dict()
    FooTuple(bars=(Bar(),)).dict()
    FooSet(bars={Bar()}).dict()

...

Dict method recursively converts models to dictionaries and tries to put them in the same containers they were in. It falls down for Set because of dict type is unhashable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions