Skip to content

Commit 6a916ac

Browse files
committed
fix: address review feedback on PythonFunctionOptimizer extraction
- Add clarifying comment on shared replace_function_definitions_in_module import - Remove misleading alias in test_unused_helper_revert.py, use PythonFunctionOptimizer directly - Align base line_profiler_step return type to dict[str, Any] - Fix latent bug: handle non-empty TestResults in line_profiler_step
1 parent 2b40e4b commit 6a916ac

3 files changed

Lines changed: 26 additions & 23 deletions

File tree

codeflash/languages/python/function_optimizer.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,11 @@ def line_profiler_step(
177177
self.write_code_and_helpers(
178178
self.function_to_optimize_source_code, original_helper_code, self.function_to_optimize.file_path
179179
)
180-
if isinstance(line_profile_results, TestResults) and not line_profile_results.test_results:
181-
logger.warning(
182-
f"Timeout occurred while running line profiler for original function {self.function_to_optimize.function_name}"
183-
)
180+
if isinstance(line_profile_results, TestResults):
181+
if not line_profile_results.test_results:
182+
logger.warning(
183+
f"Timeout occurred while running line profiler for original function {self.function_to_optimize.function_name}"
184+
)
184185
return {"timings": {}, "unit": 0, "str_out": ""}
185186
if line_profile_results["str_out"] == "":
186187
logger.warning(

codeflash/optimization/function_optimizer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
if TYPE_CHECKING:
117117
import ast
118118
from argparse import Namespace
119+
from typing import Any
119120

120121
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
121122
from codeflash.either import Result
@@ -1545,6 +1546,7 @@ def replace_function_and_helpers_with_optimized_code(
15451546
optimized_code: CodeStringsMarkdown,
15461547
original_helper_code: dict[Path, str],
15471548
) -> bool:
1549+
# Despite the module path, this function dispatches to language-specific replacers internally
15481550
from codeflash.languages.python.static_analysis.code_replacer import replace_function_definitions_in_module
15491551

15501552
did_update = False
@@ -2776,7 +2778,7 @@ def get_test_env(
27762778

27772779
def line_profiler_step(
27782780
self, code_context: CodeOptimizationContext, original_helper_code: dict[Path, str], candidate_index: int
2779-
) -> dict:
2781+
) -> dict[str, Any]:
27802782
if self.language_support is not None and hasattr(self.language_support, "instrument_source_for_line_profiler"):
27812783
try:
27822784
line_profiler_output_path = get_run_tmp_file(Path("line_profiler_output.json"))

tests/test_unused_helper_revert.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
detect_unused_helper_functions,
1111
revert_unused_helper_functions,
1212
)
13-
from codeflash.languages.python.function_optimizer import PythonFunctionOptimizer as FunctionOptimizer
13+
from codeflash.languages.python.function_optimizer import PythonFunctionOptimizer
1414
from codeflash.models.models import CodeStringsMarkdown
1515
from codeflash.verification.verification_utils import TestConfig
1616

@@ -83,7 +83,7 @@ def helper_function_2(x):
8383
)
8484

8585
# Create function optimizer
86-
optimizer = FunctionOptimizer(
86+
optimizer = PythonFunctionOptimizer(
8787
function_to_optimize=function_to_optimize,
8888
test_cfg=test_cfg,
8989
function_to_optimize_source_code=main_file.read_text(),
@@ -194,7 +194,7 @@ def helper_function_2(x):
194194
)
195195

196196
# Create function optimizer
197-
optimizer = FunctionOptimizer(
197+
optimizer = PythonFunctionOptimizer(
198198
function_to_optimize=function_to_optimize,
199199
test_cfg=test_cfg,
200200
function_to_optimize_source_code=main_file.read_text(),
@@ -269,7 +269,7 @@ def helper_function_2(x):
269269
)
270270

271271
# Create function optimizer
272-
optimizer = FunctionOptimizer(
272+
optimizer = PythonFunctionOptimizer(
273273
function_to_optimize=function_to_optimize,
274274
test_cfg=test_cfg,
275275
function_to_optimize_source_code=main_file.read_text(),
@@ -365,7 +365,7 @@ def entrypoint_function(n):
365365
)
366366

367367
# Create function optimizer
368-
optimizer = FunctionOptimizer(
368+
optimizer = PythonFunctionOptimizer(
369369
function_to_optimize=function_to_optimize,
370370
test_cfg=test_cfg,
371371
function_to_optimize_source_code=main_file.read_text(),
@@ -559,7 +559,7 @@ def helper_method_2(self, x):
559559
)
560560

561561
# Create function optimizer
562-
optimizer = FunctionOptimizer(
562+
optimizer = PythonFunctionOptimizer(
563563
function_to_optimize=function_to_optimize,
564564
test_cfg=test_cfg,
565565
function_to_optimize_source_code=main_file.read_text(),
@@ -710,7 +710,7 @@ def process_data(self, n):
710710
)
711711

712712
# Create function optimizer
713-
optimizer = FunctionOptimizer(
713+
optimizer = PythonFunctionOptimizer(
714714
function_to_optimize=function_to_optimize,
715715
test_cfg=test_cfg,
716716
function_to_optimize_source_code=main_file.read_text(),
@@ -895,7 +895,7 @@ def local_helper(self, x):
895895
)
896896

897897
# Create function optimizer
898-
optimizer = FunctionOptimizer(
898+
optimizer = PythonFunctionOptimizer(
899899
function_to_optimize=function_to_optimize,
900900
test_cfg=test_cfg,
901901
function_to_optimize_source_code=main_file.read_text(),
@@ -1051,7 +1051,7 @@ def entrypoint_function(n):
10511051
)
10521052

10531053
# Create function optimizer
1054-
optimizer = FunctionOptimizer(
1054+
optimizer = PythonFunctionOptimizer(
10551055
function_to_optimize=function_to_optimize,
10561056
test_cfg=test_cfg,
10571057
function_to_optimize_source_code=main_file.read_text(),
@@ -1215,7 +1215,7 @@ def entrypoint_function(n):
12151215
)
12161216

12171217
# Create function optimizer
1218-
optimizer = FunctionOptimizer(
1218+
optimizer = PythonFunctionOptimizer(
12191219
function_to_optimize=function_to_optimize,
12201220
test_cfg=test_cfg,
12211221
function_to_optimize_source_code=main_file.read_text(),
@@ -1442,7 +1442,7 @@ def calculate_class(cls, n):
14421442
)
14431443

14441444
# Create function optimizer
1445-
optimizer = FunctionOptimizer(
1445+
optimizer = PythonFunctionOptimizer(
14461446
function_to_optimize=function_to_optimize,
14471447
test_cfg=test_cfg,
14481448
function_to_optimize_source_code=main_file.read_text(),
@@ -1576,7 +1576,7 @@ async def async_entrypoint(n):
15761576
)
15771577

15781578
# Create function optimizer
1579-
optimizer = FunctionOptimizer(
1579+
optimizer = PythonFunctionOptimizer(
15801580
function_to_optimize=function_to_optimize,
15811581
test_cfg=test_cfg,
15821582
function_to_optimize_source_code=main_file.read_text(),
@@ -1664,7 +1664,7 @@ def sync_entrypoint(n):
16641664
function_to_optimize = FunctionToOptimize(file_path=main_file, function_name="sync_entrypoint", parents=[])
16651665

16661666
# Create function optimizer
1667-
optimizer = FunctionOptimizer(
1667+
optimizer = PythonFunctionOptimizer(
16681668
function_to_optimize=function_to_optimize,
16691669
test_cfg=test_cfg,
16701670
function_to_optimize_source_code=main_file.read_text(),
@@ -1773,7 +1773,7 @@ async def mixed_entrypoint(n):
17731773
)
17741774

17751775
# Create function optimizer
1776-
optimizer = FunctionOptimizer(
1776+
optimizer = PythonFunctionOptimizer(
17771777
function_to_optimize=function_to_optimize,
17781778
test_cfg=test_cfg,
17791779
function_to_optimize_source_code=main_file.read_text(),
@@ -1874,7 +1874,7 @@ def sync_helper_method(self, x):
18741874
)
18751875

18761876
# Create function optimizer
1877-
optimizer = FunctionOptimizer(
1877+
optimizer = PythonFunctionOptimizer(
18781878
function_to_optimize=function_to_optimize,
18791879
test_cfg=test_cfg,
18801880
function_to_optimize_source_code=main_file.read_text(),
@@ -1960,7 +1960,7 @@ async def async_entrypoint(n):
19601960
)
19611961

19621962
# Create function optimizer
1963-
optimizer = FunctionOptimizer(
1963+
optimizer = PythonFunctionOptimizer(
19641964
function_to_optimize=function_to_optimize,
19651965
test_cfg=test_cfg,
19661966
function_to_optimize_source_code=main_file.read_text(),
@@ -2039,7 +2039,7 @@ def gcd_recursive(a: int, b: int) -> int:
20392039
function_to_optimize = FunctionToOptimize(file_path=main_file, function_name="gcd_recursive", parents=[])
20402040

20412041
# Create function optimizer
2042-
optimizer = FunctionOptimizer(
2042+
optimizer = PythonFunctionOptimizer(
20432043
function_to_optimize=function_to_optimize,
20442044
test_cfg=test_cfg,
20452045
function_to_optimize_source_code=main_file.read_text(),
@@ -2152,7 +2152,7 @@ async def async_entrypoint_with_generators(n):
21522152
)
21532153

21542154
# Create function optimizer
2155-
optimizer = FunctionOptimizer(
2155+
optimizer = PythonFunctionOptimizer(
21562156
function_to_optimize=function_to_optimize,
21572157
test_cfg=test_cfg,
21582158
function_to_optimize_source_code=main_file.read_text(),

0 commit comments

Comments
 (0)