Sets and frozenset
set
- allows
list
, tuple
, set
, frozenset
, deque
, or generators and casts to a set;
when a generic parameter is provided, the appropriate validation is applied to all items of the set
typing.Set
- handled the same as
set
above
frozenset
- allows
list
, tuple
, set
, frozenset
, deque
, or generators and casts to a frozen set;
when a generic parameter is provided, the appropriate validation is applied to all items of the set
typing.FrozenSet
- handled the same as
frozenset
above
from typing import FrozenSet, Optional, Set
from pydantic import BaseModel
class Model(BaseModel):
simple_set: Optional[set] = None
set_of_ints: Optional[Set[int]] = None
simple_frozenset: Optional[frozenset] = None
frozenset_of_ints: Optional[FrozenSet[int]] = None
print(Model(simple_set={'1', '2', '3'}).simple_set)
#> {'1', '2', '3'}
print(Model(simple_set=['1', '2', '3']).simple_set)
#> {'1', '2', '3'}
print(Model(set_of_ints=['1', '2', '3']).set_of_ints)
#> {1, 2, 3}
m1 = Model(simple_frozenset=['1', '2', '3'])
print(type(m1.simple_frozenset))
#> <class 'frozenset'>
print(sorted(m1.simple_frozenset))
#> ['1', '2', '3']
m2 = Model(frozenset_of_ints=['1', '2', '3'])
print(type(m2.frozenset_of_ints))
#> <class 'frozenset'>
print(sorted(m2.frozenset_of_ints))
#> [1, 2, 3]