Package: bump-pydantic
Bump Pydantic ♻️Bump Pydantic is a tool to help you migrate your code from Pydantic V1 to V2.[!NOTE]If you find bugs, please report them on theissue tracker.Table of contentsBump Pydantic ♻️Table of contentsInstallationUsageCheck diff before applying changesApply changesRulesBP001: Add defaultNonetoOptional[T],Union[T, None]andAnyfieldsBP002: ReplaceConfigclass bymodel_configattributeBP003: ReplaceFieldold parameters to new onesBP004: Replace importsBP005: ReplaceGenericModelbyBaseModelBP006: Replace__root__byRootModelBP007: Replace decoratorsBP008: Replacecon*functions byAnnotatedversionsBP009: Mark pydantic "protocol" functions in custom types with proper TODOsLicenseInstallationThe installation is as simple as:pipinstallbump-pydanticUsagebump-pydanticis a CLI tool, hence you can use it from your terminal.It's easy to use. If your project structure is:repository/
└──my_package/└──<pythonsourcefiles>Then you'll want to do:cd/path/to/repository
bump-pydanticmy_packageCheck diff before applying changesTo check the diff before applying the changes, you can run:bump-pydantic--diff<path>Apply changesTo apply the changes, you can run:bump-pydantic<path>RulesYou can find below the list of rules that are applied bybump-pydantic.It's also possible to disable rules by using the--disableoption.BP001: Add defaultNonetoOptional[T],Union[T, None]andAnyfields✅ Add defaultNonetoOptional[T]fields.The following code will be transformed:classUser(BaseModel):name:Optional[str]Into:classUser(BaseModel):name:Optional[str]=NoneBP002: ReplaceConfigclass bymodel_configattribute✅ ReplaceConfigclass bymodel_config = ConfigDict().✅ Rename oldConfigattributes to newmodel_configattributes.✅ Add a TODO comment in case the transformation can't be done automatically.✅ ReplaceExtraenum by string values.The following code will be transformed:frompydanticimportBaseModel,ExtraclassUser(BaseModel):name:strclassConfig:extra=Extra.forbidInto:frompydanticimportConfigDict,BaseModelclassUser(BaseModel):name:strmodel_config=ConfigDict(extra="forbid")BP003: ReplaceFieldold parameters to new ones✅ ReplaceFieldold parameters to new ones.✅ Replacefield: Enum = Field(Enum.VALUE, const=True)byfield: Literal[Enum.VALUE] = Enum.VALUE.The following code will be transformed:fromtypingimportListfrompydanticimportBaseModel,FieldclassUser(BaseModel):name:List[str]=Field(...,min_items=1)Into:fromtypingimportListfrompydanticimportBaseModel,FieldclassUser(BaseModel):name:List[str]=Field(...,min_length=1)BP004: Replace imports✅ ReplaceBaseSettingsfrompydantictopydantic_settings.✅ ReplaceColorandPaymentCardNumberfrompydantictopydantic_extra_types.BP005: ReplaceGenericModelbyBaseModel✅ ReplaceGenericModelbyBaseModel.The following code will be transformed:fromtypingimportGeneric,TypeVarfrompydantic.genericsimportGenericModelT=TypeVar('T')classUser(GenericModel,Generic[T]):name:strInto:fromtypingimportGeneric,TypeVarfrompydanticimportBaseModelT=TypeVar('T')classUser(BaseModel,Generic[T]):name:strBP006: Replace__root__byRootModel✅ Replace__root__byRootModel.The following code will be transformed:fromtypingimportListfrompydanticimportBaseModelclassUser(BaseModel):age:intname:strclassUsers(BaseModel):__root__=List[User]Into:fromtypingimportListfrompydanticimportRootModel,BaseModelclassUser(BaseModel):age:intname:strclassUsers(RootModel[List[User]]):passBP007: Replace decorators✅ Replace@validatorby@field_validator.✅ Replace@root_validatorby@model_validator.The following code will be transformed:frompydanticimportBaseModel,validator,root_validatorclassUser(BaseModel):name:str@validator('name',pre=True)defvalidate_name(cls,v):returnv@root_validator(pre=True)defvalidate_root(cls,values):returnvaluesInto:frompydanticimportBaseModel,field_validator,model_validatorclassUser(BaseModel):name:str@field_validator('name',mode='before')defvalidate_name(cls,v):returnv@model_validator(mode='before')defvalidate_root(cls,values):returnvaluesBP008: Replacecon*functions byAnnotatedversions✅ Replaceconstr(*args)byAnnotated[str, StringConstraints(*args)].✅ Replaceconint(*args)byAnnotated[int, Field(*args)].✅ Replaceconfloat(*args)byAnnotated[float, Field(*args)].✅ Replaceconbytes(*args)byAnnotated[bytes, Field(*args)].✅ Replacecondecimal(*args)byAnnotated[Decimal, Field(*args)].✅ Replaceconset(T, *args)byAnnotated[Set[T], Field(*args)].✅ Replaceconfrozenset(T, *args)byAnnotated[Set[T], Field(*args)].✅ Replaceconlist(T, *args)byAnnotated[List[T], Field(*args)].The following code will be transformed:frompydanticimportBaseModel,constrclassUser(BaseModel):name:constr(min_length=1)Into:frompydanticimportBaseModel,StringConstraintsfromtyping_extensionsimportAnnotatedclassUser(BaseModel):name:Annotated[str,StringConstraints(min_length=1)]BP009: Mark Pydantic "protocol" functions in custom types with proper TODOs✅ Mark__get_validators__as to be replaced by__get_pydantic_core_schema__.✅ Mark__modify_schema__as to be replaced by__get_pydantic_json_schema__.The following code will be transformed:classSomeThing:@classmethoddef__get_validators__(cls):yield from[]@classmethoddef__modify_schema__(cls,field_schema,field):iffield:field_schema['example']="Weird example"Into:classSomeThing:@classmethod# TODO[pydantic]: We couldn't refactor `__get_validators__`, please create the `__get_pydantic_core_schema__` manually.# Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.def__get_validators__(cls):yield from[]@classmethod# TODO[pydantic]: We couldn't refactor `__modify_schema__`, please create the `__get_pydantic_json_schema__` manually.# Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.def__modify_schema__(cls,field_schema,field):iffield:field_schema['example']="Weird example"LicenseThis project is licensed under the terms of the MIT license.