@@ -173,10 +173,54 @@ def create_function_optimizer(
173173 replay_tests_dir = self .replay_tests_dir ,
174174 )
175175
176- def run (self ) -> None :
177- from codeflash .code_utils .checkpoint import CodeflashRunCheckpoint
176+ def prepare_module_for_optimization (
177+ self , original_module_path : Path
178+ ) -> tuple [dict [Path , ValidCode ], ast .Module ] | None :
178179 from codeflash .code_utils .code_replacer import normalize_code , normalize_node
179180 from codeflash .code_utils .static_analysis import analyze_imported_modules
181+
182+ logger .info (f"Examining file { original_module_path !s} …" )
183+ console .rule ()
184+
185+ original_module_code : str = original_module_path .read_text (encoding = "utf8" )
186+ try :
187+ original_module_ast = ast .parse (original_module_code )
188+ except SyntaxError as e :
189+ logger .warning (f"Syntax error parsing code in { original_module_path } : { e } " )
190+ logger .info ("Skipping optimization due to file error." )
191+ return None
192+ normalized_original_module_code = ast .unparse (normalize_node (original_module_ast ))
193+ validated_original_code : dict [Path , ValidCode ] = {
194+ original_module_path : ValidCode (
195+ source_code = original_module_code , normalized_code = normalized_original_module_code
196+ )
197+ }
198+
199+ imported_module_analyses = analyze_imported_modules (
200+ original_module_code , original_module_path , self .args .project_root
201+ )
202+
203+ has_syntax_error = False
204+ for analysis in imported_module_analyses :
205+ callee_original_code = analysis .file_path .read_text (encoding = "utf8" )
206+ try :
207+ normalized_callee_original_code = normalize_code (callee_original_code )
208+ except SyntaxError as e :
209+ logger .warning (f"Syntax error parsing code in callee module { analysis .file_path } : { e } " )
210+ logger .info ("Skipping optimization due to helper file error." )
211+ has_syntax_error = True
212+ break
213+ validated_original_code [analysis .file_path ] = ValidCode (
214+ source_code = callee_original_code , normalized_code = normalized_callee_original_code
215+ )
216+
217+ if has_syntax_error :
218+ return None
219+
220+ return validated_original_code , original_module_ast
221+
222+ def run (self ) -> None :
223+ from codeflash .code_utils .checkpoint import CodeflashRunCheckpoint
180224 from codeflash .discovery .discover_unit_tests import discover_unit_tests
181225
182226 ph ("cli-optimize-run-start" )
@@ -223,43 +267,11 @@ def run(self) -> None:
223267 self .functions_checkpoint = CodeflashRunCheckpoint (self .args .module_root )
224268
225269 for original_module_path in file_to_funcs_to_optimize :
226- logger .info (f"Examining file { original_module_path !s} …" )
227- console .rule ()
228-
229- original_module_code : str = original_module_path .read_text (encoding = "utf8" )
230- try :
231- original_module_ast = ast .parse (original_module_code )
232- except SyntaxError as e :
233- logger .warning (f"Syntax error parsing code in { original_module_path } : { e } " )
234- logger .info ("Skipping optimization due to file error." )
270+ module_prep_result = self .prepare_module_for_optimization (original_module_path )
271+ if module_prep_result is None :
235272 continue
236- normalized_original_module_code = ast .unparse (normalize_node (original_module_ast ))
237- validated_original_code : dict [Path , ValidCode ] = {
238- original_module_path : ValidCode (
239- source_code = original_module_code , normalized_code = normalized_original_module_code
240- )
241- }
242273
243- imported_module_analyses = analyze_imported_modules (
244- original_module_code , original_module_path , self .args .project_root
245- )
246-
247- has_syntax_error = False
248- for analysis in imported_module_analyses :
249- callee_original_code = analysis .file_path .read_text (encoding = "utf8" )
250- try :
251- normalized_callee_original_code = normalize_code (callee_original_code )
252- except SyntaxError as e :
253- logger .warning (f"Syntax error parsing code in callee module { analysis .file_path } : { e } " )
254- logger .info ("Skipping optimization due to helper file error." )
255- has_syntax_error = True
256- break
257- validated_original_code [analysis .file_path ] = ValidCode (
258- source_code = callee_original_code , normalized_code = normalized_callee_original_code
259- )
260-
261- if has_syntax_error :
262- continue
274+ validated_original_code , original_module_ast = module_prep_result
263275
264276 for function_to_optimize in file_to_funcs_to_optimize [original_module_path ]:
265277 function_iterator_count += 1
0 commit comments