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)
BP004: Replace imports
✅ Replace BaseSettings from pydantic to pydantic_settings.
✅ Replace Color and PaymentCardNumber from pydantic to pydantic_extra_types.
BP005: Replace GenericModel by BaseModel
✅ Replace GenericModel by BaseModel.
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
from pydantic import BaseModel
T = TypeVar('T')
class User(BaseModel, Generic[T]):
name: str
BP006: Replace __root__ by RootModel
✅ Replace __root__ by RootModel.
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, BaseModel
class User(BaseModel):
age: int
name: str
class Users(RootModel[List[User]]):
pass
BP007: Replace decorators
✅ 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
BP008: Replace con* functions by Annotated versions
✅ Replace constr(*args) by Annotated[str, StringConstraints(*args)].
✅ Replace conint(*args) by Annotated[int, Field(*args)].
✅ Replace confloat(*args) by Annotated[float, Field(*args)].
✅ Replace conbytes(*args) by Annotated[bytes, Field(*args)].
✅ Replace condecimal(*args) by Annotated[Decimal, Field(*args)].
✅ Replace conset(T, *args) by Annotated[Set[T], Field(*args)].
✅ Replace confrozenset(T, *args) by Annotated[Set[T], Field(*args)].
✅ Replace conlist(T, *args) by Annotated[List[T], Field(*args)].
The following code will be transformed:
from pydantic import BaseModel, constr
class User(BaseModel):
name: constr(min_length=1)
Into:
from pydantic import BaseModel, StringConstraints
from typing_extensions import Annotated
class User(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:
class SomeThing:
@classmethod
def __get_validators__(cls):
yield from []
@classmethod
def __modify_schema__(cls, field_schema, field):
if field:
field_schema['example'] = "Weird example"
Into:
class SomeThing:
@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):
if field:
field_schema['example'] = "Weird example"
License
This project is licensed under the terms of the MIT license.
Works well with
Use together with bump-pydantic: extensions, drivers, servers and companion tools.
pydanticPythie’s Index: 85Data validation using Python type hints1.2B downloads/mo
pydantic-settingsPythie’s Index: 86Settings management using Pydantic514M downloads/mo