-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X
Description
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
I found what seems to be a regression in Pydantic 1.8+.
When a model has property typed as a Literal[MyEnum.MEMBER], the schema() and schema_json() methods fail, until I add another property typed as MyEnum.
Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":
pydantic version: 1.8.1
pydantic compiled: True
install path: /tmp/tmp.iid8D0iv22/.venv/lib/python3.9/site-packages/pydantic
python version: 3.9.2 (default, Feb 20 2021, 18:40:11) [GCC 10.2.0]
platform: Linux-5.11.6-arch1-1-x86_64-with-glibc2.33
optional deps. installed: ['typing-extensions']
from typing import Literal
from enum import Enum
from pydantic import BaseModel
class MyEnum(str, Enum):
FOO = "foo"
BAR = "bar"
class ModelWithLiteralEnumMember(BaseModel):
kind: Literal[MyEnum.FOO]
ModelWithLiteralEnumMember.schema_json() # This works until 1.7.3 but crashes on 1.8+
class ModelWithLiteralEnumMemberAndFullEnum(BaseModel):
kind: Literal[MyEnum.FOO]
possible_kinds: MyEnum
ModelWithLiteralEnumMemberAndFullEnum.schema_json() # ... but now with the new property it does workTraceback looks like this:
Traceback (most recent call last):
File "/tmp/tmp.iid8D0iv22/main.py", line 33, in <module>
print(ModelWithLiteralEnumMember.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 810, in pydantic.schema.field_singleton_schema
KeyError: <enum 'MyEnum'>
I have made a more complete gist comparing the results in 1.7.3, 1.8 and 1.8.1, with multiple tries to workaround the issue: https://gist.github.com/nymous/686d38ba1aa150fefe6da6d78b1cb441
cc @senpos, what we discussed on the FastAPI Discord
Metadata
Metadata
Assignees
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X