Skip to content

Commit 4759f07

Browse files
committed
remove has_any_async_functions
race cond and invalid method
1 parent 2700650 commit 4759f07

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

codeflash/code_utils/code_utils.py

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

253253

254-
def has_any_async_functions(code: str) -> bool:
255-
try:
256-
module = ast.parse(code)
257-
except SyntaxError:
258-
return False
259-
return any(isinstance(node, ast.AsyncFunctionDef) for node in ast.walk(module))
260-
261-
262254
def cleanup_paths(paths: list[Path]) -> None:
263255
for path in paths:
264256
if path and path.exists():

codeflash/verification/coverage_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ def load_from_sqlite_database(
3838

3939
cov = Coverage(data_file=database_path, config_file=config_path, data_suffix=True, auto_data=True, branch=True)
4040

41-
if not database_path.stat().st_size or not database_path.exists():
41+
if not database_path.exists() or not database_path.stat().st_size:
4242
logger.debug(f"Coverage database {database_path} is empty or does not exist")
4343
sentry_sdk.capture_message(f"Coverage database {database_path} is empty or does not exist")
44-
return CoverageUtils.create_empty(source_code_path, function_name, code_context)
44+
return CoverageData.create_empty(source_code_path, function_name, code_context)
4545
cov.load()
4646

4747
reporter = JsonReporter(cov)
@@ -51,7 +51,7 @@ def load_from_sqlite_database(
5151
reporter.report(morfs=[source_code_path.as_posix()], outfile=f)
5252
except NoDataError:
5353
sentry_sdk.capture_message(f"No coverage data found for {function_name} in {source_code_path}")
54-
return CoverageUtils.create_empty(source_code_path, function_name, code_context)
54+
return CoverageData.create_empty(source_code_path, function_name, code_context)
5555
with temp_json_file.open() as f:
5656
original_coverage_data = json.load(f)
5757

tests/test_code_utils.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +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,
20+
validate_python_code,
2121
)
2222
from codeflash.code_utils.concolic_utils import clean_concolic_tests
2323
from codeflash.code_utils.coverage_utils import generate_candidates, prepare_coverage_files
@@ -445,25 +445,41 @@ def test_Grammar_copy():
445445
assert cleaned_code == expected_cleaned_code.strip()
446446

447447

448-
def test_has_any_async_functions_with_async_code() -> None:
449-
code = """
450-
def normal_function():
451-
pass
448+
def test_validate_python_code_valid() -> None:
449+
code = "def hello():\n return 'world'"
450+
result = validate_python_code(code)
451+
assert result == code
452452

453-
async def async_function():
454-
pass
455-
"""
456-
result = has_any_async_functions(code)
457-
assert result is True
458453

454+
def test_validate_python_code_invalid() -> None:
455+
code = "def hello(:\n return 'world'"
456+
with pytest.raises(ValueError, match="Invalid Python code"):
457+
validate_python_code(code)
459458

460-
def test_has_any_async_functions_without_async_code() -> None:
461-
code = """
462-
def normal_function():
463-
pass
464459

465-
def another_function():
466-
pass
460+
def test_validate_python_code_empty() -> None:
461+
code = ""
462+
result = validate_python_code(code)
463+
assert result == code
464+
465+
466+
def test_validate_python_code_complex_invalid() -> None:
467+
code = "if True\n print('missing colon')"
468+
with pytest.raises(ValueError, match="Invalid Python code.*line 1.*column 8"):
469+
validate_python_code(code)
470+
471+
472+
def test_validate_python_code_valid_complex() -> None:
473+
code = """
474+
def calculate(a, b):
475+
if a > b:
476+
return a + b
477+
else:
478+
return a * b
479+
480+
class MyClass:
481+
def __init__(self):
482+
self.value = 42
467483
"""
468-
result = has_any_async_functions(code)
469-
assert result is False
484+
result = validate_python_code(code)
485+
assert result == code

0 commit comments

Comments
 (0)