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

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

Closed
3 tasks done
pboehm opened this issue Mar 24, 2021 · 1 comment · Fixed by #2582
Closed
3 tasks done

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

pboehm opened this issue Mar 24, 2021 · 1 comment · Fixed by #2582
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@pboehm
Copy link

pboehm commented Mar 24, 2021

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!

@pboehm pboehm added the bug V1 Bug related to Pydantic V1.X label Mar 24, 2021
@PrettyWood
Copy link
Member

Hi @pboehm!
Thank you for reporting with a clear example AND for checking open fixes like #2536. Always nice to spare some time 🙏
You're absolutely right and the fix has in fact nothing to do with schema generation per se. I opened a fix I'll see if fastapi tests are ok

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
Development

Successfully merging a pull request may close this issue.

2 participants