We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
The newly release improvements to the Enum class break enums with a custom __new__ implementation.
Enum
__new__
I don't think this is an official intended use case of the Enum class, so I don't have an issue with this if this doesn't get fixed.
A super easy work-around is to use a PlainValidator to just call the constructor in python, not rust.
PlainValidator
print( TypeAdapter(Annotated[Animal, PlainValidator(lambda s: Animal(s))]).validate_python( "meow" ) )
from enum import Enum from pydantic import TypeAdapter class Animal(Enum): CAT = "cat", "meow" DOG = "dog", "woof" def __new__(cls, species: str, sound: str): obj = object.__new__(cls) obj._value_ = species obj._all_values = (species, sound) obj.species = species obj.sound = sound cls._value2member_map_[sound] = obj return obj print(Animal("cat")) print(Animal("meow")) print(TypeAdapter(Animal).validate_python("meow"))
pydantic version: 2.7.0 pydantic-core version: 2.18.1 pydantic-core build: profile=release pgo=false install path: /Users/aikow/.miniconda3/envs/py310-pydantic/lib/python3.10/site-packages/pydantic python version: 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:53:34) [Clang 16.0.6 ] platform: macOS-14.4.1-x86_64-i386-64bit related packages: typing_extensions-4.11.0 commit: unknown
The text was updated successfully, but these errors were encountered:
Makes sense, happy to accept a pr to pydantic-core to detect custom __new__ methods and call them if that's possible.
Btw, we already support the __missing__ if that helps?
__missing__
Sorry, something went wrong.
No branches or pull requests
Initial Checks
Description
The newly release improvements to the
Enum
class break enums with a custom__new__
implementation.I don't think this is an official intended use case of the
Enum
class, so I don't have an issue with this if this doesn't get fixed.A super easy work-around is to use a
PlainValidator
to just call the constructor in python, not rust.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: