forked from pydantic/pydantic
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for nested generics (pydantic#1104)
* Add support for nested generics * Allow instantiation of unparameterized generics * Add better more partial instantiation tests * Add changes * Add docs
- Loading branch information
1 parent
d943d95
commit 0b37746
Showing
6 changed files
with
272 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add support for nested generic models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Generic, TypeVar | ||
|
||
from pydantic import ValidationError | ||
from pydantic.generics import GenericModel | ||
|
||
AT = TypeVar('AT') | ||
BT = TypeVar('BT') | ||
|
||
class Model(GenericModel, Generic[AT, BT]): | ||
a: AT | ||
b: BT | ||
|
||
print(Model(a='a', b='a')) | ||
|
||
IntT = TypeVar('IntT', bound=int) | ||
typevar_model = Model[int, IntT] | ||
print(typevar_model(a=1, b=1)) | ||
try: | ||
typevar_model(a='a', b='a') | ||
except ValidationError as exc: | ||
print(exc) | ||
|
||
concrete_model = typevar_model[int] | ||
print(concrete_model(a=1, b=1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters