samples-python icon indicating copy to clipboard operation
samples-python copied to clipboard

Sample request: Pydantic v2 converter

Open cretz opened this issue 2 years ago • 4 comments

Describe the solution you'd like

Rename sample pydantic_converter to pydanticv1_converter (and tests/packages/dep-group/etc) and make a new pydantic_converter sample that works with v2

cretz avatar Nov 30 '23 15:11 cretz

I am setting up a new project and I would really appreciate this for the "type safety without having to redefine attributes twice" benefits!

In particular, the pydantic_encoder does not work anymore in v2: https://github.com/pydantic/pydantic/discussions/6652

JaneJeon avatar Jan 24 '24 20:01 JaneJeon

Until the sample is present, you can apply the concepts/approaches of the custom conversion to whatever is needed for v2 without applying the specific code

cretz avatar Jan 24 '24 20:01 cretz

@JaneJeon I am using pydantic 2 with temporal just by changing pydantic_encoder to to_jsonable_python:

from pydantic_core import to_jsonable_python

...
        # We let JSON conversion errors be thrown to caller
        return Payload(
            metadata={"encoding": self.encoding.encode()},
            data=json.dumps(value, separators=(",", ":"), sort_keys=True, default=to_jsonable_python).encode(),
        )

slingshotsys avatar Jan 26 '24 21:01 slingshotsys