Skip to content

Commit

Permalink
whitelist iterables
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Mar 2, 2021
1 parent 92b5d0c commit 5c5c106
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pydantic/generics.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import sys
import typing
from enum import Enum
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Dict,
Generic,
Iterable,
Iterator,
List,
Mapping,
Expand Down Expand Up @@ -206,13 +204,16 @@ def check_parameters_count(cls: Type[GenericModel], parameters: Tuple[Any, ...])
raise TypeError(f'Too {description} parameters for {cls.__name__}; actual {actual}, expected {expected}')


DictValues: Type[Any] = {}.values().__class__


def iter_contained_typevars(v: Any) -> Iterator[TypeVarType]:
"""Recursively iterate through all subtypes and type args of `v` and yield any typevars that are found."""
if isinstance(v, TypeVar):
yield v
elif hasattr(v, '__parameters__') and not get_origin(v) and lenient_issubclass(v, GenericModel):
yield from v.__parameters__
elif isinstance(v, Iterable) and not lenient_issubclass(v, Enum):
elif isinstance(v, (DictValues, list)):
for var in v:
yield from iter_contained_typevars(var)
else:
Expand Down

0 comments on commit 5c5c106

Please sign in to comment.