diff --git a/changes/842-zpencerq.rst b/changes/842-zpencerq.rst new file mode 100644 index 0000000..fcd1b34 --- /dev/null +++ b/changes/842-zpencerq.rst @@ -0,0 +1 @@ +Only check ``TypeVar`` param on base ``GenericModel`` class diff --git a/pydantic/generics.py b/pydantic/generics.py index ab3ea52..9380a28 100644 --- a/pydantic/generics.py +++ b/pydantic/generics.py @@ -27,7 +27,7 @@ class GenericModel(BaseModel): raise TypeError('Cannot parameterize a concrete instantiation of a generic model') if not isinstance(params, tuple): params = (params,) - if any(isinstance(param, TypeVar) for param in params): # type: ignore + if cls is GenericModel and any(isinstance(param, TypeVar) for param in params): # type: ignore raise TypeError(f'Type parameters should be placed on typing.Generic, not GenericModel') if Generic not in cls.__bases__: raise TypeError(f'Type {cls.__name__} must inherit from typing.Generic before being parameterized') diff --git a/tests/test_generics.py b/tests/test_generics.py index 4de02c8..8b13feb 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -174,6 +174,16 @@ def test_parameters_must_be_typevar(): assert str(exc_info.value) == f'Type parameters should be placed on typing.Generic, not GenericModel' +@skip_36 +def test_subclass_can_be_genericized(): + T = TypeVar('T') + + class Result(GenericModel, Generic[T]): + pass + + Result[T] + + @skip_36 def test_parameter_count(): T = TypeVar('T')