-
-
Notifications
You must be signed in to change notification settings - Fork 92
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
WIP - Pydantic v2 support #1238
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… kept in core schema and not on fieldsinfo
…pydantic config checks, failing 32/442
…gh, fix default config and inheriting from it, failing 26/442
…ntic, failing 17/442
…but always on reverse side, failing 15/442
…but always on reverse side, failing 15/442
…ema dumping, some work on openapi docs, failing 13/442
…s own, failing 9/442
…pydantic, will be fixed in pydantic-core, remaining is circural schema for related models, failing 6/442
@collerek Is this your up-to-date wip branch? are you open to getting some help with this? |
@TouwaStar Now it is 😅 I would not only be open but super grateful. :) Apart from fixing typing/lining/renaming methods/etc. which I left for the end there are 2 main issues:
|
* wip, fixing tests * iteration, fixing some more tests * iteration, fixing some more tests * adhere to comments * adhere to comments * remove unnecessary dict call, re-add getattribute for testing * todo for reverse relationship * adhere to comments, remove prints
Checklist:
|
CodSpeed Performance ReportMerging #1238 will degrade performances by 23.36%Comparing Summary
Benchmarks breakdown
|
…ix unnecessary loop in one of the test
…lated methods too
…hem in initialization
vvanglro
reviewed
Mar 20, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Switching to Pydantic v2.X.X
Migration to 0.20.0 based on pydantic 2.X.X
Version 0.20.0 provides support for pydantic v2.X.X that provides significant speed boost (validation and serialization is written in rust) and cleaner api for developers,
at the same time it drops support for pydantic v.1.X.X. There are changes in
ormar
interface corresponding to changes made inpydantic
.Breaking changes
Migration to version >= 0.20.0 requires several changes in order to work properly.
ormar
Model configurationInstead of defining a
Meta
class now each of the ormar models require an ormar_config parameter that is an instance of theOrmarConfig
class.Note that the attribute must be named
ormar_config
and be an instance of the config class.OrmarConfig
api/ parametersThe
ormar_config
expose the same set of settings asMeta
class used to provide.That means that you can use any of the following parameters initializing the config:
BaseMeta
equivalent - best practiceNote that to reduce the duplication of code and ease of development it's still recommended to create a base config and provide each of the models with a copy.
OrmarConfig provides a convenient
copy
method for that purpose.The
copy
method accepts the same parameters asOrmarConfig
init, so you can overwrite if needed, but by default it will return already existing attributes, except for:tablename
,order_by
andconstraints
which by default are cleared.choices
Field parameter is no longer supported.Before version 0.20 you could provide
choices
parameter to any existing ormar Field to limit the accepted values.This functionality was dropped, and you should use
ormar.Enum
field that was designed for this purpose.If you want to keep the database field type (i.e. an Integer field) you can always write a custom validator.
pydantic_only
Field parameter is no longer supportedpydantic_only
fields were already deprecated and are removed in v 0.20. Ormar allows defining pydantic fields as in ordinary pydantic model.property_field
decorator is no longer supportedproperty_field
decorator was used to provide a way to pass calculated fields that were included in dictionary/ serialized json representation of the model.Version 2.X of pydantic introduced such a possibility, so you should now switch to the one native to the pydantic.
Deprecated methods
All methods listed below are deprecated and will be removed in version 0.30 of
ormar
.dict()
becomes themodel_dump()
Note that parameters remain the same i.e.
include
,exclude
etc.json()
becomes themodel_dump_json()
Note that parameters remain the same i.e.
include
,exclude
etc.construct()
becomes themodel_construct()
To read more about construct please refer to
pydantic
documentation.