Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions codeflash/languages/python/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
CodeContext,
FunctionFilterCriteria,
HelperFunction,
Language,
ReferenceInfo,
TestInfo,
TestResult,
)
from codeflash.languages.language_enum import Language
from codeflash.languages.registry import register_language
from codeflash.models.function_types import FunctionParent

Expand Down Expand Up @@ -48,8 +48,8 @@ def function_sources_to_helpers(sources: list[FunctionSource]) -> list[HelperFun
qualified_name=fs.qualified_name,
file_path=fs.file_path,
source_code=fs.source_code,
start_line=fs.jedi_definition.line if fs.jedi_definition else 1,
end_line=fs.jedi_definition.line if fs.jedi_definition else 1,
start_line=getattr(getattr(fs, "jedi_definition", None), "line", 1),
end_line=getattr(getattr(fs, "jedi_definition", None), "line", 1),
)
for fs in sources
]
Expand Down Expand Up @@ -119,7 +119,7 @@ def visit_FunctionDef(self, node: cst.FunctionDef) -> None:
)


@register_language
@register_language # type: ignore[arg-type] # PythonSupport satisfies LanguageSupport protocol structurally
class PythonSupport:
"""Python language support implementation.

Expand Down Expand Up @@ -214,6 +214,7 @@ def load_coverage(
) -> Any:
from codeflash.verification.coverage_utils import CoverageUtils

assert coverage_config_file is not None
return CoverageUtils.load_from_sqlite_database(
database_path=coverage_database_file,
config_path=coverage_config_file,
Expand Down Expand Up @@ -854,7 +855,7 @@ def compare_test_results(
candidate_results_path: Path,
project_root: Path | None = None,
project_classpath: str | None = None,
) -> tuple[bool, list]:
) -> tuple[bool, list[Any]]:
"""Compare test results between original and candidate code.

Args:
Expand Down Expand Up @@ -1017,7 +1018,7 @@ def instrument_source_for_line_profiler(
# This is handled through the existing infrastructure
return True

def parse_line_profile_results(self, line_profiler_output_file: Path) -> dict:
def parse_line_profile_results(self, line_profiler_output_file: Path) -> dict[str, Any]:
"""Parse line profiler output for Python.

Args:
Expand Down Expand Up @@ -1078,7 +1079,7 @@ def run_behavioral_tests(
from codeflash.code_utils.config_consts import TOTAL_LOOPING_TIME_EFFECTIVE
from codeflash.languages.python.static_analysis.coverage_utils import prepare_coverage_files
from codeflash.languages.python.test_runner import execute_test_subprocess
from codeflash.models.models import TestType
from codeflash.models.test_type import TestType

blocklisted_plugins = ["benchmark", "codspeed", "xdist", "sugar"]

Expand Down Expand Up @@ -1110,7 +1111,7 @@ def run_behavioral_tests(
common_pytest_args.append(f"--timeout={timeout}")

result_file_path = get_run_tmp_file(Path("pytest_results.xml"))
result_args = [f"--junitxml={result_file_path.as_posix()}", "-o", "junit_logging=all"]
result_args = [f"--junitxml={result_file_path}", "-o", "junit_logging=all"]

pytest_test_env = test_env.copy()
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
Expand All @@ -1137,14 +1138,7 @@ def run_behavioral_tests(
shlex.split(f"{SAFE_SYS_EXECUTABLE} -m coverage erase"), cwd=cwd, env=pytest_test_env, timeout=30
)
logger.debug(cov_erase)
coverage_cmd = [
SAFE_SYS_EXECUTABLE,
"-m",
"coverage",
"run",
f"--rcfile={coverage_config_file.as_posix()}",
"-m",
]
coverage_cmd = [SAFE_SYS_EXECUTABLE, "-m", "coverage", "run", f"--rcfile={coverage_config_file}", "-m"]
coverage_cmd.extend(self.pytest_cmd_tokens(IS_POSIX))

blocklist_args = [f"-p no:{plugin}" for plugin in blocklisted_plugins if plugin != "cov"]
Expand Down Expand Up @@ -1201,7 +1195,7 @@ def run_benchmarking_tests(
pytest_args.append(f"--timeout={timeout}")

result_file_path = get_run_tmp_file(Path("pytest_results.xml"))
result_args = [f"--junitxml={result_file_path.as_posix()}", "-o", "junit_logging=all"]
result_args = [f"--junitxml={result_file_path}", "-o", "junit_logging=all"]
pytest_test_env = test_env.copy()
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
blocklist_args = [f"-p no:{plugin}" for plugin in blocklisted_plugins]
Expand Down Expand Up @@ -1243,7 +1237,7 @@ def run_line_profile_tests(
if timeout is not None:
pytest_args.append(f"--timeout={timeout}")
result_file_path = get_run_tmp_file(Path("pytest_results.xml"))
result_args = [f"--junitxml={result_file_path.as_posix()}", "-o", "junit_logging=all"]
result_args = [f"--junitxml={result_file_path}", "-o", "junit_logging=all"]
pytest_test_env = test_env.copy()
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
blocklist_args = [f"-p no:{plugin}" for plugin in blocklisted_plugins]
Expand All @@ -1258,7 +1252,7 @@ def run_line_profile_tests(

def generate_concolic_tests(
self, test_cfg: Any, project_root: Path, function_to_optimize: FunctionToOptimize, function_to_optimize_ast: Any
) -> tuple[dict, str]:
) -> tuple[dict[str, Any], str]:
import ast
import importlib.util
import subprocess
Expand All @@ -1281,7 +1275,7 @@ def generate_concolic_tests(
crosshair_available = importlib.util.find_spec("crosshair") is not None

start_time = time.perf_counter()
function_to_concolic_tests: dict = {}
function_to_concolic_tests: dict[str, Any] = {}
concolic_test_suite_code = ""

if not crosshair_available:
Expand Down
8 changes: 3 additions & 5 deletions codeflash/languages/python/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from codeflash.cli_cmds.console import logger
from codeflash.code_utils.code_utils import custom_addopts
from codeflash.code_utils.shell_utils import get_cross_platform_subprocess_run_args
from codeflash.languages.registry import get_language_support

# Pattern to extract timing from stdout markers: !######...:<duration_ns>######!
Expand Down Expand Up @@ -92,11 +91,10 @@ def _ensure_runtime_files(project_root: Path, language: str = "javascript") -> N

def execute_test_subprocess(
cmd_list: list[str], cwd: Path, env: dict[str, str] | None, timeout: int = 600
) -> subprocess.CompletedProcess:
) -> subprocess.CompletedProcess[str]:
"""Execute a subprocess with the given command list, working directory, environment variables, and timeout."""
logger.debug(f"executing test run with command: {' '.join(cmd_list)}")
with custom_addopts():
run_args = get_cross_platform_subprocess_run_args(
cwd=cwd, env=env, timeout=timeout, check=False, text=True, capture_output=True
return subprocess.run(
cmd_list, cwd=cwd, env=env, timeout=timeout, check=False, text=True, capture_output=True, close_fds=False
)
return subprocess.run(cmd_list, **run_args) # noqa: PLW1510
Loading