bump-pydantic

0.8.0 · active · verified Fri Apr 17

bump-pydantic is a command-line tool designed to automatically convert Pydantic V1 codebases to Pydantic V2 syntax. It helps users migrate their models, validators, and settings from the old API to the new one, facilitating a smoother transition. The current version is 0.8.0, and it has a regular release cadence, with several minor versions released frequently to add new features and fix issues.

Common errors

Warnings

Install

Quickstart

This quickstart demonstrates how to use `bump-pydantic` to convert a simple Pydantic V1 model to V2 syntax. It creates a temporary Python file, runs the tool on it, and then prints the modified content. In practice, you would run `bump-pydantic` directly from your terminal on your project's directory or specific files.

import os

# Create a dummy Pydantic V1 file for demonstration
with open('my_model_v1.py', 'w') as f:
    f.write(
        """from typing import Optional
from pydantic import BaseModel, Field, EmailStr

class User(BaseModel):
    name: str = Field(..., min_length=1)
    email: EmailStr
    age: Optional[int] = Field(None, example=30)
"""
    )

# Run bump-pydantic on the created file
# In a real scenario, you would run this command in your terminal
# and typically on a directory, e.g., `bump-pydantic .`
# For this quickstart, we'll demonstrate using a single file.
print("Running bump-pydantic on my_model_v1.py...")
os.system("bump-pydantic my_model_v1.py")

# Optionally, print the modified content
with open('my_model_v1.py', 'r') as f:
    print("\n--- Modified my_model_v1.py ---")
    print(f.read())

# Clean up the dummy file
os.remove('my_model_v1.py')

view raw JSON →