Skip to main content

Convert Pydantic from V1 to V2 ♻

Project description

Bump Pydantic ♻️

PyPI - Version PyPI - Python Version

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


Installation

The installation is as simple as:

pip install bump-pydantic

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 <path>

Apply changes

To apply the changes, you can run:

bump-pydantic <path>

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 to Optional[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 by model_config = ConfigDict().
  • ✅ Rename old Config attributes to new model_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) by field: 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 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

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

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)]

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bump_pydantic-0.4.0.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

bump_pydantic-0.4.0-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file bump_pydantic-0.4.0.tar.gz.

File metadata

  • Download URL: bump_pydantic-0.4.0.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for bump_pydantic-0.4.0.tar.gz
Algorithm Hash digest
SHA256 1f45ec83b511b87ccb8868630a45a693fa70263a854f182dc5f6cd2ffac1d771
MD5 41e9606000579d37b91b1a9ce3a86633
BLAKE2b-256 0144c5400a587f289e3db51aae1553ef36d4fd8b53ccdf1e09abf4eb0804e0e9

See more details on using hashes here.

File details

Details for the file bump_pydantic-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bump_pydantic-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b98a4b5542eb400a591646815ed92c1bafbe79938412c5f73ee749ddce92e55
MD5 e6a13d025bf0ea2f744132c1482287fc
BLAKE2b-256 3924727f7386a048876cb564adf6281818cda16aa7687a0a95a6c27b3081cc53

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page