Convert Pydantic from V1 to V2 ♻
Project description
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 the issue tracker.
Table of contents
- Bump Pydantic ♻️
- Table of contents
- Installation
- Usage
- Rules
- BP001: Add default
None
toOptional[T]
,Union[T, None]
andAny
fields - BP002: Replace
Config
class bymodel_config
attribute - BP003: Replace
Field
old parameters to new ones - BP004: Replace imports
- BP003: Replace
Config
class bymodel_config
- BP005: Replace
GenericModel
byBaseModel
- BP006: Replace
__root__
byRootModel
- BP007: Replace decorators
- BP008: Replace
pydantic.parse_obj_as
bypydantic.TypeAdapter
- BP001: Add default
- License
Installation
The installation is as simple as:
pip install "bump-pydantic @ git+https://github.com/pydantic/bump-pydantic@main"
Usage
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
Check diff before applying changes
To check the diff before applying the changes, you can run:
bump-pydantic --diff <package>
Apply changes
To apply the changes, you can run:
bump-pydantic <package>
Rules
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.
BP001: Add default None
to Optional[T]
, Union[T, None]
and Any
fields
- ✅ 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
BP002: Replace Config
class by model_config
attribute
- ✅ 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")
BP003: Replace Field
old parameters to new ones
- ✅ 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)
BP004: Replace imports
- ✅ Replace
BaseSettings
frompydantic
topydantic_settings
. - ✅ Replace
Color
andPaymentCardNumber
frompydantic
topydantic_extra_types
.
BP003: Replace Config
class by model_config
- ✅ Replace
Config
class bymodel_config = ConfigDict()
.
The following code will be transformed:
class User(BaseModel):
name: str
class Config:
extra = 'forbid'
Into:
class User(BaseModel):
name: str
model_config = ConfigDict(extra='forbid')
BP005: Replace GenericModel
by BaseModel
- ✅ 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
BP006: Replace __root__
by RootModel
- ✅ 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
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 pydantic.parse_obj_as
by pydantic.TypeAdapter
- ✅ Replace
pydantic.parse_obj_as(T, obj)
topydantic.TypeAdapter(T).validate_python(obj)
.
The following code will be transformed:
from typing import List
from pydantic import BaseModel, parse_obj_as
class User(BaseModel):
name: str
class Users(BaseModel):
users: List[User]
users = parse_obj_as(Users, {'users': [{'name': 'John'}]})
Into:
from typing import List
from pydantic import BaseModel, TypeAdapter
class User(BaseModel):
name: str
class Users(BaseModel):
users: List[User]
users = TypeAdapter(Users).validate_python({'users': [{'name': 'John'}]})
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bump_pydantic-0.1.0.tar.gz
.
File metadata
- Download URL: bump_pydantic-0.1.0.tar.gz
- Upload date:
- Size: 22.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5c5c6ed8c0d686ddbb00905e8e2a6bdd356f262e1d99e8007cfd9d836f90b1a |
|
MD5 | b2ddd8170e87c60f2ee8a87973caa20c |
|
BLAKE2b-256 | 9bedb9a747545374962e46e3ec036fa0a4cf9bb70ede98501c925672fcc4605a |
File details
Details for the file bump_pydantic-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: bump_pydantic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 23.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e89d1eb474e1d23e8365099b0dc728cea34e10a779a25871628bed0b5a22832 |
|
MD5 | 2ead5706e0da99c78993e704cdf8b215 |
|
BLAKE2b-256 | 2790767df035417c9cf152e4683c08314b8e24a15c80180362ab2a2f1c611e3d |