mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
25 lines
474 B
Python
25 lines
474 B
Python
from typing import Generic, TypeVar
|
|
|
|
from pydantic import ValidationError
|
|
from pydantic.generics import GenericModel
|
|
|
|
T = TypeVar('T')
|
|
|
|
|
|
class InnerT(GenericModel, Generic[T]):
|
|
inner: T
|
|
|
|
|
|
class OuterT(GenericModel, Generic[T]):
|
|
outer: T
|
|
nested: InnerT[T]
|
|
|
|
|
|
nested = InnerT[int](inner=1)
|
|
print(OuterT[int](outer=1, nested=nested))
|
|
try:
|
|
nested = InnerT[str](inner='a')
|
|
print(OuterT[int](outer='a', nested=nested))
|
|
except ValidationError as e:
|
|
print(e)
|