Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RecursionError when using Literal types in GenericModel #2454

Closed
espdev opened this issue Mar 2, 2021 · 2 comments · Fixed by #2438
Closed

RecursionError when using Literal types in GenericModel #2454

espdev opened this issue Mar 2, 2021 · 2 comments · Fixed by #2438
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@espdev
Copy link

espdev commented Mar 2, 2021

Bug

If we are trying to use Literal types in GenericModel we are getting RecursionError: maximum recursion depth exceeded in comparison.

The minimal code example:

from typing import Literal, Dict, TypeVar, Generic
from pydantic.generics import GenericModel

Fields = Literal['foo', 'bar']

FieldType = TypeVar('FieldType')
ValueType = TypeVar('ValueType')

class GModel(GenericModel, Generic[FieldType, ValueType]):
    field: Dict[FieldType, ValueType]

GModelType = GModel[Fields, str]

The traceback:

Traceback (most recent call last):
  File "scratch_101.py", line 12, in <module>
    GModelType = GModel[Fields, str]
  File "virtualenvs\foobar-HGIuaRl7-py3.9\lib\site-packages\pydantic\generics.py", line 110, in __class_getitem__
    {param: None for param in iter_contained_typevars(typevars_map.values())}
  File "virtualenvs\foobar-HGIuaRl7-py3.9\lib\site-packages\pydantic\generics.py", line 110, in <dictcomp>
    {param: None for param in iter_contained_typevars(typevars_map.values())}
  File "virtualenvs\foobar-HGIuaRl7-py3.9\lib\site-packages\pydantic\generics.py", line 216, in iter_contained_typevars
    yield from iter_contained_typevars(var)
  File "virtualenvs\foobar-HGIuaRl7-py3.9\lib\site-packages\pydantic\generics.py", line 220, in iter_contained_typevars
    yield from iter_contained_typevars(arg)
  File "virtualenvs\foobar-HGIuaRl7-py3.9\lib\site-packages\pydantic\generics.py", line 216, in iter_contained_typevars
    yield from iter_contained_typevars(var)
  File "virtualenvs\foobar-HGIuaRl7-py3.9\lib\site-packages\pydantic\generics.py", line 216, in iter_contained_typevars
    yield from iter_contained_typevars(var)
  File "virtualenvs\foobar-HGIuaRl7-py3.9\lib\site-packages\pydantic\generics.py", line 216, in iter_contained_typevars
    yield from iter_contained_typevars(var)
  [Previous line repeated 982 more times]
  File "virtualenvs\foobar-HGIuaRl7-py3.9\lib\site-packages\pydantic\generics.py", line 214, in iter_contained_typevars
    elif isinstance(v, Iterable):
  File "C:\Programs\Python\Python39_x64\lib\typing.py", line 657, in __instancecheck__
    return self.__subclasscheck__(type(obj))
  File "C:\Programs\Python\Python39_x64\lib\typing.py", line 789, in __subclasscheck__
    return issubclass(cls, self.__origin__)
  File "C:\Programs\Python\Python39_x64\lib\abc.py", line 102, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
RecursionError: maximum recursion depth exceeded in comparison

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

pydantic version: 1.8
        pydantic compiled: True
             install path: /home/esp/.cache/pypoetry/virtualenvs/foobar-n-Q38lsL-py3.9/lib/python3.9/site-packages/pydantic
           python version: 3.9.1 (default, Dec  8 2020, 02:26:20)  [GCC 9.3.0]
                 platform: Linux-4.4.0-19041-Microsoft-x86_64-with-glibc2.31
 optional deps. installed: ['typing-extensions']
@espdev espdev added the bug V1 Bug related to Pydantic V1.X label Mar 2, 2021
@PrettyWood
Copy link
Member

PrettyWood commented Mar 2, 2021

Hi @espdev thanks for reporting!
Does #2438 solve your issue?
I reckon we should update the change description of the fix 😬

EDIT: Just checked. It does :)

@espdev
Copy link
Author

espdev commented Mar 2, 2021

@PrettyWood Great! I am closing the issue because it has already been fixed in PR. Thanks!

@espdev espdev closed this as completed Mar 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
2 participants