Initial Checks
Description
When an field has an Enum value as its default value, then it is translated to a str when both use_enum_values and validate_default are set True, as is expected from the documentation.
However, when the type of the Field is a Literal of that enum, the validate_default doesn't seem to work. See code below.
The same happens when the type is Literal[MyEnum.FOO, MyEnum.BAR]
Example Code
from enum import StrEnum, auto
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
class MyEnum(StrEnum):
FOO = auto()
BAR = auto()
class MyModel(BaseModel):
model_config = ConfigDict(use_enum_values=True)
enum_field: MyEnum = Field(default=MyEnum.FOO, validate_default=True)
class MyModelLiteral(BaseModel):
model_config = ConfigDict(use_enum_values=True)
enum_field: Literal[MyEnum.FOO] = Field(default=MyEnum.FOO, validate_default=True)
# default="foo" gives same result
print(type(MyModel().enum_field)) # <class 'str'>
print(type(MyModelLiteral().enum_field)) # <enum 'MyEnum'>
Python, Pydantic & OS Version
pydantic version: 2.7.4
pydantic-core version: 2.18.4
pydantic-core build: profile=release pgo=true
install path: /home/kwint/.cache/pypoetry/virtualenvs/material-library-8A638HCG-py3.11/lib/python3.11/site-packages/pydantic
python version: 3.11.8 (main, Feb 25 2024, 16:39:33) [GCC 11.4.0]
platform: Linux-5.15.153.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
related packages: typing_extensions-4.12.2 mypy-1.10.0
commit: unknown
Initial Checks
Description
When an field has an Enum value as its default value, then it is translated to a str when both
use_enum_valuesandvalidate_defaultare set True, as is expected from the documentation.However, when the type of the Field is a Literal of that enum, the validate_default doesn't seem to work. See code below.
The same happens when the type is
Literal[MyEnum.FOO, MyEnum.BAR]Example Code
Python, Pydantic & OS Version