Skip to content

Commit 26f48b6

Browse files
committed
Merge branch 'refs/heads/main' into better-stdout-capture
2 parents 6dd2fee + 47f6c02 commit 26f48b6

File tree

14 files changed

+241
-23
lines changed

14 files changed

+241
-23
lines changed

codeflash/api/cfapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def suggest_changes(
115115
"existingTests": existing_tests,
116116
"generatedTests": generated_tests,
117117
"traceId": trace_id,
118-
"coverage": coverage_message,
118+
"coverage_message": coverage_message,
119119
}
120120
return make_cfapi_request(endpoint="/suggest-pr-changes", method="POST", payload=payload)
121121

@@ -151,7 +151,7 @@ def create_pr(
151151
"existingTests": existing_tests,
152152
"generatedTests": generated_tests,
153153
"traceId": trace_id,
154-
"coverage": coverage_message,
154+
"coverage_message": coverage_message,
155155
}
156156
return make_cfapi_request(endpoint="/create-pr", method="POST", payload=payload)
157157

codeflash/benchmarking/replay_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ def create_trace_replay_test_code(
115115
if function_name == "__init__":
116116
ret = {class_name_alias}(*args[1:], **kwargs)
117117
else:
118-
instance = args[0] # self
119-
ret = instance{method_name}(*args[1:], **kwargs)
118+
ret = {class_name_alias}{method_name}(*args, **kwargs)
120119
"""
121120
)
122121

codeflash/code_utils/code_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from codeflash.cli_cmds.console import logger
1616
from codeflash.code_utils.config_parser import find_pyproject_toml
1717

18+
ImportErrorPattern = re.compile(r"ModuleNotFoundError.*$", re.MULTILINE)
19+
1820

1921
@contextmanager
2022
def custom_addopts() -> None:

codeflash/code_utils/config_consts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
N_TESTS_TO_GENERATE = 2
99
TOTAL_LOOPING_TIME = 10.0 # 10 second candidate benchmarking budget
1010
COVERAGE_THRESHOLD = 60.0
11+
MIN_TESTCASE_PASSED_THRESHOLD = 6

codeflash/code_utils/env_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool =
2222
f.flush()
2323
tmp_file = Path(f.name)
2424
try:
25-
format_code(formatter_cmds, tmp_file)
25+
format_code(formatter_cmds, tmp_file, print_status=False)
2626
except Exception:
2727
print(
2828
"⚠️ Codeflash requires a code formatter to be installed in your environment, but none was found. Please install a supported formatter, verify the formatter-cmds in your codeflash pyproject.toml config and try again."

codeflash/code_utils/formatter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pathlib import Path
1414

1515

16-
def format_code(formatter_cmds: list[str], path: Path) -> str:
16+
def format_code(formatter_cmds: list[str], path: Path, print_status: bool = True) -> str: # noqa
1717
# TODO: Only allow a particular whitelist of formatters here to prevent arbitrary code execution
1818
formatter_name = formatter_cmds[0].lower()
1919
if not path.exists():
@@ -28,7 +28,8 @@ def format_code(formatter_cmds: list[str], path: Path) -> str:
2828
try:
2929
result = subprocess.run(formatter_cmd_list, capture_output=True, check=False)
3030
if result.returncode == 0:
31-
console.rule(f"Formatted Successfully with: {formatter_name.replace('$file', path.name)}")
31+
if print_status:
32+
console.rule(f"Formatted Successfully with: {formatter_name.replace('$file', path.name)}")
3233
else:
3334
logger.error(f"Failed to format code with {' '.join(formatter_cmd_list)}")
3435
except FileNotFoundError as e:

codeflash/discovery/discover_unit_tests.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414

1515
import pytest
1616
from pydantic.dataclasses import dataclass
17+
from rich.panel import Panel
18+
from rich.text import Text
1719

1820
from codeflash.cli_cmds.console import console, logger, test_files_progress_bar
19-
from codeflash.code_utils.code_utils import custom_addopts, get_run_tmp_file, module_name_from_file_path
21+
from codeflash.code_utils.code_utils import (
22+
ImportErrorPattern,
23+
custom_addopts,
24+
get_run_tmp_file,
25+
module_name_from_file_path,
26+
)
2027
from codeflash.code_utils.compat import SAFE_SYS_EXECUTABLE, codeflash_cache_db
2128
from codeflash.models.models import CodePosition, FunctionCalledInTest, TestsInFile, TestType
2229

@@ -180,6 +187,10 @@ def discover_tests_pytest(
180187
logger.warning(
181188
f"Failed to collect tests. Pytest Exit code: {exitcode}={pytest.ExitCode(exitcode).name}\n {error_section}"
182189
)
190+
if "ModuleNotFoundError" in result.stdout:
191+
match = ImportErrorPattern.search(result.stdout).group()
192+
panel = Panel(Text.from_markup(f"⚠️ {match} ", style="bold red"), expand=False)
193+
console.print(panel)
183194

184195
elif 0 <= exitcode <= 5:
185196
logger.warning(f"Failed to collect tests. Pytest Exit code: {exitcode}={pytest.ExitCode(exitcode).name}")

codeflash/discovery/functions_to_optimize.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ def visit_FunctionDef(self, node: FunctionDef) -> None:
9494
self.functions.append(
9595
FunctionToOptimize(function_name=node.name, file_path=self.file_path, parents=self.ast_path[:])
9696
)
97-
# Continue visiting the body of the function to find nested functions
98-
self.generic_visit(node)
9997

10098
def generic_visit(self, node: ast.AST) -> None:
10199
if isinstance(node, (FunctionDef, AsyncFunctionDef, ClassDef)):

codeflash/optimization/function_optimizer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from codeflash.code_utils import env_utils
2424
from codeflash.code_utils.code_replacer import replace_function_definitions_in_module
2525
from codeflash.code_utils.code_utils import (
26+
ImportErrorPattern,
2627
cleanup_paths,
2728
file_name_from_test_module_name,
2829
get_run_tmp_file,
@@ -1192,6 +1193,12 @@ def run_and_parse_tests(
11921193
f"stdout: {run_result.stdout}\n"
11931194
f"stderr: {run_result.stderr}\n"
11941195
)
1196+
if "ModuleNotFoundError" in run_result.stdout:
1197+
from rich.text import Text
1198+
1199+
match = ImportErrorPattern.search(run_result.stdout).group()
1200+
panel = Panel(Text.from_markup(f"⚠️ {match} ", style="bold red"), expand=False)
1201+
console.print(panel)
11951202
if testing_type in {TestingMode.BEHAVIOR, TestingMode.PERFORMANCE}:
11961203
results, coverage_results = parse_test_results(
11971204
test_xml_path=result_file_path,

codeflash/result/critic.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
from codeflash.cli_cmds.console import logger
66
from codeflash.code_utils import env_utils
7-
from codeflash.code_utils.config_consts import COVERAGE_THRESHOLD, MIN_IMPROVEMENT_THRESHOLD
7+
from codeflash.code_utils.config_consts import (
8+
COVERAGE_THRESHOLD,
9+
MIN_IMPROVEMENT_THRESHOLD,
10+
MIN_TESTCASE_PASSED_THRESHOLD,
11+
)
812
from codeflash.models.models import TestType
913

1014
if TYPE_CHECKING:
@@ -50,7 +54,7 @@ def quantity_of_tests_critic(candidate_result: OptimizedCandidateResult) -> bool
5054
for test_type in report:
5155
pass_count += report[test_type]["passed"]
5256

53-
if pass_count >= 4:
57+
if pass_count >= MIN_TESTCASE_PASSED_THRESHOLD:
5458
return True
5559
# If only one test passed, check if it's a REPLAY_TEST
5660
return bool(pass_count == 1 and report[TestType.REPLAY_TEST]["passed"] == 1)

0 commit comments

Comments
 (0)