mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
e7227db41a
* starting insert prints * working exec_script * remove prints, fix exec_examples.py * more cleanup of examples, better model printing * upgrade netlify runtime * extra docs deps * few more small tweaks
24 lines
627 B
Python
24 lines
627 B
Python
from typing import ClassVar, List, Union
|
|
|
|
from typing_extensions import Literal
|
|
|
|
from pydantic import BaseModel, ValidationError
|
|
|
|
class Cake(BaseModel):
|
|
kind: Literal['cake']
|
|
required_utensils: ClassVar[List[str]] = ['fork', 'knife']
|
|
|
|
class IceCream(BaseModel):
|
|
kind: Literal['icecream']
|
|
required_utensils: ClassVar[List[str]] = ['spoon']
|
|
|
|
class Meal(BaseModel):
|
|
dessert: Union[Cake, IceCream]
|
|
|
|
print(type(Meal(dessert={'kind': 'cake'}).dessert).__name__)
|
|
print(type(Meal(dessert={'kind': 'icecream'}).dessert).__name__)
|
|
try:
|
|
Meal(dessert={'kind': 'pie'})
|
|
except ValidationError as e:
|
|
print(str(e))
|