|
| 1 | +import codegen |
| 2 | +from codegen import Codebase |
| 3 | + |
| 4 | + |
| 5 | +@codegen.function("dict-to-pydantic-schema") |
| 6 | +def run(codebase: Codebase): |
| 7 | + """Convert dictionary literals to Pydantic models in a Python codebase. |
| 8 | +
|
| 9 | + This codemod: |
| 10 | + 1. Finds all dictionary literals in global variables and class attributes |
| 11 | + 2. Creates corresponding Pydantic models |
| 12 | + 3. Updates the assignments to use the new models |
| 13 | + 4. Adds necessary Pydantic imports |
| 14 | + """ |
| 15 | + # Track statistics |
| 16 | + files_modified = 0 |
| 17 | + models_created = 0 |
| 18 | + |
| 19 | + # Iterate through all files in the codebase |
| 20 | + for file in codebase.files: |
| 21 | + needs_imports = False |
| 22 | + file_modified = False |
| 23 | + |
| 24 | + # Look for dictionary assignments in global variables |
| 25 | + for global_var in file.global_vars: |
| 26 | + try: |
| 27 | + if "{" in global_var.source and "}" in global_var.source: |
| 28 | + dict_content = global_var.value.source.strip("{}") |
| 29 | + if not dict_content.strip(): |
| 30 | + continue |
| 31 | + |
| 32 | + # Convert dict to Pydantic model |
| 33 | + class_name = global_var.name.title() + "Schema" |
| 34 | + model_def = f"""class {class_name}(BaseModel): |
| 35 | + {dict_content.replace(",", "\n ")}""" |
| 36 | + |
| 37 | + print(f"\nConverting '{global_var.name}' to schema") |
| 38 | + print("\nOriginal code:") |
| 39 | + print(global_var.source) |
| 40 | + print("\nNew code:") |
| 41 | + print(model_def) |
| 42 | + print(f"{class_name}(**{global_var.value.source})") |
| 43 | + print("-" * 50) |
| 44 | + |
| 45 | + # Insert model and update assignment |
| 46 | + global_var.insert_before(model_def + "\n\n") |
| 47 | + global_var.set_value(f"{class_name}(**{global_var.value.source})") |
| 48 | + needs_imports = True |
| 49 | + models_created += 1 |
| 50 | + file_modified = True |
| 51 | + except Exception as e: |
| 52 | + print(f"Error processing global variable {global_var.name}: {str(e)}") |
| 53 | + |
| 54 | + # Look for dictionary assignments in class attributes |
| 55 | + for cls in file.classes: |
| 56 | + for attr in cls.attributes: |
| 57 | + try: |
| 58 | + if "{" in attr.source and "}" in attr.source: |
| 59 | + dict_content = attr.value.source.strip("{}") |
| 60 | + if not dict_content.strip(): |
| 61 | + continue |
| 62 | + |
| 63 | + # Convert dict to Pydantic model |
| 64 | + class_name = attr.name.title() + "Schema" |
| 65 | + model_def = f"""class {class_name}(BaseModel): |
| 66 | + {dict_content.replace(",", "\n ")}""" |
| 67 | + |
| 68 | + print(f"\nConverting'{attr.name}' to schema") |
| 69 | + print("\nOriginal code:") |
| 70 | + print(attr.source) |
| 71 | + print("\nNew code:") |
| 72 | + print(model_def) |
| 73 | + print(f"{class_name}(**{attr.value.source})") |
| 74 | + print("-" * 50) |
| 75 | + |
| 76 | + # Insert model and update attribute |
| 77 | + cls.insert_before(model_def + "\n\n") |
| 78 | + attr.set_value(f"{class_name}(**{attr.value.source})") |
| 79 | + needs_imports = True |
| 80 | + models_created += 1 |
| 81 | + file_modified = True |
| 82 | + except Exception as e: |
| 83 | + print(f"Error processing attribute {attr.name} in class {cls.name}: {str(e)}") |
| 84 | + |
| 85 | + # Add imports if needed |
| 86 | + if needs_imports: |
| 87 | + file.add_import_from_import_string("from pydantic import BaseModel") |
| 88 | + |
| 89 | + if file_modified: |
| 90 | + files_modified += 1 |
| 91 | + |
| 92 | + print("\nModification complete:") |
| 93 | + print(f"Files modified: {files_modified}") |
| 94 | + print(f"Schemas created: {models_created}") |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + print("Initializing codebase...") |
| 99 | + codebase = Codebase.from_repo("modal-labs/modal-client") |
| 100 | + |
| 101 | + print("Running codemod...") |
| 102 | + run(codebase) |
0 commit comments