Add support for nested generics (#1104)

* Add support for nested generics

* Allow instantiation of unparameterized generics

* Add better more partial instantiation tests

* Add changes

* Add docs
This commit is contained in:
David Montague
2020-01-10 13:06:52 -08:00
committed by Samuel Colvin
parent dea8ac2b09
commit dbc044e357
6 changed files with 272 additions and 39 deletions
+21
View File
@@ -0,0 +1,21 @@
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)