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 the issue tracker.
The installation is as simple as:
pip install bump-pydantic
bump-pydantic
is a CLI tool, hence you can use it from your terminal.
To see the available options, you can run:
bump-pydantic --help
To check the diff before applying the changes, you can run:
bump-pydantic --diff <path>
To apply the changes, you can run:
bump-pydantic <path>
You can find below the list of rules that are applied by bump-pydantic
.
It's also possible to disable rules by using the --disable
option.
- ✅ Add default
None
toOptional[T]
fields.
The following code will be transformed:
class User(BaseModel):
name: Optional[str]
Into:
class User(BaseModel):
name: Optional[str] = None
- ✅ Replace
Config
class bymodel_config = ConfigDict()
. - ✅ Rename old
Config
attributes to newmodel_config
attributes. - ✅ Add a TODO comment in case the transformation can't be done automatically.
- ✅ Replace
Extra
enum by string values.
The following code will be transformed:
from pydantic import BaseModel, Extra
class User(BaseModel):
name: str
class Config:
extra = Extra.forbid
Into:
from pydantic import ConfigDict, BaseModel
class User(BaseModel):
name: str
model_config = ConfigDict(extra="forbid")
- ✅ Replace
Field
old parameters to new ones. - ✅ Replace
field: Enum = Field(Enum.VALUE, const=True)
byfield: Literal[Enum.VALUE] = Enum.VALUE
.
The following code will be transformed:
from typing import List
from pydantic import BaseModel, Field
class User(BaseModel):
name: List[str] = Field(..., min_items=1)
Into:
from typing import List
from pydantic import BaseModel, Field
class User(BaseModel):
name: List[str] = Field(..., min_length=1)
- ✅ Replace
BaseSettings
frompydantic
topydantic_settings
. - ✅ Replace
Color
andPaymentCardNumber
frompydantic
topydantic_extra_types
.
- ✅ Replace
GenericModel
byBaseModel
.
The following code will be transformed:
from typing import Generic, TypeVar
from pydantic.generics import GenericModel
T = TypeVar('T')
class User(GenericModel, Generic[T]):
name: str
Into:
from typing import Generic, TypeVar
T = TypeVar('T')
class User(BaseModel, Generic[T]):
name: str
- ✅ Replace
__root__
byRootModel
.
The following code will be transformed:
from typing import List
from pydantic import BaseModel
class User(BaseModel):
age: int
name: str
class Users(BaseModel):
__root__ = List[User]
Into:
from typing import List
from pydantic import RootModel
class User(BaseModel):
age: int
name: str
class Users(RootModel[List[User]]):
pass
- ✅ Replace
@validator
by@field_validator
. - ✅ Replace
@root_validator
by@model_validator
.
The following code will be transformed:
from pydantic import BaseModel, validator, root_validator
class User(BaseModel):
name: str
@validator('name', pre=True)
def validate_name(cls, v):
return v
@root_validator(pre=True)
def validate_root(cls, values):
return values
Into:
from pydantic import BaseModel, field_validator, model_validator
class User(BaseModel):
name: str
@field_validator('name', mode='before')
def validate_name(cls, v):
return v
@model_validator(mode='before')
def validate_root(cls, values):
return values
This project is licensed under the terms of the MIT license.