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:
Spencer Ellinor
2019-09-30 06:30:14 -07:00
committed by Samuel Colvin
parent 6f97c0f9f7
commit 253983559b
3 changed files with 12 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
Only check ``TypeVar`` param on base ``GenericModel`` class
+1 -1
View File
@@ -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')
+10
View File
@@ -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')