1515from  codeflash .code_utils .line_profile_utils  import  add_decorator_imports 
1616from  codeflash .discovery .functions_to_optimize  import  FunctionToOptimize 
1717from  codeflash .models .models  import  (
18+     CodeOptimizationContext ,
1819    CodePosition ,
1920    FunctionParent ,
2021    TestFile ,
2122    TestFiles ,
2223    TestingMode ,
2324    TestsInFile ,
24-     TestType ,  CodeOptimizationContext , 
25+     TestType ,
2526)
2627from  codeflash .optimization .function_optimizer  import  FunctionOptimizer 
2728from  codeflash .verification .verification_utils  import  TestConfig 
@@ -518,8 +519,8 @@ def test_sort():
518519            testing_time = 0.1 ,
519520            line_profiler_output_file  =  line_profiler_output_file 
520521        )
521-         tmp_lpr  =  list (line_profile_results [' timings'  ].keys ())
522-         assert  len (tmp_lpr ) ==  1  and  line_profile_results [' timings'  ][tmp_lpr [0 ]][0 ][1 ]== 2 
522+         tmp_lpr  =  list (line_profile_results [" timings"  ].keys ())
523+         assert  len (tmp_lpr ) ==  1  and  line_profile_results [" timings"  ][tmp_lpr [0 ]][0 ][1 ]== 2 
523524    finally :
524525        if  computed_fn_opt :
525526            func_optimizer .write_code_and_helpers (
@@ -771,8 +772,8 @@ def test_sort_parametrized(input, expected_output):
771772            testing_time = 0.1 ,
772773            line_profiler_output_file  =  line_profiler_output_file 
773774        )
774-         tmp_lpr  =  list (line_profile_results [' timings'  ].keys ())
775-         assert  len (tmp_lpr ) ==  1  and  line_profile_results [' timings'  ][tmp_lpr [0 ]][0 ][1 ]== 3 
775+         tmp_lpr  =  list (line_profile_results [" timings"  ].keys ())
776+         assert  len (tmp_lpr ) ==  1  and  line_profile_results [" timings"  ][tmp_lpr [0 ]][0 ][1 ]== 3 
776777    finally :
777778        if  computed_fn_opt :
778779            func_optimizer .write_code_and_helpers (
@@ -1110,8 +1111,8 @@ def test_sort_parametrized_loop(input, expected_output):
11101111            testing_time = 0.1 ,
11111112            line_profiler_output_file  =  line_profiler_output_file 
11121113        )
1113-         tmp_lpr  =  list (line_profile_results [' timings'  ].keys ())
1114-         assert  len (tmp_lpr ) ==  1  and  line_profile_results [' timings'  ][tmp_lpr [0 ]][0 ][1 ]== 6 
1114+         tmp_lpr  =  list (line_profile_results [" timings"  ].keys ())
1115+         assert  len (tmp_lpr ) ==  1  and  line_profile_results [" timings"  ][tmp_lpr [0 ]][0 ][1 ]== 6 
11151116    finally :
11161117        if  computed_fn_opt :
11171118            func_optimizer .write_code_and_helpers (
@@ -1383,8 +1384,8 @@ def test_sort():
13831384            testing_time = 0.1 ,
13841385            line_profiler_output_file  =  line_profiler_output_file 
13851386        )
1386-         tmp_lpr  =  list (line_profile_results [' timings'  ].keys ())
1387-         assert  len (tmp_lpr ) ==  1  and  line_profile_results [' timings'  ][tmp_lpr [0 ]][0 ][1 ]== 3 
1387+         tmp_lpr  =  list (line_profile_results [" timings"  ].keys ())
1388+         assert  len (tmp_lpr ) ==  1  and  line_profile_results [" timings"  ][tmp_lpr [0 ]][0 ][1 ]== 3 
13881389    finally :
13891390        if  computed_fn_opt  is  True :
13901391            func_optimizer .write_code_and_helpers (
@@ -3139,153 +3140,3 @@ def test_sleepfunc_sequence_short(self, n, expected_total_sleep_time):
31393140
31403141    finally :
31413142        test_path .unlink (missing_ok = True )
3142- 
3143- def  test_add_decorator_imports_helper_in_class ():
3144-     code_path  =  (Path (__file__ ).parent .resolve () /  "../code_to_optimize/bubble_sort_classmethod.py" ).resolve ()
3145-     tests_root  =  Path (__file__ ).parent .resolve () /  "../code_to_optimize/tests/pytest/" 
3146-     project_root_path  =  (Path (__file__ ).parent  /  ".." ).resolve ()
3147-     original_cwd  =  Path .cwd ()
3148-     run_cwd  =  Path (__file__ ).parent .parent .resolve ()
3149-     test_config  =  TestConfig (
3150-         tests_root = tests_root ,
3151-         tests_project_rootdir = project_root_path ,
3152-         project_root_path = project_root_path ,
3153-         test_framework = "pytest" ,
3154-         pytest_cmd = "pytest" ,
3155-     )
3156-     func  =  FunctionToOptimize (function_name = "sort_classmethod" , parents = [], file_path = code_path )
3157-     func_optimizer  =  FunctionOptimizer (function_to_optimize = func , test_cfg = test_config )
3158-     os .chdir (run_cwd )
3159-     #func_optimizer = pass 
3160-     try :
3161-         ctx_result  =  func_optimizer .get_code_optimization_context ()
3162-         code_context : CodeOptimizationContext  =  ctx_result .unwrap ()
3163-         original_helper_code : dict [Path , str ] =  {}
3164-         helper_function_paths  =  {hf .file_path  for  hf  in  code_context .helper_functions }
3165-         for  helper_function_path  in  helper_function_paths :
3166-             with  helper_function_path .open (encoding = "utf8" ) as  f :
3167-                 helper_code  =  f .read ()
3168-                 original_helper_code [helper_function_path ] =  helper_code 
3169-         computed_fn_opt  =  True 
3170-         line_profiler_output_file  =  add_decorator_imports (
3171-             func_optimizer .function_to_optimize , code_context )
3172-         expected_code_main  =  f"""from line_profiler import profile as codeflash_line_profile 
3173- codeflash_line_profile.enable(output_prefix='{ line_profiler_output_file }  ') 
3174- 
3175- from code_to_optimize.bubble_sort_in_class import BubbleSortClass 
3176- 
3177- 
3178- @codeflash_line_profile 
3179- def sort_classmethod(x): 
3180-     y = BubbleSortClass() 
3181-     return y.sorter(x) 
3182- """ 
3183-         expected_code_helper  =  """from line_profiler import profile as codeflash_line_profile 
3184- 
3185- 
3186- def hi(): 
3187-     pass 
3188- 
3189- 
3190- class BubbleSortClass: 
3191-     def __init__(self): 
3192-         pass 
3193- 
3194-     @codeflash_line_profile 
3195-     def sorter(self, arr): 
3196-         n = len(arr) 
3197-         for i in range(n): 
3198-             for j in range(0, n - i - 1): 
3199-                 if arr[j] > arr[j + 1]: 
3200-                     arr[j], arr[j + 1] = arr[j + 1], arr[j] 
3201-         return arr 
3202- 
3203-     def helper(self, arr, j): 
3204-         return arr[j] > arr[j + 1] 
3205- """ 
3206-         assert  code_path .read_text ("utf-8" ) ==  expected_code_main 
3207-         assert  code_context .helper_functions [0 ].file_path .read_text ("utf-8" ) ==  expected_code_helper 
3208-     finally :
3209-         #if computed_fn_opt: 
3210-         func_optimizer .write_code_and_helpers (
3211-             func_optimizer .function_to_optimize_source_code , original_helper_code , func_optimizer .function_to_optimize .file_path 
3212-         )
3213- 
3214- def  test_add_decorator_imports_helper_in_nested_class ():
3215-     code_path  =  (Path (__file__ ).parent .resolve () /  "../code_to_optimize/bubble_sort_nested_classmethod.py" ).resolve ()
3216-     tests_root  =  Path (__file__ ).parent .resolve () /  "../code_to_optimize/tests/pytest/" 
3217-     project_root_path  =  (Path (__file__ ).parent  /  ".." ).resolve ()
3218-     original_cwd  =  Path .cwd ()
3219-     run_cwd  =  Path (__file__ ).parent .parent .resolve ()
3220-     test_config  =  TestConfig (
3221-         tests_root = tests_root ,
3222-         tests_project_rootdir = project_root_path ,
3223-         project_root_path = project_root_path ,
3224-         test_framework = "pytest" ,
3225-         pytest_cmd = "pytest" ,
3226-     )
3227-     func  =  FunctionToOptimize (function_name = "sort_classmethod" , parents = [], file_path = code_path )
3228-     func_optimizer  =  FunctionOptimizer (function_to_optimize = func , test_cfg = test_config )
3229-     os .chdir (run_cwd )
3230-     #func_optimizer = pass 
3231-     try :
3232-         ctx_result  =  func_optimizer .get_code_optimization_context ()
3233-         code_context : CodeOptimizationContext  =  ctx_result .unwrap ()
3234-         original_helper_code : dict [Path , str ] =  {}
3235-         helper_function_paths  =  {hf .file_path  for  hf  in  code_context .helper_functions }
3236-         for  helper_function_path  in  helper_function_paths :
3237-             with  helper_function_path .open (encoding = "utf8" ) as  f :
3238-                 helper_code  =  f .read ()
3239-                 original_helper_code [helper_function_path ] =  helper_code 
3240-         computed_fn_opt  =  True 
3241-         line_profiler_output_file  =  add_decorator_imports (
3242-             func_optimizer .function_to_optimize , code_context )
3243-         expected_code_main  =  f"""from line_profiler import profile as codeflash_line_profile 
3244- codeflash_line_profile.enable(output_prefix='{ line_profiler_output_file }  ') 
3245- 
3246- from code_to_optimize.bubble_sort_in_nested_class import WrapperClass 
3247- 
3248- 
3249- @codeflash_line_profile 
3250- def sort_classmethod(x): 
3251-     y = WrapperClass.BubbleSortClass() 
3252-     return y.sorter(x) 
3253- """ 
3254-         expected_code_helper  =  """from line_profiler import profile as codeflash_line_profile 
3255- 
3256- 
3257- def hi(): 
3258-     pass 
3259- 
3260- 
3261- class WrapperClass: 
3262-     def __init__(self): 
3263-         pass 
3264- 
3265-     class BubbleSortClass: 
3266-         def __init__(self): 
3267-             pass 
3268- 
3269-         @codeflash_line_profile 
3270-         def sorter(self, arr): 
3271-             def inner_helper(arr, j): 
3272-                 return arr[j] > arr[j + 1] 
3273- 
3274-             for i in range(len(arr)): 
3275-                 for j in range(len(arr) - 1): 
3276-                     if arr[j] > arr[j + 1]: 
3277-                         temp = arr[j] 
3278-                         arr[j] = arr[j + 1] 
3279-                         arr[j + 1] = temp 
3280-             return arr 
3281- 
3282-         def helper(self, arr, j): 
3283-             return arr[j] > arr[j + 1] 
3284- """ 
3285-         assert  code_path .read_text ("utf-8" ) ==  expected_code_main 
3286-         assert  code_context .helper_functions [0 ].file_path .read_text ("utf-8" ) ==  expected_code_helper 
3287-     finally :
3288-         #if computed_fn_opt: 
3289-         func_optimizer .write_code_and_helpers (
3290-             func_optimizer .function_to_optimize_source_code , original_helper_code , func_optimizer .function_to_optimize .file_path 
3291-         )
0 commit comments