Skip to content

Commit ce32dbb

Browse files
authored
Merge branch 'main' into lsp/config-suggestions-configured-values
2 parents 4cf4e9e + 16ff462 commit ce32dbb

File tree

5 files changed

+1125
-8
lines changed

5 files changed

+1125
-8
lines changed

codeflash/code_utils/formatter.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ def is_diff_line(line: str) -> bool:
9696
return len(diff_lines)
9797

9898

99+
def format_generated_code(generated_test_source: str, formatter_cmds: list[str]) -> str:
100+
with tempfile.TemporaryDirectory() as test_dir_str:
101+
# try running formatter, if nothing changes (could be due to formatting failing or no actual formatting needed) return code with 2 or more newlines substituted with 2 newlines
102+
original_temp = Path(test_dir_str) / "original_temp.py"
103+
original_temp.write_text(generated_test_source, encoding="utf8")
104+
_, formatted_code, changed = apply_formatter_cmds(
105+
formatter_cmds, original_temp, test_dir_str, print_status=False, exit_on_failure=False
106+
)
107+
if not changed:
108+
return re.sub(r"\n{2,}", "\n\n", formatted_code)
109+
return formatted_code
110+
111+
99112
def format_code(
100113
formatter_cmds: list[str],
101114
path: Union[str, Path],
@@ -120,7 +133,7 @@ def format_code(
120133
original_code_lines = len(original_code.split("\n"))
121134

122135
if check_diff and original_code_lines > 50:
123-
# we dont' count the formatting diff for the optimized function as it should be well-formatted
136+
# we don't count the formatting diff for the optimized function as it should be well-formatted
124137
original_code_without_opfunc = original_code.replace(optimized_code, "")
125138

126139
original_temp = Path(test_dir_str) / "original_temp.py"

codeflash/models/models.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from collections import defaultdict
3+
from collections import Counter, defaultdict
44
from typing import TYPE_CHECKING
55

66
from rich.tree import Tree
@@ -675,6 +675,16 @@ def total_passed_runtime(self) -> int:
675675
[min(usable_runtime_data) for _, usable_runtime_data in self.usable_runtime_data_by_test_case().items()]
676676
)
677677

678+
def file_to_no_of_tests(self, test_functions_to_remove: list[str]) -> Counter[Path]:
679+
map_gen_test_file_to_no_of_tests = Counter()
680+
for gen_test_result in self.test_results:
681+
if (
682+
gen_test_result.test_type == TestType.GENERATED_REGRESSION
683+
and gen_test_result.id.test_function_name not in test_functions_to_remove
684+
):
685+
map_gen_test_file_to_no_of_tests[gen_test_result.file_name] += 1
686+
return map_gen_test_file_to_no_of_tests
687+
678688
def __iter__(self) -> Iterator[FunctionTestInvocation]:
679689
return iter(self.test_results)
680690

codeflash/optimization/function_optimizer.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
remove_functions_from_generated_tests,
5656
)
5757
from codeflash.code_utils.env_utils import get_pr_number
58-
from codeflash.code_utils.formatter import format_code, sort_imports
58+
from codeflash.code_utils.formatter import format_code, format_generated_code, sort_imports
5959
from codeflash.code_utils.git_utils import git_root_dir
6060
from codeflash.code_utils.instrument_existing_tests import inject_profiling_into_existing_test
6161
from codeflash.code_utils.line_profile_utils import add_decorator_imports
@@ -1401,6 +1401,9 @@ def process_review(
14011401
generated_tests = remove_functions_from_generated_tests(
14021402
generated_tests=generated_tests, test_functions_to_remove=test_functions_to_remove
14031403
)
1404+
map_gen_test_file_to_no_of_tests = original_code_baseline.behavior_test_results.file_to_no_of_tests(
1405+
test_functions_to_remove
1406+
)
14041407

14051408
original_runtime_by_test = original_code_baseline.benchmarking_test_results.usable_runtime_data_by_test_case()
14061409
optimized_runtime_by_test = (
@@ -1413,11 +1416,16 @@ def process_review(
14131416

14141417
generated_tests_str = ""
14151418
for test in generated_tests.generated_tests:
1416-
generated_tests_str += f"```python\n{test.generated_original_test_source}\n```"
1417-
generated_tests_str += "\n\n"
1419+
if map_gen_test_file_to_no_of_tests[test.behavior_file_path] > 0:
1420+
formatted_generated_test = format_generated_code(
1421+
test.generated_original_test_source, self.args.formatter_cmds
1422+
)
1423+
generated_tests_str += f"```python\n{formatted_generated_test}\n```"
1424+
generated_tests_str += "\n\n"
14181425

14191426
if concolic_test_str:
1420-
generated_tests_str += f"```python\n{concolic_test_str}\n```\n\n"
1427+
formatted_generated_test = format_generated_code(concolic_test_str, self.args.formatter_cmds)
1428+
generated_tests_str += f"```python\n{formatted_generated_test}\n```\n\n"
14211429

14221430
existing_tests, replay_tests, concolic_tests = existing_tests_source_for(
14231431
self.function_to_optimize.qualified_name_with_modules_from_root(self.project_root),
@@ -1533,7 +1541,7 @@ def process_review(
15331541
trace_id=self.function_trace_id, is_optimization_found=best_optimization is not None
15341542
)
15351543

1536-
# If worktree mode, do not revert code and helpers,, otherwise we would have an empty diff when writing the patch in the lsp
1544+
# If worktree mode, do not revert code and helpers, otherwise we would have an empty diff when writing the patch in the lsp
15371545
if self.args.worktree:
15381546
return
15391547

0 commit comments

Comments
 (0)