tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.68k stars 390 forks source link

feat: support pydantic2.0 #1427

Closed long2ice closed 1 year ago

long2ice commented 1 year ago

Description

Support pydantic2.0

Motivation and Context

https://github.com/tortoise/tortoise-orm/discussions/1419

How Has This Been Tested?

Checklist:

PeiPeiWow commented 1 year ago

Hi, I am using pydantic2.0, but still have this error, please help! from app.models import AnyUser, Token, User_Pydantic, Token_Pydantic, UserIn_Pydantic File "/Users/qilong/Desktop/fastApiUser/app/models.py", line 34, in User_Pydantic = pydantic_model_creator(AnyUser, name="AnyUser") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/qilong/Desktop/fastApiUser/venv/lib/python3.11/site-packages/tortoise/contrib/pydantic/creator.py", line 437, in pydantic_model_creator model = cast(Type[PydanticModel], type(_name, (PydanticModel,), properties)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/qilong/Desktop/fastApiUser/venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 95, in new private_attributes = inspect_namespace( ^^^^^^^^^^^^^^^^^^ File "/Users/qilong/Desktop/fastApiUser/venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 287, in inspect_namespace and value.module == namespace['module']


KeyError: '__module__'
long2ice commented 1 year ago

Did you use latest code?

PeiPeiWow commented 1 year ago

Did you use latest code?

Yes, I used the latest code. Maybe you have any other ideas?

PeiPeiWow commented 1 year ago

When I downgraded pydantic==2.0.0, I got this

255324618-57bae3c9-2f77-44b1-bf6f-20b64ff65079

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. fastapi 0.100.0 requires pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,<3.0.0,>=1.7.4, but you have pydantic 2.0 which is incompatible.

so I have to install pydantic==1.10.11, and this solves the problem.

Do you have any other ideas?

farzbood commented 1 year ago

FastAPI (0.100.0) dependency list shows that it excluded Pydantic (2.0.0, 2.0.1) so first of all using the latest stable version of both frameworks would be a rational choice, which in case of Pydantic it is (2.0.3) While the maintaners of TortoiseORM are working on finalizing the version bump to support Pydantic V2 ,for clarification of conflicts, you can refer to the list of breaking changes with corresponding migration guides, based on Pydantic official docs.

https://docs.pydantic.dev/latest/migration/#migration-guide Good Luck

PeiPeiWow commented 1 year ago

FastAPI (0.100.0) dependency list shows that it excluded Pydantic (2.0.0, 2.0.1) so first of all using the latest stable version of both frameworks would be a rational choice, which in case of Pydantic it is (2.0.3) While the maintaners of TortoiseORM are working on finalizing the version bump to support Pydantic V2 ,for clarification of conflicts, you can refer to the list of breaking changes with corresponding migration guides, based on Pydantic official docs.

https://docs.pydantic.dev/latest/migration/#migration-guide Good Luck

Thank you so much