-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Bug
It seems the implementation of pydantic.generics.GenericModel does not support generic subclasses. Example:
import pydantic.generics
import typing
T = typing.TypeVar("T")
class ResponseModel(pydantic.generics.GenericModel, typing.Generic[T]):
data: List[T]
class PaginatedResponseModel(ResponseModel[T]):
total_count: int
ConcreteModel = PaginatedReponseModel[dict] # TypeError: Cannot parameterize a concrete instantiation of a generic model
PaginatedReponseModel is erroneously considered "concrete" even though type variables are used as parameters, and not concrete types.
It seems GenericModel.class_getitem does not check if any parameters are TypeVar, like typing.Generic does.
To support generic subclasses, __class_getitem__ should check if all of the parameters are fully concrete type(i.e. any of them are typing.TypeVars, or any of them are generic types that have non-concrete type parameters). If not, a concrete model should not be created.
I would consider this a bug since it does not follow the behavior of other typing.Generic usages, but it could also be a feature request.
Please complete:
- OS: Ubuntu 18.04.1
- Python version
import sys; print(sys.version): 3.7.1 (default, Oct 22 2018, 11:21:55)
[GCC 8.2.0] - Pydantic version
import pydantic; print(pydantic.VERSION):1.0