Skip to content

Latest commit

 

History

History
93 lines (71 loc) · 2.44 KB

File metadata and controls

93 lines (71 loc) · 2.44 KB

Dict to Schema

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.

How the Conversion Script Works

The script (run.py) automates the entire conversion process in a few key steps:

  1. 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
  2. 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
  3. 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
  4. 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

Common Conversion Patterns

Global Variables

# Before
config = {"host": "localhost", "port": 8080}

# After
class ConfigSchema(BaseModel):
    host: str = "localhost"
    port: int = 8080

config = ConfigSchema(**{"host": "localhost", "port": 8080})

Class Attributes

# 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})

Running the Conversion

# Install Codegen
pip install codegen

# Run the conversion
python run.py

Learn More

Contributing

Feel free to submit issues and enhancement requests!