mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
aa5e8c60b7
* Better error for unsuported "typing" objects. Fixes #745 Add a better error message for fields with types from the typing module that are not directly supported by Pydantic. Previously, it caused a cryptic assertion failure. * Add support for frozenset fields. Also provide an example of their usage. Fixes #745 * Address review comments. Fixes #745 * use equals not "is" for int comparison.
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
from typing import Dict, FrozenSet, List, Optional, Sequence, Set, Tuple, Union
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
class Model(BaseModel):
|
|
simple_list: list = None
|
|
list_of_ints: List[int] = None
|
|
|
|
simple_tuple: tuple = None
|
|
tuple_of_different_types: Tuple[int, float, str, bool] = None
|
|
|
|
simple_dict: dict = None
|
|
dict_str_float: Dict[str, float] = None
|
|
|
|
simple_set: set = None
|
|
set_bytes: Set[bytes] = None
|
|
frozen_set: FrozenSet[int] = None
|
|
|
|
str_or_bytes: Union[str, bytes] = None
|
|
none_or_str: Optional[str] = None
|
|
|
|
sequence_of_ints: Sequence[int] = None
|
|
|
|
compound: Dict[Union[str, bytes], List[Set[int]]] = None
|
|
|
|
print(Model(simple_list=['1', '2', '3']).simple_list) # > ['1', '2', '3']
|
|
print(Model(list_of_ints=['1', '2', '3']).list_of_ints) # > [1, 2, 3]
|
|
|
|
print(Model(simple_dict={'a': 1, b'b': 2}).simple_dict) # > {'a': 1, b'b': 2}
|
|
print(Model(dict_str_float={'a': 1, b'b': 2}).dict_str_float) # > {'a': 1.0, 'b': 2.0}
|
|
|
|
print(Model(simple_tuple=[1, 2, 3, 4]).simple_tuple) # > (1, 2, 3, 4)
|
|
print(Model(tuple_of_different_types=[4, 3, 2, 1]).tuple_of_different_types) # > (4, 3.0, '2', True)
|
|
|
|
print(Model(sequence_of_ints=[1, 2, 3, 4]).sequence_of_ints) # > [1, 2, 3, 4]
|
|
print(Model(sequence_of_ints=(1, 2, 3, 4)).sequence_of_ints) # > (1, 2, 3, 4)
|