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

Migrate to Pydantic V2 #1115

Closed
mawoka-myblock opened this issue Jun 22, 2023 · 14 comments
Closed

Migrate to Pydantic V2 #1115

mawoka-myblock opened this issue Jun 22, 2023 · 14 comments

Comments

@mawoka-myblock
Copy link

mawoka-myblock commented Jun 22, 2023

Pydantic V2 changes the API and is already in beta. A migration would be necessary, especially when FastAPI will require Pydantic V2 and ormar V1. Here's the link to the migration guide: https://docs.pydantic.dev/dev-v2/migration/

furkan-guvenc added a commit to furkan-guvenc/fastapi-crudrouter that referenced this issue Jun 27, 2023
@hail-pas
Copy link

Hi, Is there any progress ? Since FastApi has just released ther version 0.100.0 with pydantic v2

@LasseRegin
Copy link

Very interested in this. Have tried looking at creating a PR myself with the upgrade, however it is definitely not a trivial task.

@mawoka-myblock
Copy link
Author

I've looked through ormar's code and pydantic's upgrade guide and many things used by ormar were removed without any alternative.

@mawoka-myblock mawoka-myblock changed the title Prepare for Pydantic V2 Migrate to Pydantic V2 Jul 31, 2023
@estarfoo
Copy link

estarfoo commented Aug 4, 2023

To save a little time for whoever else wants to start working on this:

See also #1151. Since Pydantic V2 retains the V1 API in a separate namespace pydantic.v1, one might hope for a quick&dirty fix in that PR’s branch dependabot/pip/pydantic-2.1.1 like this:

git grep -l 'from pydantic' | xargs sed -i -e 's/^from pydantic/from pydantic.v1/'
git grep -l '^import pydantic$' | xargs sed -i -e 's/^import pydantic$/import pydantic.v1 as pydantic/'

Which leaves only 19 errors in make test, all related to FastAPI support, however…

This, in turn, looks related to fastapi/fastapi#9966: FastAPI, as of version 0.100.1, sets in fastapi._compat a flag PYDANTIC_V2, based on pydantic.version.VERSION[:2] == '2.'. It uses the flag in about 30 places throughout its code (tests excluded) to switch between V1 and V2 handling. Recalling from above: quick&dirty, could we toggle the flag and get this to run? Sadly, no.

This does not work, because FastAPI apparently sets up things at import time:

 from fastapi import FastAPI
 import pydantic.version as pydantic_version
 pydantic_version.VERSION = '1.999'

But neither does this, because FastAPI has import statements dependent on the Pydantic version, which fail if the flag is wrong:

 import pydantic.version as pydantic_version
 pydantic_version.VERSION = '1.999'
 from fastapi import FastAPI

So where to go from here?

  • For ormar, enhancing the above idea with pydantic.v1 imports by a switch on the Pydantic version (i. e. import pydantic if V1 is installed, import pydantic.v1 as pydantic if V2 is installed) could be a first step. This is not really V2 support, but perhaps it could run with either V1 or V2 installed, as long as the entire application sticks to the V1 API.
  • FastAPI might need to support V1 and V2 models in parallel with Pydantic V2 installed, per Working with Pydantic v1 while having v2 installed fastapi/fastapi#9966. (This could, by the way, be helpful for users who would like to upgrade, but cannot migrate all their non-ormar models to V2 right away, and need to transition via pydantic.v1 for a while.) Which seems to require a lot of changes there, but at least the code for either case is already present.
    • A dirtier approach might be to tweak the PYDANTIC_V2 flag in FastAPI, but fix its import statements to load modules from pydantic.v1 if that’s available and PYDANTIC_V2 is False. I don’t imagine that this would pass any review, but maybe it would run.
  • At this point, you should be able to run your ormar project with Pydantic V2, assuming you change your own code to import pydantic.v1 as needed.
  • Will the ormar developer team will add and maintain parallel support for both Pydantic versions in the code, or make a hard switch from some release on? It would be nice to have compatible combinations of up-to-date Pydantic/FastAPI/ormar versions…

@mawoka-myblock
Copy link
Author

mawoka-myblock commented Aug 4, 2023

The problem is, that's, as you've already said, a quick and dirty fix and nothing for the long run.

@collerek
Copy link
Owner

collerek commented Aug 5, 2023

It's true that a lot of methods and logic was changed and was removed but when I started to check a lot of them are replaceable by something else, not sure yet if all of them but after all functionality wise the new version of pydantic covers most if not all of the previous use cases. There will be some breaking changes for sure as now config is a dict and not a inner class etc. The change scope is big so it will take time but I will try to port it to V2, will see if possible.

@mawoka-myblock
Copy link
Author

It's true that a lot of methods and logic was changed and was removed but when I started to check a lot of them are replaceable by something else, not sure yet if all of them but after all functionality wise the new version of pydantic covers most if not all of the previous use cases. There will be some breaking changes for sure as now config is a dict and not a inner class etc. The change scope is big so it will take time but I will try to port it to V2, will see if possible.

Some great news! I really appreciate your response right here! Please keep us up to datea and many thanks again for this awesome library!

@sincorchetes
Copy link

News?

@kitty-eu-org
Copy link

I like ormar very much, but because my project uses pydantic2, I have to choose other orm frameworks. I hope that the development team of ormar can support pydantic2 as soon as possible. I hope that ormar will get better and better

@hyusetiawan
Copy link

@hezhaozhao-git which orm has support for pydantic v2?

@brunorpinho
Copy link

brunorpinho commented Aug 30, 2023

I haven't tried myself, but it seems that the most unique pydantic features can be bumped with this tool: https://github.com/pydantic/bump-pydantic

I'm not sure if they handle init and similar stuff not supported in pydantic v2, but I would try this.

@markuss23
Copy link

Are there any news about using pydanticV2 in ormar? I am forced to use sqlachelmy with pydanticV2, but I would like to use something different

@collerek
Copy link
Owner

You can try version 0.20.0

@mawoka-myblock
Copy link
Author

@collerek You're awesome, thank you!

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

No branches or pull requests

10 participants