Код: Выделить всё
from typing import Literal, Union
from pydantic import BaseModel, Field, ValidationError
class Cat(BaseModel):
pet_type: Literal['cat']
meows: int
class Dog(BaseModel):
pet_type: Literal['dog']
barks: float
class Lizard(BaseModel):
pet_type: Literal['reptile', 'lizard']
scales: bool
class Model(BaseModel):
pet: Union[Cat, Dog, Lizard] = Field(..., discriminator='pet_type')
n: int
print(Model(pet={'pet_type': 'dog', 'barks': 3.14, 'eats': 'biscuit'}, n=1))
""" try:
Model(pet={'pet_type': 'dog'}, n=1)
except ValidationError as e:
print(e) """
И есть ли шанс, что мы сможем предоставить входные данные напрямую вместо объекта pet?
Код: Выделить всё
print(Model({'pet_type': 'dog', 'barks': 3.14, 'eats': 'biscuit', n=1})). Я пробовал без дескриминатора
Подробнее здесь: https://stackoverflow.com/questions/718 ... -in-schema