Skip to content

Literal enum values does not respect the use_enum_values config #1747

@eistaa

Description

@eistaa

Bug

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

             pydantic version: 1.6.1
            pydantic compiled: True
                 install path: <path-to-virtualenv>/lib/python3.8/site-packages/pydantic
               python version: 3.8.1 (default, Dec 29 2019, 16:51:17)  [GCC 9.2.0]
                     platform: Linux-5.7.2-arch1-1-x86_64-with-glibc2.29
     optional deps. installed: ['typing-extensions']

When combining enum values with a Literal typing the use_enum_values config is not respected:

from enum import Enum
from typing import Literal

from pydantic import BaseModel

class FruitEnum(Enum):
    pear = 'pear'
    banana = 'banana'

class CookingModel(BaseModel):
    fruit: FruitEnum

    class Config:
        use_enum_values = True

class LiteralCookingModel(BaseModel):
    fruit: Literal[FruitEnum.pear]

    class Config:
        use_enum_values = True

cm = CookingModel(fruit=FruitEnum.pear)
print(cm.dict())
# outputs: {'fruit': 'pear'}

lcm = LiteralCookingModel(fruit=FruitEnum.pear)
print(lcm.dict())
# outputs: {'fruit': <FruitEnum.pear: 'pear'>}

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