https://github.com/bigbuildbench/pydantic_bump-pydantic
Science Score: 13.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (11.9%) to scientific vocabulary
Repository
Basic Info
- Host: GitHub
- Owner: BigBuildBench
- License: mit
- Language: Python
- Default Branch: master
- Size: 42 KB
Statistics
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
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
-
- Table of contents
- Installation
- Usage
- Check diff before applying changes
- Apply changes
- 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
- BP005: Replace
GenericModelbyBaseModel - BP006: Replace
__root__byRootModel - BP007: Replace decorators
- BP008: Replace
con*functions byAnnotatedversions - BP009: Mark pydantic "protocol" functions in custom types with proper TODOs
- License
Installation
The installation is as simple as:
bash
pip install bump-pydantic
Usage
bump-pydantic is a CLI tool, hence you can use it from your terminal.
It's easy to use. If your project structure is:
bash
repository/
└── my_package/
└── <python source files>
Then you'll want to do:
bash
cd /path/to/repository
bump-pydantic my_package
Check diff before applying changes
To check the diff before applying the changes, you can run:
bash
bump-pydantic --diff <path>
Apply changes
To apply the changes, you can run:
bash
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
NonetoOptional[T]fields.
The following code will be transformed:
py
class User(BaseModel):
name: Optional[str]
Into:
py
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:
```py from pydantic import BaseModel, Extra
class User(BaseModel): name: str
class Config:
extra = Extra.forbid
```
Into:
```py 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. - ✅ Replace
field: Enum = Field(Enum.VALUE, const=True)byfield: Literal[Enum.VALUE] = Enum.VALUE.
The following code will be transformed:
```py from typing import List
from pydantic import BaseModel, Field
class User(BaseModel): name: List[str] = Field(..., min_items=1) ```
Into:
```py 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.
BP005: Replace GenericModel by BaseModel
- ✅ Replace
GenericModelbyBaseModel.
The following code will be transformed:
```py from typing import Generic, TypeVar from pydantic.generics import GenericModel
T = TypeVar('T')
class User(GenericModel, Generic[T]): name: str ```
Into:
```py 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__byRootModel.
The following code will be transformed:
```py from typing import List
from pydantic import BaseModel
class User(BaseModel): age: int name: str
class Users(BaseModel): root = List[User] ```
Into:
```py 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
@validatorby@field_validator. - ✅ Replace
@root_validatorby@model_validator.
The following code will be transformed:
```py 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:
```py from pydantic import BaseModel, fieldvalidator, modelvalidator
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)byAnnotated[str, StringConstraints(*args)]. - ✅ Replace
conint(*args)byAnnotated[int, Field(*args)]. - ✅ Replace
confloat(*args)byAnnotated[float, Field(*args)]. - ✅ Replace
conbytes(*args)byAnnotated[bytes, Field(*args)]. - ✅ Replace
condecimal(*args)byAnnotated[Decimal, Field(*args)]. - ✅ Replace
conset(T, *args)byAnnotated[Set[T], Field(*args)]. - ✅ Replace
confrozenset(T, *args)byAnnotated[Set[T], Field(*args)]. - ✅ Replace
conlist(T, *args)byAnnotated[List[T], Field(*args)].
The following code will be transformed:
```py from pydantic import BaseModel, constr
class User(BaseModel): name: constr(min_length=1) ```
Into:
```py 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:
```py 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:
``py
class SomeThing:
@classmethod
# TODO[pydantic]: We couldn't refactorget_validators, please create thegetpydanticcore_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"
```
BP010: Add type annotations or TODO comments to fields without them
- ✅ Add type annotations based on the default value for a few types that can be inferred, like
bool,str,int,float. - ✅ Add
# TODO[pydantic]: add type annotationcomments to fields that can't be inferred.
The following code will be transformed:
```py from pydantic import BaseModel, Field
class Potato(BaseModel): name: str is_sale = True tags = ["tag1", "tag2"] price = 10.5 description = "Some item" active = Field(default=True) ready = Field(True) age = Field(10, title="Age") ```
Into:
```py from pydantic import BaseModel, Field
class Potato(BaseModel): name: str is_sale: bool = True # TODO[pydantic]: add type annotation tags = ["tag1", "tag2"] price: float = 10.5 description: str = "Some item" active: bool = Field(default=True) ready: bool = Field(True) age: int = Field(10, title="Age") ```
License
This project is licensed under the terms of the MIT license.
Owner
- Name: BigBuildBench
- Login: BigBuildBench
- Kind: organization
- Repositories: 1
- Profile: https://github.com/BigBuildBench
abbr. B3, benchmarking the repo-level understanding capability of your LLMs by reconstructing project build-file.
GitHub Events
Total
- Create event: 2
Last Year
- Create event: 2
Dependencies
- actions/checkout v4 composite
- actions/setup-python v5 composite
- pre-commit/action v3.0.1 composite
- pypa/gh-action-pypi-publish release/v1 composite
- re-actors/alls-green release/v1 composite
- samuelcolvin/check-python-version v4.1 composite