Giter VIP home page Giter VIP logo

Comments (4)

sydney-runkle avatar sydney-runkle commented on May 28, 2024 1

@graydenshand,

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

from pydantic.

graydenshand avatar graydenshand commented on May 28, 2024

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.

from pydantic.

graydenshand avatar graydenshand commented on May 28, 2024

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"

from pydantic.

sydney-runkle avatar sydney-runkle commented on May 28, 2024

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.

from pydantic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.