-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
436 additions
and
35 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
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
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 TypeVar, Type, List, Dict, Generic, Optional | ||
from pydantic.generics import GenericModel | ||
|
||
|
||
AT = TypeVar('AT') | ||
BT = TypeVar('BT') | ||
CT = TypeVar('CT') | ||
DT = TypeVar('DT') | ||
ET = TypeVar('ET') | ||
|
||
|
||
class A(GenericModel, Generic[AT, BT, CT, DT]): | ||
a: Type[AT] | ||
b: List[BT] | ||
c: Dict[CT, DT] | ||
|
||
class B(A[int, BT, CT, DT], Generic[BT, CT, DT, ET]): | ||
hij: Optional[ET] | ||
|
||
|
||
B[str, float, bytes, bool]().<caret> |
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 TypeVar, Type, List, Dict, Generic, Optional | ||
from pydantic.generics import GenericModel | ||
|
||
|
||
AT = TypeVar('AT') | ||
BT = TypeVar('BT') | ||
CT = TypeVar('CT') | ||
DT = TypeVar('DT') | ||
ET = TypeVar('ET') | ||
|
||
|
||
class A(GenericModel, Generic[AT, BT, CT, DT]): | ||
a: Type[AT] | ||
b: List[BT] | ||
c: Dict[CT, DT] | ||
|
||
class B(A[int, BT, CT, DT], Generic[BT, CT, DT, ET]): | ||
hij: Optional[ET] | ||
|
||
|
||
B[str, float, bytes, bool](<caret>) |
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,38 @@ | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
ClassVar, | ||
Dict, | ||
Generic, | ||
Iterator, | ||
List, | ||
Mapping, | ||
Optional, | ||
Tuple, | ||
Type, | ||
TypeVar, | ||
Union, | ||
cast, | ||
get_type_hints, | ||
) | ||
|
||
from .main import BaseModel | ||
|
||
_generic_types_cache: Dict[Tuple[Type[Any], Union[Any, Tuple[Any, ...]]], Type[BaseModel]] = {} | ||
GenericModelT = TypeVar('GenericModelT', bound='GenericModel') | ||
TypeVarType = Any # since mypy doesn't allow the use of TypeVar as a type | ||
|
||
|
||
class GenericModel(BaseModel): | ||
__slots__ = () | ||
__concrete__: ClassVar[bool] = False | ||
|
||
if TYPE_CHECKING: | ||
# Putting this in a TYPE_CHECKING block allows us to replace `if Generic not in cls.__bases__` with | ||
# `not hasattr(cls, "__parameters__")`. This means we don't need to force non-concrete subclasses of | ||
# `GenericModel` to also inherit from `Generic`, which would require changes to the use of `create_model` below. | ||
__parameters__: ClassVar[Tuple[TypeVarType, ...]] | ||
|
||
# Setting the return type as Type[Any] instead of Type[BaseModel] prevents PyCharm warnings | ||
def __class_getitem__(cls: Type[GenericModelT], params: Union[Type[Any], Tuple[Type[Any], ...]]) -> Type[Any]: | ||
pass |
Oops, something went wrong.