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
NonetoOptional[T],Union[T, None]andAnyfields - BP002: Replace
Configclass bymodel_configattribute - BP003: Replace
Fieldold parameters to new ones - BP004: Replace imports
- BP003: Replace
Configclass bymodel_config - BP005: Replace
GenericModelbyBaseModel - BP006: Replace
__root__byRootModel
- 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
NonetoOptional[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
Configclass bymodel_config = ConfigDict(). - ✅ Rename old
Configattributes to newmodel_configattributes. - ✅ Add a TODO comment in case the transformation can't be done automatically.
- ✅ Replace
Extraenum 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
Fieldold parameters to new ones.
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
BaseSettingsfrompydantictopydantic_settings. - ✅ Replace
ColorandPaymentCardNumberfrompydantictopydantic_extra_types.
BP003: Replace Config class by model_config
- ✅ Replace
Configclass 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
GenericModelbyBaseModel.
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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bump_pydantic-0.0.1.tar.gz.
File metadata
- Download URL: bump_pydantic-0.0.1.tar.gz
- Upload date:
- Size: 26.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
454e3b2f0f5569536a71c80ca9a42f9a9030b1c192acd860b1086c6a43b2d830
|
|
| MD5 |
e7766e7ac4855ee8544d6a65c8667610
|
|
| BLAKE2b-256 |
29979cefc5fce9310628fd694b4d4fd4a616babb9ab87b07d2975bad51c4aa82
|
File details
Details for the file bump_pydantic-0.0.1-py3-none-any.whl.
File metadata
- Download URL: bump_pydantic-0.0.1-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31c81b41f449f8b686a72c70369ceb015ec5c58b3b8465c3c8636f01cd3dd7c3
|
|
| MD5 |
b8451b3ce48902053f100b7f2aa1ca3e
|
|
| BLAKE2b-256 |
f527a662c6bfc4775810a0a66dd3d303437a15433f574680fbd48c805369f403
|