Skip to content

GenericModel does not support generic subclasses #947

@DrPyser

Description

@DrPyser

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions