fix: resolve remaining test failures from main sync

- Update test_inject_profiling_used_frameworks, test_async_run_and_parse,
  test_pickle_patcher to use new inject_profiling_into_existing_test API
  (test_string param removed)
- Add parse_line_profile_results function to parse_line_profile_test_output
  module (imported by main's PythonFunctionOptimizer and test_instrument_tests)
This commit is contained in:
Kevin Turcios 2026-03-04 00:13:27 -05:00
parent af7ce7fce2
commit 92ab600edc
4 changed files with 22 additions and 18 deletions

View file

@ -5,9 +5,15 @@ from __future__ import annotations
import inspect
import linecache
import os
from typing import TYPE_CHECKING, Optional
import dill as pickle
from codeflash.code_utils.tabulate import tabulate
if TYPE_CHECKING:
from pathlib import Path
def show_func(
filename: str, start_lineno: int, func_name: str, timings: list[tuple[int, int, float]], unit: float
@ -112,3 +118,17 @@ def show_text_non_python(stats: dict, line_contents: dict[tuple[str, int], str])
)
out_table += "\n"
return out_table
def parse_line_profile_results(line_profiler_output_file: Optional[Path]) -> tuple[dict, None]:
line_profiler_output_file = line_profiler_output_file.with_suffix(".lprof")
stats_dict: dict = {}
if not line_profiler_output_file.exists():
return {"timings": {}, "unit": 0, "str_out": ""}, None
with line_profiler_output_file.open("rb") as f:
stats = pickle.load(f)
stats_dict["timings"] = stats.timings
stats_dict["unit"] = stats.unit
str_out = show_text(stats_dict)
stats_dict["str_out"] = str_out
return stats_dict, None

View file

@ -805,7 +805,6 @@ def test_sync_sort():
os.chdir(run_cwd)
success, instrumented_test = inject_profiling_into_existing_test(
test_code,
test_path,
[CodePosition(6, 13), CodePosition(10, 13)], # Lines where sync_sorter is called
func,

View file

@ -1105,7 +1105,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(4, 13)],
function_to_optimize=func,
@ -1132,7 +1131,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1159,7 +1157,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1186,7 +1183,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1213,7 +1209,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1240,7 +1235,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1267,7 +1261,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1294,7 +1287,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1322,7 +1314,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(6, 13)],
function_to_optimize=func,
@ -1351,7 +1342,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(7, 13)],
function_to_optimize=func,
@ -1386,7 +1376,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(4, 13)],
function_to_optimize=func,
@ -1413,7 +1402,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1440,7 +1428,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1467,7 +1454,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(5, 13)],
function_to_optimize=func,
@ -1496,7 +1482,6 @@ def test_my_function():
func = FunctionToOptimize(function_name="my_function", parents=[], file_path=Path("mymodule.py"))
success, instrumented_code = inject_profiling_into_existing_test(
test_string=code,
test_path=test_file,
call_positions=[CodePosition(7, 13)],
function_to_optimize=func,

View file

@ -349,7 +349,7 @@ def test_run_and_parse_picklepatch() -> None:
run_cwd = project_root
os.chdir(run_cwd)
success, new_test = inject_profiling_into_existing_test(
original_replay_test_code, replay_test_path, [CodePosition(17, 15)], func, project_root, mode=TestingMode.BEHAVIOR
replay_test_path, [CodePosition(17, 15)], func, project_root, mode=TestingMode.BEHAVIOR
)
os.chdir(original_cwd)
assert success
@ -443,7 +443,7 @@ def bubble_sort_with_unused_socket(data_container):
function_name="bubble_sort_with_used_socket", parents=[], file_path=Path(fto_used_socket_path)
)
success, new_test = inject_profiling_into_existing_test(
original_replay_test_code, replay_test_path, [CodePosition(23, 15)], func, project_root, mode=TestingMode.BEHAVIOR
replay_test_path, [CodePosition(23, 15)], func, project_root, mode=TestingMode.BEHAVIOR
)
os.chdir(original_cwd)
assert success