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

Support for pydantic v2 #215

Closed
gaborbernat opened this issue Jul 25, 2023 · 3 comments · Fixed by #218 or #219
Closed

Support for pydantic v2 #215

gaborbernat opened this issue Jul 25, 2023 · 3 comments · Fixed by #218 or #219
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@gaborbernat
Copy link

gaborbernat commented Jul 25, 2023

https://github.com/cloudevents/sdk-python/blob/main/cloudevents/pydantic/event.py#L87

https://docs.pydantic.dev/latest/migration/

@xSAVIKx xSAVIKx added enhancement New feature or request help wanted Extra attention is needed labels Jul 26, 2023
@xSAVIKx
Copy link
Member

xSAVIKx commented Jul 26, 2023

Hey @gaborbernat, thx for pointing this out. Is there any info on how one may support both v1 and v2 of Pydantic? While switching to v2 completely will break things for the library users.

@gaborbernat
Copy link
Author

I don't think there's support for supporting both... You could do some gimnastics to switch implementation based on detected version 🤷‍♂️ but will get messy.

@febus982
Copy link
Contributor

febus982 commented Aug 20, 2023

The main problem could be testing but the pydantic V2 implementation includes the old V1, namespaced in the pydantic.v1 module.

It looks horrible but importing the BaseModel like this might work:

pydantic_version = pydantic.version.VERSION.split('.')[0]

from pydantic import BaseModel as BaseModelV2
if pydantic_version == '1'
    from pydantic BaseModel as BaseModelV1
else
    from pydantic.v1 BaseModel as BaseModelV1

class _CloudEventBaseV1(BaseModelV1):
   # Existing implementation

class _CloudEventBaseV2(BaseModelV2):
   # Pydantic V2 configured model
   
if pydantic_version == '1':
    CloudEvent = _CloudEventBaseV1
else:
    CloudEvent = _CloudEventBaseV2

It looks horrible, I know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
3 participants