@@ -13,46 +13,50 @@ def run(codebase: Codebase):
1313 3. Updates the assignments to use the new models
1414 4. Adds necessary Pydantic imports
1515 """
16- # Track statistics
1716 files_modified = 0
1817 models_created = 0
1918
20- # Iterate through all files in the codebase
21- for file in codebase .files :
19+ total_files = len (codebase .files )
20+ print (f"\n 📁 Scanning { total_files } files for dictionary literals..." )
21+
22+ for i , file in enumerate (codebase .files , 1 ):
2223 needs_imports = False
2324 file_modified = False
2425
25- # Look for dictionary assignments in global variables
26+ print (f"\n 🔍 Checking file { i } /{ total_files } : { file .path } " )
27+
2628 for global_var in file .global_vars :
2729 try :
2830 if "{" in global_var .source and "}" in global_var .source :
2931 dict_content = global_var .value .source .strip ("{}" )
3032 if not dict_content .strip ():
3133 continue
3234
33- # Convert dict to Pydantic model
3435 class_name = global_var .name .title () + "Schema"
3536 model_def = f"""class { class_name } (BaseModel):
3637 { dict_content .replace ("," , "\n " )} """
3738
38- print (f"\n Converting '{ global_var .name } ' to schema" )
39- print ("\n Original code:" )
40- print (global_var .source )
41- print ("\n New code:" )
42- print (model_def )
43- print (f"{ class_name } (**{ global_var .value .source } )" )
44- print ("-" * 50 )
39+ print ("\n " + "=" * 60 )
40+ print (f"🔄 Converting global variable '{ global_var .name } ' to schema" )
41+ print ("=" * 60 )
42+ print ("📝 Original code:" )
43+ print (f" { global_var .name } = { global_var .value .source } " )
44+ print ("\n ✨ Generated schema:" )
45+ print (" " + model_def .replace ("\n " , "\n " ))
46+ print ("\n ✅ Updated code:" )
47+ print (f" { global_var .name } = { class_name } (**{ global_var .value .source } )" )
48+ print ("=" * 60 )
4549
46- # Insert model and update assignment
4750 global_var .insert_before (model_def + "\n \n " )
4851 global_var .set_value (f"{ class_name } (**{ global_var .value .source } )" )
4952 needs_imports = True
5053 models_created += 1
5154 file_modified = True
5255 except Exception as e :
53- print (f"Error processing global variable { global_var .name } : { str (e )} " )
56+ print (f"\n ❌ Error processing global variable '{ global_var .name } ':" )
57+ print (f" { str (e )} " )
58+ print (" Skipping this variable and continuing...\n " )
5459
55- # Look for dictionary assignments in class attributes
5660 for cls in file .classes :
5761 for attr in cls .attributes :
5862 try :
@@ -61,43 +65,55 @@ def run(codebase: Codebase):
6165 if not dict_content .strip ():
6266 continue
6367
64- # Convert dict to Pydantic model
6568 class_name = attr .name .title () + "Schema"
6669 model_def = f"""class { class_name } (BaseModel):
6770 { dict_content .replace ("," , "\n " )} """
6871
69- print (f"\n Converting'{ attr .name } ' to schema" )
70- print ("\n Original code:" )
71- print (attr .source )
72- print ("\n New code:" )
73- print (model_def )
74- print (f"{ class_name } (**{ attr .value .source } )" )
75- print ("-" * 50 )
72+ print ("\n " + "=" * 60 )
73+ print (f"🔄 Converting class attribute '{ cls .name } .{ attr .name } ' to schema" )
74+ print ("=" * 60 )
75+ print ("📝 Original code:" )
76+ print (f" class { cls .name } :" )
77+ print (f" { attr .name } = { attr .value .source } " )
78+ print ("\n ✨ Generated schema:" )
79+ print (" " + model_def .replace ("\n " , "\n " ))
80+ print ("\n ✅ Updated code:" )
81+ print (f" class { cls .name } :" )
82+ print (f" { attr .name } = { class_name } (**{ attr .value .source } )" )
83+ print ("=" * 60 )
7684
77- # Insert model and update attribute
7885 cls .insert_before (model_def + "\n \n " )
7986 attr .set_value (f"{ class_name } (**{ attr .value .source } )" )
8087 needs_imports = True
8188 models_created += 1
8289 file_modified = True
8390 except Exception as e :
84- print (f"Error processing attribute { attr .name } in class { cls .name } : { str (e )} " )
91+ print (f"\n ❌ Error processing attribute '{ attr .name } ' in class '{ cls .name } ':" )
92+ print (f" { str (e )} " )
93+ print (" Skipping this attribute and continuing...\n " )
8594
86- # Add imports if needed
8795 if needs_imports :
96+ print (f" ➕ Adding Pydantic imports to { file .path } " )
8897 file .add_import_from_import_string ("from pydantic import BaseModel" )
8998
9099 if file_modified :
100+ print (f" ✅ Successfully modified { file .path } " )
91101 files_modified += 1
92102
93- print ("\n Modification complete:" )
94- print (f"Files modified: { files_modified } " )
95- print (f"Schemas created: { models_created } " )
96-
103+ print ("\n " + "=" * 60 )
104+ print ("📊 Summary of Changes" )
105+ print ("=" * 60 )
106+ print (f"✨ Files modified: { files_modified } " )
107+ print (f"🔄 Schemas created: { models_created } " )
108+ print ("=" * 60 )
97109
98110if __name__ == "__main__" :
99- print ("Initializing codebase..." )
100- codebase = Codebase .from_repo ("modal-labs/modal-client" , commit = "81941c24897889a2ff2f627c693fa734967e693c" , programming_language = ProgrammingLanguage .PYTHON )
101-
102- print ("Running codemod..." )
111+ print ("\n 🔍 Initializing codebase..." )
112+ codebase = Codebase .from_repo ("fastapi/fastapi" , programming_language = ProgrammingLanguage .PYTHON )
113+ print ("\n 🚀 Running dict-to-pydantic-schema codemod..." )
114+ print ("\n ℹ️ This codemod will:" )
115+ print (" 1. Find dictionary literals in your code" )
116+ print (" 2. Convert them to Pydantic models" )
117+ print (" 3. Update assignments to use the new models" )
118+ print (" 4. Add required imports\n " )
103119 run (codebase )
0 commit comments