Skip to content

Enum construction from float worked in v2.6.4 but fails in v2.7.1 #9375

@Idealcoder

Description

@Idealcoder

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Constructing an enum from a float worked in v2.6.4 but fails in v2.7.1.

Is this an intended change or a regression?

I see some changes around enum validation have been made #9064, pydantic/pydantic-core#1235 @samuelcolvin

Example Code

from enum import Enum

from pydantic import TypeAdapter

class MyEnum(Enum):
    A = 1
    B = 2

ta = TypeAdapter(MyEnum)

assert ta.validate_python(1.0) is MyEnum.A
assert ta.validate_python(2.0) is MyEnum.B

# pydantic=2.6.4 passes validation

# pydantic=2.7.1 fails validation
#
# pydantic_core._pydantic_core.ValidationError: 1 validation error for enum[MyEnum]
#   Input should be 1, 2 or 3 [type=enum, input_value=1.0, input_type=float]
#     For further information visit https://errors.pydantic.dev/2.7/v/enum

Python, Pydantic & OS Version

pydantic version: 2.6.4
        pydantic-core version: 2.16.3
          pydantic-core build: profile=release pgo=false
                 install path: {redacted}\site-packages\pydantic
               python version: 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:39:05) [MSC v.1929 64 bit (AMD64)]
                     platform: Windows-10-10.0.19045-SP0
             related packages: fastapi-0.106.0 mypy-1.0.1 pydantic-settings-2.2.1 typing_extensions-4.10.0
                       commit: unknown


             pydantic version: 2.7.1
        pydantic-core version: 2.18.2
          pydantic-core build: profile=release pgo=false
                 install path: {redacted}\site-packages\pydantic
               python version: 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:39:05) [MSC v.1929 64 bit (AMD64)]
                     platform: Windows-10-10.0.19045-SP0
             related packages: fastapi-0.106.0 mypy-1.0.1 pydantic-settings-2.2.1 typing_extensions-4.11.0
                       commit: unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V2Bug related to Pydantic V2

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions