Skip to content
New issue

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

Serializing computed fields returning enums #9394

Open
1 task done
graydenshand opened this issue May 5, 2024 · 5 comments
Open
1 task done

Serializing computed fields returning enums #9394

graydenshand opened this issue May 5, 2024 · 5 comments
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@graydenshand
Copy link

graydenshand commented May 5, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I am unable to serialize a computed field that returns an enum,

Example Code

from enum import Enum
from pydantic import BaseModel, computed_field, field_serializer

class MyEnum(Enum):
    A = "a"
    B = "b"

class MyModel(BaseModel):
    
    @computed_field
    @property
    def computed_a_or_b(self) -> MyEnum:
        return MyEnum.B

    @field_serializer("computed_a_or_b")
    def serialize_my_enum(self, a_or_b: MyEnum) -> str:
        return a_or_b.value


if __name__ == "__main__":
    m = MyModel()
    assert m.model_dump()['computed_a_or_b'] == "b"

The above snippet raises the following error:

KeyError: '__main__.MyEnum:5602143824'

Python, Pydantic & OS Version

pydantic version: 2.7.0
        pydantic-core version: 2.18.1
          pydantic-core build: profile=release pgo=true
                 install path: myproject/.venv/lib/python3.12/site-packages/pydantic
               python version: 3.12.0 (main, Nov 11 2023, 13:36:14) [Clang 15.0.0 (clang-1500.0.40.1)]
                     platform: macOS-14.4.1-arm64-arm-64bit
             related packages: mypy-1.9.0 typing_extensions-4.11.0
                       commit: unknown
@graydenshand graydenshand added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 5, 2024
@graydenshand graydenshand reopened this May 5, 2024
@graydenshand
Copy link
Author

One other observation to note, if I add a static field to the model with the same enum type, the field_serializer on the computed field works.
e.g. if I add a static field to the above example,

class MyModel(BaseModel):
    static_a_or_b: MyEnum | None = None
    ...

The above snippet succeeds.

@graydenshand
Copy link
Author

Another workaround is to annotate the enum with a WrapSerializer.

from enum import Enum
from pydantic import BaseModel, computed_field, WrapSerializer
from typing import Annotated, Any

class MyEnum(Enum):
    A = "a"
    B = "b"

def serialize_my_enum(value: Any, handler, info) ->  str:
    partial_result = handler(value, info)
    return partial_result.value

MySerializableEnum = Annotated[MyEnum, WrapSerializer(serialize_my_enum)]

class MyModel(BaseModel):    
    @computed_field
    @property
    def computed_a_or_b(self) -> MySerializableEnum:
        return MyEnum.B

if __name__ == "__main__":
    m = MyModel()
    assert m.model_dump()['computed_a_or_b'] == "b"

@sydney-runkle
Copy link
Member

@graydenshand,

Thanks for reporting. Adding to our 2.7 issues milestone. Looks similar (at least in terms of the error) to #9319.

@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label May 14, 2024
@sydney-runkle sydney-runkle added this to the 2.7 fixes milestone May 14, 2024
@sydney-runkle
Copy link
Member

Hmm, looks like this issue has been around for a while (not introduced in 2.7). Moving to the 2.8 milestone - still a priority, but not a rush to get out for 2.7.

@sydney-runkle sydney-runkle modified the milestones: 2.7 fixes, v2.8.0 May 20, 2024
@NeevCohen
Copy link
Contributor

Hey @sydney-runkle I'd like to work on this if it's still available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

No branches or pull requests

3 participants