Skip to content

Commit

Permalink
Resolve type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
diabolo-dan committed Mar 19, 2021
1 parent dc23c0e commit b731c84
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pydantic/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
GenericModelT = TypeVar('GenericModelT', bound='GenericModel')
TypeVarType = Any # since mypy doesn't allow the use of TypeVar as a type

_assigned_parameters: Dict[Type[Any], Dict[TypeVarType, Any]] = {}


class GenericModel(BaseModel):
__slots__ = ()
Expand Down Expand Up @@ -91,7 +93,7 @@ def __class_getitem__(cls: Type[GenericModelT], params: Union[Type[Any], Tuple[T
),
)

created_model._assigned_parameters = typevars_map
_assigned_parameters[created_model] = typevars_map

if called_globally: # create global reference and therefore allow pickling
object_by_reference = None
Expand Down Expand Up @@ -166,20 +168,23 @@ class B(A[V], Generic[V]):
then: `A[int] in B.__concrete_bases__({V: int})`
```
"""
if '_assigned_parameters' in cls.__dict__:
if cls in _assigned_parameters:
# class is a partially "bound" form of a generic model
# We need to determine the mapping for the base_model parameters
mapped_types = {key: typevars_map.get(value, value) for key, value in cls._assigned_parameters.items()}
mapped_types: Mapping[TypeVarType, Type[Any]] = {
key: typevars_map.get(value, value) for key, value in _assigned_parameters[cls].items()
}
else:
mapped_types = typevars_map
for base_model in cls.__bases__:
if issubclass(base_model, GenericModel) and issubclass(base_model, Generic):
if not base_model.__parameters__:
if issubclass(base_model, GenericModel):
if not hasattr(base_model, '__parameters__') or not base_model.__parameters__:
# type is Generic or
# base model is already concrete, and will be included transitively.
continue
else:
base_parameters = tuple([mapped_types[param] for param in base_model.__parameters__])
result = base_model[base_parameters]
result = base_model.__class_getitem__(base_parameters)
if result != base_model and result != cls:
yield result

Expand Down

0 comments on commit b731c84

Please sign in to comment.