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

Pydantic v2 support #304

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

djbios
Copy link

@djbios djbios commented Jan 31, 2024

Fixes #297

Changes proposed in this pull request:

  • Pydantic V2 is supported
  • existing async tests supports python > 3.8 now

Attention: @prkumar

If this will be merged, #298 is not needed.

@emp-l
Copy link

emp-l commented Mar 12, 2024

any plans to merge this any time soon?

@wabiloo
Copy link

wabiloo commented Aug 29, 2024

@djbios have you considered releasing your own fork? It appears that @prkumar is not maintaining this project anymore...

@@ -9,7 +9,7 @@


def _encode_pydantic(obj):
from pydantic.json import pydantic_encoder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that here is better to auto-detect the version of pydantic that is installed and import the correct function so we keep backward compatibility.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, good point. Pydantic claims they will stop supporting V1 when V3 is released (somewhere this year), so I am not sure if V1 support gonna be needed.
What I missed is explicitly requiring a pydantic>2 for uplink - that's fixed now

@leiserfg
Copy link
Contributor

leiserfg commented Feb 4, 2025

@djbios can you add a test for a model inheriting from pydantic.v1.BaseModel to know if we need to do something extra in that case?

@leiserfg
Copy link
Contributor

leiserfg commented Feb 4, 2025

IMHO we better keep this implementation as is for v1 and/or for the v1 module of v2, and for v2 we simply use model.model_dump(mode='json')).
When we implemented this module, it was done that way because pydantic v1 didn't have how to serialize BaseModels as "jsonifiables" (for instance, they returned datetimes instead of strings). But now that they do, using their api directly is way more simple and flexible (as the users can easilly change the way the models are rendered).

@djbios
Copy link
Author

djbios commented Feb 4, 2025

@djbios can you add a test for a model inheriting from pydantic.v1.BaseModel to know if we need to do something extra in that case?

I'm not sure that can happen IRL (user has v2 installed but uses V1 models), but to support that it would require extra changes

IMHO we better keep this implementation as is for v1 and/or for the v1 module of v2, and for v2 we simply use model.model_dump(mode='json')).

I agree backward compatibility would be great, just not sure how to unit test it properly in the current setup.

@leiserfg
Copy link
Contributor

leiserfg commented Feb 4, 2025

I'm not sure that can happen IRL (user has v2 installed but uses V1 models), but to support that it would require extra changes

No, v2 has a copy of v1 that can be accessed as from pydantic.v1 import BaseModel which is very handy while migrating.

@leiserfg
Copy link
Contributor

leiserfg commented Feb 4, 2025

That's why I was doing https://github.com/prkumar/uplink/pull/295/files, maybe we don't have to support the old pydantic, but keeping support for the embedded v1makes sense as it helps to migrate with fewer changes.

@djbios
Copy link
Author

djbios commented Feb 4, 2025

No, v2 has a copy of v1 that can be accessed as from pydantic.v1 import BaseModel which is very handy while migrating.

That's true, but as I mentioned, v1's BaseModel won't work with v2's to_jsonable_python, at least as it is now

@leiserfg
Copy link
Contributor

leiserfg commented Feb 4, 2025

I will make a PR with my proposal, I think my idea is clearer in code than in a comment 😄

@wabiloo
Copy link

wabiloo commented Feb 4, 2025

I don't think this project is still maintained. Would you guys consider forking it and publishing it?

# For Python 3.5 and newer
async def coroutine():
return mock_response
elif (3, 3) <= sys.version_info < (3, 5):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be safe to drop this. I don't believe we officially support Python versions below 3.5

import asyncio
# Check the Python version and define coroutine accordingly
if sys.version_info >= (3, 5):
# For Python 3.5 and newer
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking: I would expect this to throw a SyntaxError for Python 2.7. But, we are way beyond EOL now, and should drop 2.7 support IMO.

@prkumar
Copy link
Owner

prkumar commented Feb 5, 2025

I will make a PR with my proposal, I think my idea is clearer in code than in a comment 😄

@leiserfg - added this same question to your PR: f I understand correctly, you are saying that Pydantic v2 includes a v1 package so that users could upgrade to v2 without needing to migrate their usage to the v2 BaseModel directly. Is that right? And, in that scenario, a package with pydantic 2 installed may still be using v1 BaseModel.

@gpflaum
Copy link
Contributor

gpflaum commented Feb 5, 2025

I don't think this project is still maintained. Would you guys consider forking it and publishing it?

@wabiloo Good news here: #311

Copy link
Owner

@prkumar prkumar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djbios - Thanks for putting this together! I also see @leiserfg sent #312.

There is possible path forward where we stack #312 on top of this, keep this one focused on adding v2 support, and focus #312 on extending it to support v1.

@djbios and @leiserfg - let me know if that path sounds good to both of you. Otherwise, let's pick one PR to focus on and commit.

Thank you both!

@leiserfg
Copy link
Contributor

leiserfg commented Feb 6, 2025

What do you think of using my pr for the pydantic V2 stuff and keeping from this one the async part only?

@djbios
Copy link
Author

djbios commented Feb 6, 2025

@leiserfg I guess if we decide to drop old Python versions support - the async part is not needed anymore. We should update the docs/setup.py if so though

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

Successfully merging this pull request may close these issues.

Pydantic v2 is not supported
6 participants