Skip to content

Commit cbf6439

Browse files
authored
Merge pull request #33 from codeflash-ai/skip-async-dependent
Skip async dependent functions
2 parents 620a83d + f1dc490 commit cbf6439

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ def validate_python_code(code: str) -> str:
108108
return code
109109

110110

111+
def has_any_async_functions(code: str) -> bool:
112+
try:
113+
module = ast.parse(code)
114+
except SyntaxError:
115+
return False
116+
return any(isinstance(node, ast.AsyncFunctionDef) for node in ast.walk(module))
117+
118+
111119
def cleanup_paths(paths: list[Path]) -> None:
112120
for path in paths:
113121
path.unlink(missing_ok=True)

codeflash/optimization/function_optimizer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
file_name_from_test_module_name,
2929
get_run_tmp_file,
3030
module_name_from_file_path,
31+
has_any_async_functions,
3132
)
3233
from codeflash.code_utils.config_consts import (
3334
INDIVIDUAL_TESTCASE_TIMEOUT,
@@ -133,8 +134,8 @@ def optimize_function(self) -> Result[BestOptimization, str]:
133134
with helper_function_path.open(encoding="utf8") as f:
134135
helper_code = f.read()
135136
original_helper_code[helper_function_path] = helper_code
136-
137-
logger.info("Code to be optimized:")
137+
if has_any_async_functions(code_context.code_to_optimize_with_helpers):
138+
return Failure("Codeflash does not support async functions in the code to optimize.")
138139
code_print(code_context.read_writable_code)
139140

140141
for module_abspath, helper_code_source in original_helper_code.items():

codeflash/optimization/optimizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def run(self) -> None:
139139
validated_original_code[analysis.file_path] = ValidCode(
140140
source_code=callee_original_code, normalized_code=normalized_callee_original_code
141141
)
142+
142143
if has_syntax_error:
143144
continue
144145

@@ -148,7 +149,7 @@ def run(self) -> None:
148149
f"Optimizing function {function_iterator_count} of {num_optimizable_functions}: "
149150
f"{function_to_optimize.qualified_name}"
150151
)
151-
152+
console.rule()
152153
if not (
153154
function_to_optimize_ast := get_first_top_level_function_or_method_ast(
154155
function_to_optimize.function_name, function_to_optimize.parents, original_module_ast
@@ -159,7 +160,6 @@ def run(self) -> None:
159160
f"Skipping optimization."
160161
)
161162
continue
162-
163163
function_optimizer = self.create_function_optimizer(
164164
function_to_optimize,
165165
function_to_optimize_ast,

tests/test_code_utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
is_class_defined_in_file,
1818
module_name_from_file_path,
1919
path_belongs_to_site_packages,
20+
has_any_async_functions,
2021
)
2122
from codeflash.code_utils.concolic_utils import clean_concolic_tests
2223
from codeflash.code_utils.coverage_utils import generate_candidates, prepare_coverage_files
@@ -441,3 +442,27 @@ def test_Grammar_copy():
441442
Grammar.copy(Grammar())
442443
"""
443444
assert cleaned_code == expected_cleaned_code.strip()
445+
446+
447+
def test_has_any_async_functions_with_async_code() -> None:
448+
code = """
449+
def normal_function():
450+
pass
451+
452+
async def async_function():
453+
pass
454+
"""
455+
result = has_any_async_functions(code)
456+
assert result is True
457+
458+
459+
def test_has_any_async_functions_without_async_code() -> None:
460+
code = """
461+
def normal_function():
462+
pass
463+
464+
def another_function():
465+
pass
466+
"""
467+
result = has_any_async_functions(code)
468+
assert result is False

0 commit comments

Comments
 (0)