Skip to content

Schema generation with GenericModel and multiple enums is broken since 1.8.0 #2575

@pboehm

Description

@pboehm

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Since pydantic 1.8.0 schema generation with GenericModel and multiple Enum fields using this GenericModel fails with a KeyError. I have tried the fix in #2536 but this seems to be another corner case that has to be fixed.

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

             pydantic version: 1.8.1
            pydantic compiled: True
                 install path: /Users/philipp/.pyenv/versions/3.7.10/envs/pydantic_schema_error@3.7.10/lib/python3.7/site-packages/pydantic
               python version: 3.7.10 (default, Mar  4 2021, 21:37:12)  [Clang 12.0.0 (clang-1200.0.32.29)]
                     platform: Darwin-20.3.0-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']
from enum import Enum
from typing import TypeVar, Generic, Optional
from pydantic import BaseModel
from pydantic.generics import GenericModel

ConfigValueT = TypeVar("ConfigValueT")


class ConfigOption(GenericModel, Generic[ConfigValueT]):
    value: ConfigValueT
    value_source: str


class EnumA(str, Enum):
    a = "a"
    aa = "aa"


class EnumB(str, Enum):
    b = "b"
    bb = "bb"


class TestModel(BaseModel):
    option_a: ConfigOption[EnumA]
    option_b: ConfigOption[EnumB]  # it also works with only `option_a` but not with multiple


print(TestModel.schema_json())  # This works until 1.7.3 but crashes on 1.8+

This results in a KeyError:

Traceback (most recent call last):
  File "test.py", line 29, in <module>
    print(TestModel.schema_json())  # This works until 1.7.3 but crashes on 1.8+
  File "pydantic/main.py", line 715, in pydantic.main.BaseModel.schema_json
  File "pydantic/main.py", line 704, in pydantic.main.BaseModel.schema
  File "pydantic/schema.py", line 167, in pydantic.schema.model_schema
  File "pydantic/schema.py", line 548, in pydantic.schema.model_process_schema
  File "pydantic/schema.py", line 589, in pydantic.schema.model_type_schema
  File "pydantic/schema.py", line 241, in pydantic.schema.field_schema
  File "pydantic/schema.py", line 495, in pydantic.schema.field_type_schema
  File "pydantic/schema.py", line 839, in pydantic.schema.field_singleton_schema
KeyError: <class '__main__.ConfigOption[str]'>

Thank you for your work on this amazing library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions