mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Support use of TypeVar on generic subclasses (#842)
* Add failing generic subclass test * Only raise type parameter failure on base GenericModel class * Add changes to describe PR #842 * Change the class check to use is * Fix formatting in subclass test * correct change
This commit is contained in:
committed by
Samuel Colvin
parent
6f97c0f9f7
commit
253983559b
@@ -0,0 +1 @@
|
||||
Only check ``TypeVar`` param on base ``GenericModel`` class
|
||||
@@ -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')
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user