This example demonstrates how to automatically convert Python dictionary literals into Pydantic models. The codemod makes this process simple by handling all the tedious manual updates automatically.
The script (run.py) automates the entire conversion process in a few key steps:
-
Codebase Loading
codebase = Codebase.from_repo("modal-labs/modal-client")
- Loads your codebase into Codegen's intelligent code analysis engine
- Provides a simple SDK for making codebase-wide changes
- Supports any Git repository as input
-
Dictionary Detection
if "{" in global_var.source and "}" in global_var.source: dict_content = global_var.value.source.strip("{}")
- Automatically identifies dictionary literals in your code
- Processes both global variables and class attributes
- Skips empty dictionaries to avoid unnecessary conversions
-
Schema Creation
class_name = global_var.name.title() + "Schema" model_def = f"""class {class_name}(BaseModel): {dict_content.replace(",", "\n ")}"""
- Generates meaningful model names based on variable names
- Converts dictionary key-value pairs to class attributes
- Maintains proper Python indentation
-
Code Updates
global_var.insert_before(model_def + "\n\n") global_var.set_value(f"{class_name}(**{global_var.value.source})")
- Inserts new Pydantic models in appropriate locations
- Updates dictionary assignments to use the new models
- Automatically adds required Pydantic imports
# Before
config = {"host": "localhost", "port": 8080}
# After
class ConfigSchema(BaseModel):
host: str = "localhost"
port: int = 8080
config = ConfigSchema(**{"host": "localhost", "port": 8080})# Before
class Service:
defaults = {"timeout": 30, "retries": 3}
# After
class DefaultsSchema(BaseModel):
timeout: int = 30
retries: int = 3
class Service:
defaults = DefaultsSchema(**{"timeout": 30, "retries": 3})# Install Codegen
pip install codegen
# Run the conversion
python run.pyFeel free to submit issues and enhancement requests!