-
Notifications
You must be signed in to change notification settings - Fork 66
FXC-3378 Pydantic v2 #2433
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
base: develop
Are you sure you want to change the base?
FXC-3378 Pydantic v2 #2433
Conversation
0a529f4 to
a7e7a49
Compare
348a931 to
5ecc9f9
Compare
|
It seems like fundamentally there's just a few things that change? Maybe I'm missing something important/tricky. Could you explain if there's anything like that, and what changes going forward? Also very important - we will certainly need to update the backend python code too and might need to update denormalizer, metadata api, and who knows what else. |
|
Yes the biggest changes are related to custom types and serialization, everything else is relatively straightforward. I'll write something up. |
93ce359 to
dd10b26
Compare
dd0cd2b to
38d502a
Compare
ea3b4cb to
29234a6
Compare
6d3f88a to
2967f74
Compare
0a72b9c to
715c758
Compare
8ffaf83 to
24a7bdb
Compare
252205c to
0d0e01e
Compare
0d0e01e to
0d3faa8
Compare
This PR completes the migration of our codebase from Pydantic V1 to Pydantic V2.
1. Imports & Dependency Updates:
pydanticnamespace (andpydantic_settingsforBaseSettings), removingpydantic.v1compatibility imports.from pydantic import Field, model_validator).typingimports (e.g.,Optional,Union,Self,TypeAlias) and other general Python type hinting.skip_if_fields_missingdecorator and replacingannotate_typewithdiscriminated_union.pydantic_coreandpydantic.json_schema.2. Field Definitions & Type-Hinting:
pydantic.v1.Fieldusage to the newpydantic.Fieldsyntax.Optional[T]and appropriateFielddefaults (e.g.,default=None,default_factory).PositiveFloat,NonNegativeInt) and replacedconfloatwithAnnotatedandField.default_factoryfor mutable defaults (e.g.,dict,list, custom classes).3. Validation Logic:
@pydantic.v1.validatorwith@pydantic.field_validator, adjusting signatures and return values.@pydantic.v1.root_validatorto@pydantic.model_validator, specifyingmode='before'ormode='after'and updating logic for field access (e.g.,self.fieldinmode='after').infoobject, and used direct attribute access/setting (e.g.object.__setattr__). Added@classmethodwhere needed.skip_if_fields_missingdecorator, integrating its logic intomodel_validatoror relying on V2's improved validation flow._post_init_validatorslogic into@model_validator(mode='after').4. Configuration & Settings:
Configclass withmodel_config = ConfigDict(...).pydantic.v1.BaseSettingstopydantic_settings.BaseSettings.allow_population_by_field_name.5. Serialization & Parsing:
.dict()with.model_dump()and.json()(or._json()) with.model_dump_json().cls.parse_obj()tocls.model_validate().ndarray_encoder) in favor of V2's built-in capabilities orAnnotatedserializers.pydantic.ValidationErrorandPydanticSerializationError.6. Schema & Custom Types:
__get_validators__with__get_pydantic_core_schema__and__modify_schema__with__get_pydantic_json_schema__for custom type definitions.Annotatedtype along withBeforeValidator/AfterValidatorandPlainSerializer/WrapSerializer.constrained_arraywith a newarray_aliasfactory function usingAnnotated.TracedDict,get_static,hasbox).pydantic.PrivateAttrfor non-schema model attributes.7. Model Methods & Internal Refactors:
copy()andupdated_copy()methods to usemodel_copy(update=...).__init_subclass__for V2 and updated docstring generation to usemodel_fields.find_paths,find_submodels).__eq__methods for V2 compatibility and updated/added__str__,__repr__, and__rich_repr__for improved representations.__fields__access withmodel_fieldsfor introspection.