Merge pull request #1854 from codeflash-ai/cleanup/remove-dead-code
remove dead code
This commit is contained in:
commit
d6c8a0a5f7
20 changed files with 27 additions and 566 deletions
|
|
@ -221,73 +221,6 @@ class AiServiceClient:
|
|||
console.rule()
|
||||
return []
|
||||
|
||||
# Backward-compatible alias
|
||||
def optimize_python_code(
|
||||
self,
|
||||
source_code: str,
|
||||
dependency_code: str,
|
||||
trace_id: str,
|
||||
experiment_metadata: ExperimentMetadata | None = None,
|
||||
*,
|
||||
is_async: bool = False,
|
||||
n_candidates: int = 5,
|
||||
) -> list[OptimizedCandidate]:
|
||||
"""Backward-compatible alias for optimize_code() with language='python'."""
|
||||
return self.optimize_code(
|
||||
source_code=source_code,
|
||||
dependency_code=dependency_code,
|
||||
trace_id=trace_id,
|
||||
experiment_metadata=experiment_metadata,
|
||||
language="python",
|
||||
is_async=is_async,
|
||||
n_candidates=n_candidates,
|
||||
)
|
||||
|
||||
def get_jit_rewritten_code(self, source_code: str, trace_id: str) -> list[OptimizedCandidate]:
|
||||
"""Rewrite the given python code for performance via jit compilation by making a request to the Django endpoint.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
- source_code (str): The python code to optimize.
|
||||
- trace_id (str): Trace id of optimization run
|
||||
|
||||
Returns
|
||||
-------
|
||||
- List[OptimizationCandidate]: A list of Optimization Candidates.
|
||||
|
||||
"""
|
||||
start_time = time.perf_counter()
|
||||
git_repo_owner, git_repo_name = safe_get_repo_owner_and_name()
|
||||
|
||||
payload = {
|
||||
"source_code": source_code,
|
||||
"trace_id": trace_id,
|
||||
"dependency_code": "", # dummy value to please the api endpoint
|
||||
"python_version": platform.python_version(), # backward compat
|
||||
"current_username": get_last_commit_author_if_pr_exists(None),
|
||||
"repo_owner": git_repo_owner,
|
||||
"repo_name": git_repo_name,
|
||||
}
|
||||
|
||||
logger.info("!lsp|Rewriting as a JIT function…")
|
||||
console.rule()
|
||||
try:
|
||||
response = self.make_ai_service_request("/rewrite_jit", payload=payload, timeout=self.timeout)
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.exception(f"Error generating jit rewritten candidate: {e}")
|
||||
ph("cli-jit-rewrite-error-caught", {"error": str(e)})
|
||||
return []
|
||||
|
||||
if response.status_code == 200:
|
||||
optimizations_json = response.json()["optimizations"]
|
||||
console.rule()
|
||||
end_time = time.perf_counter()
|
||||
logger.debug(f"!lsp|Generating jit rewritten code took {end_time - start_time:.2f} seconds.")
|
||||
return self._get_valid_candidates(optimizations_json, OptimizedCandidateSource.JIT_REWRITE)
|
||||
self.log_error_response(response, "generating jit rewritten candidate", "cli-jit-rewrite-error-response")
|
||||
console.rule()
|
||||
return []
|
||||
|
||||
def optimize_python_code_line_profiler(
|
||||
self,
|
||||
source_code: str,
|
||||
|
|
@ -441,20 +374,7 @@ class AiServiceClient:
|
|||
console.rule()
|
||||
return []
|
||||
|
||||
# Alias for backward compatibility
|
||||
optimize_python_code_refinement = optimize_code_refinement
|
||||
|
||||
def code_repair(self, request: AIServiceCodeRepairRequest) -> OptimizedCandidate | None:
|
||||
"""Repair the optimization candidate that is not matching the test result of the original code.
|
||||
|
||||
Args:
|
||||
request: candidate details for repair
|
||||
|
||||
Returns:
|
||||
-------
|
||||
- OptimizedCandidate: new fixed candidate.
|
||||
|
||||
"""
|
||||
console.rule()
|
||||
try:
|
||||
payload = {
|
||||
|
|
|
|||
|
|
@ -1,258 +0,0 @@
|
|||
"""Language-agnostic schemas for AI service communication.
|
||||
|
||||
This module defines standardized payload schemas that work across all supported
|
||||
languages (Python, JavaScript, TypeScript, and future languages).
|
||||
|
||||
Design principles:
|
||||
1. General fields that apply to any language
|
||||
2. Language-specific fields grouped in a nested object
|
||||
3. Backward compatible with existing backend
|
||||
4. Extensible for future languages without breaking changes
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import platform
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
_PLATFORM_PYTHON_VERSION = platform.python_version()
|
||||
|
||||
|
||||
class ModuleSystem(str, Enum):
|
||||
"""Module system used by the code."""
|
||||
|
||||
COMMONJS = "commonjs" # JavaScript/Node.js require/exports
|
||||
ESM = "esm" # ES Modules import/export
|
||||
PYTHON = "python" # Python import system
|
||||
UNKNOWN = "unknown"
|
||||
|
||||
|
||||
class TestFramework(str, Enum):
|
||||
"""Supported test frameworks."""
|
||||
|
||||
# Python
|
||||
PYTEST = "pytest"
|
||||
UNITTEST = "unittest"
|
||||
|
||||
# JavaScript/TypeScript
|
||||
JEST = "jest"
|
||||
MOCHA = "mocha"
|
||||
VITEST = "vitest"
|
||||
|
||||
|
||||
@dataclass
|
||||
class LanguageInfo:
|
||||
"""Language-specific information.
|
||||
|
||||
General fields that describe the programming language and its environment.
|
||||
This is designed to be extensible for future languages.
|
||||
"""
|
||||
|
||||
# Core language identifier
|
||||
name: str # "python", "javascript", "typescript", "rust", etc.
|
||||
|
||||
# Language version (format varies by language)
|
||||
# - Python: "3.11.0"
|
||||
# - JavaScript/TypeScript: "ES2022", "ES2023"
|
||||
# - Rust: "1.70.0"
|
||||
version: str | None = None
|
||||
|
||||
# Module system (primarily for JS/TS, but could apply to others)
|
||||
module_system: ModuleSystem = ModuleSystem.UNKNOWN
|
||||
|
||||
# File extension (for generated files)
|
||||
# - Python: ".py"
|
||||
# - JavaScript: ".js", ".mjs", ".cjs"
|
||||
# - TypeScript: ".ts", ".mts", ".cts"
|
||||
file_extension: str = ""
|
||||
|
||||
# Type system info (for typed languages)
|
||||
has_type_annotations: bool = False
|
||||
type_checker: str | None = None # "mypy", "typescript", "pyright", etc.
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestInfo:
|
||||
"""Test-related information."""
|
||||
|
||||
# Test framework being used
|
||||
framework: TestFramework
|
||||
|
||||
# Timeout for test execution (seconds)
|
||||
timeout: int = 60
|
||||
|
||||
# Test file path patterns (for discovery)
|
||||
test_patterns: list[str] = field(default_factory=list)
|
||||
|
||||
# Path to test files relative to project root
|
||||
tests_root: str = "tests"
|
||||
|
||||
|
||||
@dataclass
|
||||
class OptimizeRequest:
|
||||
"""Request payload for code optimization.
|
||||
|
||||
This schema is designed to be language-agnostic while supporting
|
||||
language-specific fields through the `language_info` object.
|
||||
"""
|
||||
|
||||
# === Core required fields ===
|
||||
source_code: str # Code to optimize
|
||||
trace_id: str # Unique identifier for this optimization run
|
||||
|
||||
# === Language information ===
|
||||
language_info: LanguageInfo
|
||||
|
||||
# === Optional context ===
|
||||
dependency_code: str = "" # Read-only context code
|
||||
module_path: str = "" # Path to the module being optimized
|
||||
|
||||
# === Function metadata ===
|
||||
is_async: bool = False # Whether function is async/await
|
||||
is_numerical_code: bool | None = None # Whether code does numerical computation
|
||||
|
||||
# === Generation parameters ===
|
||||
n_candidates: int = 5 # Number of optimization candidates
|
||||
|
||||
# === Metadata ===
|
||||
codeflash_version: str = ""
|
||||
experiment_metadata: dict[str, Any] | None = None
|
||||
repo_owner: str | None = None
|
||||
repo_name: str | None = None
|
||||
current_username: str | None = None
|
||||
|
||||
def to_payload(self) -> dict[str, Any]:
|
||||
"""Convert to API payload dict, maintaining backward compatibility."""
|
||||
# Cache frequently accessed attribute
|
||||
lang = self.language_info
|
||||
|
||||
# Build payload in one shot using local references to minimize attribute lookups.
|
||||
# Add language version (canonical for all languages)
|
||||
# Backward compat: backend still expects python_version
|
||||
payload = {
|
||||
"source_code": self.source_code,
|
||||
"trace_id": self.trace_id,
|
||||
"language": lang.name,
|
||||
"dependency_code": self.dependency_code,
|
||||
"is_async": self.is_async,
|
||||
"n_candidates": self.n_candidates,
|
||||
"codeflash_version": self.codeflash_version,
|
||||
"experiment_metadata": self.experiment_metadata,
|
||||
"repo_owner": self.repo_owner,
|
||||
"repo_name": self.repo_name,
|
||||
"current_username": self.current_username,
|
||||
"is_numerical_code": self.is_numerical_code,
|
||||
"language_version": lang.version,
|
||||
"python_version": (lang.version if lang.name == "python" else platform.python_version()),
|
||||
}
|
||||
|
||||
# Module system for JS/TS
|
||||
if lang.module_system != ModuleSystem.UNKNOWN:
|
||||
payload["module_system"] = lang.module_system.value
|
||||
|
||||
return payload
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestGenRequest:
|
||||
"""Request payload for test generation.
|
||||
|
||||
This schema is designed to be language-agnostic while supporting
|
||||
language-specific fields through the `language_info` and `test_info` objects.
|
||||
"""
|
||||
|
||||
# === Core required fields ===
|
||||
source_code: str # Code being tested
|
||||
function_name: str # Name of function to generate tests for
|
||||
trace_id: str # Unique identifier
|
||||
|
||||
# === Language information ===
|
||||
language_info: LanguageInfo
|
||||
|
||||
# === Test information ===
|
||||
test_info: TestInfo
|
||||
|
||||
# === Path information ===
|
||||
module_path: str = "" # Path to source module
|
||||
test_module_path: str = "" # Path for generated test
|
||||
|
||||
# === Function metadata ===
|
||||
helper_function_names: list[str] = field(default_factory=list)
|
||||
is_async: bool = False
|
||||
is_numerical_code: bool | None = None
|
||||
|
||||
# === Generation parameters ===
|
||||
test_index: int = 0 # Index when generating multiple tests
|
||||
|
||||
# === Metadata ===
|
||||
codeflash_version: str = ""
|
||||
|
||||
def to_payload(self) -> dict[str, Any]:
|
||||
"""Convert to API payload dict, maintaining backward compatibility."""
|
||||
# Backward compat: backend still expects python_version
|
||||
python_version = self.language_info.version if self.language_info.name == "python" else _PLATFORM_PYTHON_VERSION
|
||||
|
||||
payload = {
|
||||
"source_code_being_tested": self.source_code,
|
||||
"function_to_optimize": {"function_name": self.function_name, "is_async": self.is_async},
|
||||
"helper_function_names": self.helper_function_names,
|
||||
"module_path": self.module_path,
|
||||
"test_module_path": self.test_module_path,
|
||||
"test_framework": self.test_info.framework.value,
|
||||
"test_timeout": self.test_info.timeout,
|
||||
"trace_id": self.trace_id,
|
||||
"test_index": self.test_index,
|
||||
"language": self.language_info.name,
|
||||
"codeflash_version": self.codeflash_version,
|
||||
"is_async": self.is_async,
|
||||
"is_numerical_code": self.is_numerical_code,
|
||||
"language_version": self.language_info.version,
|
||||
"python_version": python_version,
|
||||
}
|
||||
|
||||
# Module system for JS/TS
|
||||
if self.language_info.module_system != ModuleSystem.UNKNOWN:
|
||||
payload["module_system"] = self.language_info.module_system.value
|
||||
|
||||
return payload
|
||||
|
||||
|
||||
# === Helper functions to create language info ===
|
||||
|
||||
|
||||
def python_language_info(version: str | None = None) -> LanguageInfo:
|
||||
"""Create LanguageInfo for Python."""
|
||||
import platform
|
||||
|
||||
return LanguageInfo(
|
||||
name="python",
|
||||
version=version or platform.python_version(),
|
||||
module_system=ModuleSystem.PYTHON,
|
||||
file_extension=".py",
|
||||
has_type_annotations=True,
|
||||
type_checker="mypy",
|
||||
)
|
||||
|
||||
|
||||
def javascript_language_info(
|
||||
module_system: ModuleSystem = ModuleSystem.COMMONJS, version: str = "ES2022"
|
||||
) -> LanguageInfo:
|
||||
"""Create LanguageInfo for JavaScript."""
|
||||
ext = ".mjs" if module_system == ModuleSystem.ESM else ".js"
|
||||
return LanguageInfo(
|
||||
name="javascript", version=version, module_system=module_system, file_extension=ext, has_type_annotations=False
|
||||
)
|
||||
|
||||
|
||||
def typescript_language_info(module_system: ModuleSystem = ModuleSystem.ESM, version: str = "ES2022") -> LanguageInfo:
|
||||
"""Create LanguageInfo for TypeScript."""
|
||||
return LanguageInfo(
|
||||
name="typescript",
|
||||
version=version,
|
||||
module_system=module_system,
|
||||
file_extension=".ts",
|
||||
has_type_annotations=True,
|
||||
type_checker="typescript",
|
||||
)
|
||||
|
|
@ -6,7 +6,7 @@ from functools import lru_cache
|
|||
from pathlib import Path
|
||||
|
||||
from codeflash.cli_cmds import logging_config
|
||||
from codeflash.cli_cmds.console import logger
|
||||
from codeflash.cli_cmds.console import apologize_and_exit, logger
|
||||
from codeflash.code_utils import env_utils
|
||||
from codeflash.code_utils.code_utils import exit_with_message, normalize_ignore_paths
|
||||
from codeflash.code_utils.config_parser import parse_config_file
|
||||
|
|
@ -234,8 +234,6 @@ def handle_optimize_all_arg_parsing(args: Namespace) -> Namespace:
|
|||
f"I couldn't find a git repository in the current directory. "
|
||||
f"I need a git repository to run {mode} and open PRs for optimizations. Exiting..."
|
||||
)
|
||||
from codeflash.cli_cmds.cli_common import apologize_and_exit
|
||||
|
||||
apologize_and_exit()
|
||||
git_remote = getattr(args, "git_remote", None)
|
||||
if not check_and_push_branch(git_repo, git_remote=git_remote):
|
||||
|
|
|
|||
|
|
@ -1,97 +0,0 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
import sys
|
||||
from typing import Callable, NoReturn, cast
|
||||
|
||||
import click
|
||||
import inquirer
|
||||
|
||||
from codeflash.cli_cmds.console import console, logger
|
||||
|
||||
|
||||
def apologize_and_exit() -> NoReturn:
|
||||
console.rule()
|
||||
logger.info(
|
||||
"💡 If you're having trouble, see https://docs.codeflash.ai/getting-started/local-installation for further help getting started with Codeflash!"
|
||||
)
|
||||
console.rule()
|
||||
logger.info("👋 Exiting...")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def inquirer_wrapper(func: Callable[..., str | bool], *args: str | bool, **kwargs: str | bool) -> str | bool:
|
||||
new_args = []
|
||||
new_kwargs = {}
|
||||
|
||||
if len(args) == 1:
|
||||
message = str(args[0])
|
||||
else:
|
||||
message = str(kwargs["message"])
|
||||
new_kwargs = kwargs.copy()
|
||||
split_messages = split_string_to_cli_width(message, is_confirm=func == inquirer.confirm)
|
||||
for split_message in split_messages[:-1]:
|
||||
click.echo(split_message)
|
||||
|
||||
last_message = split_messages[-1]
|
||||
|
||||
if len(args) == 1:
|
||||
new_args.append(last_message)
|
||||
else:
|
||||
new_kwargs["message"] = last_message
|
||||
|
||||
return func(*new_args, **new_kwargs)
|
||||
|
||||
|
||||
def split_string_to_cli_width(string: str, is_confirm: bool = False) -> list[str]:
|
||||
cli_width, _ = shutil.get_terminal_size()
|
||||
# split string to lines that accommodate "[?] " prefix
|
||||
cli_width -= len("[?] ")
|
||||
lines = split_string_to_fit_width(string, cli_width)
|
||||
|
||||
# split last line to additionally accommodate ": " or " (y/N): " suffix
|
||||
cli_width -= len(" (y/N):") if is_confirm else len(": ")
|
||||
last_lines = split_string_to_fit_width(lines[-1], cli_width)
|
||||
|
||||
lines = lines[:-1] + last_lines
|
||||
|
||||
if len(lines) > 1:
|
||||
for i in range(len(lines[:-1])):
|
||||
# Add yellow color to question mark in "[?] " prefix
|
||||
lines[i] = "[\033[33m?\033[0m] " + lines[i]
|
||||
return lines
|
||||
|
||||
|
||||
def inquirer_wrapper_path(*args: str, **kwargs: str) -> dict[str, str] | None:
|
||||
new_args = []
|
||||
message = kwargs["message"]
|
||||
new_kwargs = kwargs.copy()
|
||||
split_messages = split_string_to_cli_width(message)
|
||||
for split_message in split_messages[:-1]:
|
||||
click.echo(split_message)
|
||||
|
||||
last_message = split_messages[-1]
|
||||
new_kwargs["message"] = last_message
|
||||
new_args.append(args[0])
|
||||
|
||||
return cast("dict[str, str]", inquirer.prompt([inquirer.Path(*new_args, **new_kwargs)]))
|
||||
|
||||
|
||||
def split_string_to_fit_width(string: str, width: int) -> list[str]:
|
||||
words = string.split()
|
||||
lines = []
|
||||
current_line = [words[0]]
|
||||
current_length = len(words[0])
|
||||
|
||||
for word in words[1:]:
|
||||
word_length = len(word)
|
||||
if current_length + word_length + 1 <= width:
|
||||
current_line.append(word)
|
||||
current_length += word_length + 1
|
||||
else:
|
||||
lines.append(" ".join(current_line))
|
||||
current_line = [word]
|
||||
current_length = word_length
|
||||
|
||||
lines.append(" ".join(current_line))
|
||||
return lines
|
||||
|
|
@ -15,8 +15,7 @@ from rich.panel import Panel
|
|||
from rich.table import Table
|
||||
from rich.text import Text
|
||||
|
||||
from codeflash.cli_cmds.cli_common import apologize_and_exit
|
||||
from codeflash.cli_cmds.console import console, logger
|
||||
from codeflash.cli_cmds.console import apologize_and_exit, console, logger
|
||||
from codeflash.cli_cmds.extension import install_vscode_extension
|
||||
from codeflash.cli_cmds.github_workflow import install_github_actions
|
||||
from codeflash.cli_cmds.init_auth import install_github_app, prompt_api_key
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import contextlib
|
||||
import logging
|
||||
import sys
|
||||
from collections import deque
|
||||
from contextlib import contextmanager
|
||||
from itertools import cycle
|
||||
|
|
@ -114,6 +115,16 @@ class DummyProgress:
|
|||
pass
|
||||
|
||||
|
||||
def apologize_and_exit() -> None:
|
||||
console.rule()
|
||||
logger.info(
|
||||
"💡 If you're having trouble, see https://docs.codeflash.ai/getting-started/local-installation for further help getting started with Codeflash!"
|
||||
)
|
||||
console.rule()
|
||||
logger.info("👋 Exiting...")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def lsp_log(message: LspMessage) -> None:
|
||||
if not is_LSP_enabled():
|
||||
return
|
||||
|
|
|
|||
|
|
@ -15,8 +15,7 @@ from rich.text import Text
|
|||
|
||||
from codeflash.api.aiservice import AiServiceClient
|
||||
from codeflash.api.cfapi import setup_github_actions
|
||||
from codeflash.cli_cmds.cli_common import apologize_and_exit
|
||||
from codeflash.cli_cmds.console import console, logger
|
||||
from codeflash.cli_cmds.console import apologize_and_exit, console, logger
|
||||
from codeflash.cli_cmds.init_config import CodeflashTheme
|
||||
from codeflash.code_utils.compat import LF
|
||||
from codeflash.code_utils.config_parser import parse_config_file
|
||||
|
|
|
|||
|
|
@ -9,8 +9,7 @@ from rich.panel import Panel
|
|||
from rich.text import Text
|
||||
|
||||
from codeflash.api.cfapi import get_user_id, is_github_app_installed_on_repo
|
||||
from codeflash.cli_cmds.cli_common import apologize_and_exit
|
||||
from codeflash.cli_cmds.console import console
|
||||
from codeflash.cli_cmds.console import apologize_and_exit, console
|
||||
from codeflash.cli_cmds.init_config import CodeflashTheme
|
||||
from codeflash.cli_cmds.oauth_handler import perform_oauth_signin
|
||||
from codeflash.code_utils.compat import LF
|
||||
|
|
|
|||
|
|
@ -12,8 +12,7 @@ import inquirer.themes
|
|||
import tomlkit
|
||||
from pydantic.dataclasses import dataclass
|
||||
|
||||
from codeflash.cli_cmds.cli_common import apologize_and_exit
|
||||
from codeflash.cli_cmds.console import console
|
||||
from codeflash.cli_cmds.console import apologize_and_exit, console
|
||||
from codeflash.code_utils.compat import LF
|
||||
from codeflash.code_utils.config_parser import parse_config_file
|
||||
from codeflash.code_utils.env_utils import check_formatter_installed
|
||||
|
|
|
|||
|
|
@ -19,8 +19,7 @@ from rich.panel import Panel
|
|||
from rich.table import Table
|
||||
from rich.text import Text
|
||||
|
||||
from codeflash.cli_cmds.cli_common import apologize_and_exit
|
||||
from codeflash.cli_cmds.console import console
|
||||
from codeflash.cli_cmds.console import apologize_and_exit, console
|
||||
from codeflash.code_utils.code_utils import validate_relative_directory_path
|
||||
from codeflash.code_utils.compat import LF
|
||||
from codeflash.code_utils.git_utils import get_git_remotes
|
||||
|
|
|
|||
|
|
@ -23,8 +23,7 @@ from rich.prompt import Confirm
|
|||
from rich.table import Table
|
||||
from rich.text import Text
|
||||
|
||||
from codeflash.cli_cmds.cli_common import apologize_and_exit
|
||||
from codeflash.cli_cmds.console import console
|
||||
from codeflash.cli_cmds.console import apologize_and_exit, console
|
||||
from codeflash.code_utils.code_utils import validate_relative_directory_path
|
||||
from codeflash.code_utils.compat import LF
|
||||
from codeflash.code_utils.git_utils import get_git_remotes
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
VERBOSE_LOGGING_FORMAT = "%(asctime)s [%(pathname)s:%(lineno)s in function %(funcName)s] %(message)s"
|
||||
LOGGING_FORMAT = "[%(levelname)s] %(message)s"
|
||||
BARE_LOGGING_FORMAT = "%(message)s"
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@ from __future__ import annotations
|
|||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
from codeflash.api.cfapi import is_github_app_installed_on_repo
|
||||
from codeflash.cli_cmds.cli_common import apologize_and_exit
|
||||
from codeflash.cli_cmds.console import paneled_text
|
||||
from codeflash.cli_cmds.console import apologize_and_exit, paneled_text
|
||||
from codeflash.code_utils.compat import LF
|
||||
from codeflash.code_utils.git_utils import get_repo_owner_and_name
|
||||
|
||||
|
|
|
|||
|
|
@ -8,44 +8,12 @@ The old CodeNormalizer ABC (deleted from base.py) is preserved below for referen
|
|||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from tree_sitter import Node
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Reference: the old CodeNormalizer ABC that was deleted from base.py.
|
||||
# Kept here so the interface contract is visible if we re-introduce a
|
||||
# normalizer hierarchy later.
|
||||
# ---------------------------------------------------------------------------
|
||||
class CodeNormalizer(ABC):
|
||||
@property
|
||||
@abstractmethod
|
||||
def language(self) -> str: ...
|
||||
|
||||
@abstractmethod
|
||||
def normalize(self, code: str) -> str: ...
|
||||
|
||||
@abstractmethod
|
||||
def normalize_for_hash(self, code: str) -> str: ...
|
||||
|
||||
def are_duplicates(self, code1: str, code2: str) -> bool:
|
||||
try:
|
||||
return self.normalize_for_hash(code1) == self.normalize_for_hash(code2)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def get_fingerprint(self, code: str) -> str:
|
||||
import hashlib
|
||||
|
||||
return hashlib.sha256(self.normalize_for_hash(code).encode()).hexdigest()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class JavaScriptVariableNormalizer:
|
||||
"""Normalizes JavaScript/TypeScript code for duplicate detection using tree-sitter.
|
||||
|
||||
|
|
|
|||
|
|
@ -1907,8 +1907,6 @@ def extract_calling_function_source(source_code: str, function_name: str, ref_li
|
|||
|
||||
"""
|
||||
try:
|
||||
from codeflash.languages.javascript.treesitter import TreeSitterAnalyzer, TreeSitterLanguage
|
||||
|
||||
# Try TypeScript first, fall back to JavaScript
|
||||
for lang in [TreeSitterLanguage.TYPESCRIPT, TreeSitterLanguage.TSX, TreeSitterLanguage.JAVASCRIPT]:
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -48,15 +48,15 @@ def _ensure_languages_registered() -> None:
|
|||
# Import support modules to trigger registration
|
||||
# These imports are deferred to avoid circular imports
|
||||
import contextlib
|
||||
import importlib
|
||||
|
||||
with contextlib.suppress(ImportError):
|
||||
from codeflash.languages.python import support as _
|
||||
|
||||
with contextlib.suppress(ImportError):
|
||||
from codeflash.languages.javascript import support as _
|
||||
|
||||
with contextlib.suppress(ImportError):
|
||||
from codeflash.languages.java import support as _ # noqa: F401
|
||||
for _lang_module in (
|
||||
"codeflash.languages.python.support",
|
||||
"codeflash.languages.javascript.support",
|
||||
"codeflash.languages.java.support",
|
||||
):
|
||||
with contextlib.suppress(ImportError):
|
||||
importlib.import_module(_lang_module)
|
||||
|
||||
_languages_registered = True
|
||||
|
||||
|
|
|
|||
|
|
@ -281,8 +281,6 @@ def get_code_block_splitter(file_path: Path | None) -> str:
|
|||
# Pattern to match markdown code blocks with optional language tag and file path
|
||||
# Matches: ```language:filepath\ncode\n``` or ```language\ncode\n```
|
||||
markdown_pattern = re.compile(r"```(\w+)(?::([^\n]+))?\n(.*?)\n```", re.DOTALL)
|
||||
# Legacy pattern for backward compatibility (only python)
|
||||
markdown_pattern_python_only = re.compile(r"```python:([^\n]+)\n(.*?)\n```", re.DOTALL)
|
||||
|
||||
|
||||
class CodeStringsMarkdown(BaseModel):
|
||||
|
|
@ -436,9 +434,6 @@ class TestFile(BaseModel):
|
|||
class TestFiles(BaseModel):
|
||||
test_files: list[TestFile]
|
||||
|
||||
def get_by_type(self, test_type: TestType) -> TestFiles:
|
||||
return TestFiles(test_files=[test_file for test_file in self.test_files if test_file.test_type == test_type])
|
||||
|
||||
def add(self, test_file: TestFile) -> None:
|
||||
if test_file not in self.test_files:
|
||||
self.test_files.append(test_file)
|
||||
|
|
@ -902,9 +897,6 @@ class TestResults(BaseModel): # noqa: PLW1641
|
|||
except (IndexError, KeyError):
|
||||
return None
|
||||
|
||||
def get_all_ids(self) -> set[InvocationId]:
|
||||
return {test_result.id for test_result in self.test_results}
|
||||
|
||||
def get_all_unique_invocation_loop_ids(self) -> set[str]:
|
||||
return {test_result.unique_invocation_loop_id for test_result in self.test_results}
|
||||
|
||||
|
|
|
|||
|
|
@ -192,44 +192,6 @@ def _write_package_json(project_root: Path, config: CodeflashConfig) -> tuple[bo
|
|||
return False, f"Failed to write package.json: {e}"
|
||||
|
||||
|
||||
def create_pyproject_toml(project_root: Path) -> tuple[bool, str]:
|
||||
"""Create a minimal pyproject.toml file.
|
||||
|
||||
Used when no pyproject.toml exists for a Python project.
|
||||
|
||||
Args:
|
||||
project_root: Project root directory.
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message).
|
||||
|
||||
"""
|
||||
pyproject_path = project_root / "pyproject.toml"
|
||||
|
||||
if pyproject_path.exists():
|
||||
return False, f"pyproject.toml already exists at {pyproject_path}"
|
||||
|
||||
try:
|
||||
doc = tomlkit.document()
|
||||
doc.add(tomlkit.comment("Created by Codeflash"))
|
||||
doc.add(tomlkit.nl())
|
||||
|
||||
# Add minimal [tool.codeflash] section
|
||||
tool_table = tomlkit.table()
|
||||
codeflash_table = tomlkit.table()
|
||||
codeflash_table.add(tomlkit.comment("Codeflash configuration - https://docs.codeflash.ai"))
|
||||
tool_table["codeflash"] = codeflash_table
|
||||
doc["tool"] = tool_table
|
||||
|
||||
with pyproject_path.open("w", encoding="utf8") as f:
|
||||
f.write(tomlkit.dumps(doc))
|
||||
|
||||
return True, f"Created {pyproject_path}"
|
||||
|
||||
except Exception as e:
|
||||
return False, f"Failed to create pyproject.toml: {e}"
|
||||
|
||||
|
||||
def remove_config(project_root: Path, language: str) -> tuple[bool, str]:
|
||||
"""Remove Codeflash config from native config file.
|
||||
|
||||
|
|
|
|||
|
|
@ -33,5 +33,4 @@ codeflash/languages/python/static_analysis/edit_generated_tests.py
|
|||
codeflash/cli_cmds/console_constants.py
|
||||
codeflash/cli_cmds/logging_config.py
|
||||
codeflash/cli_cmds/__init__.py
|
||||
codeflash/cli_cmds/cli_common.py
|
||||
codeflash/cli_cmds/cli.py
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ from codeflash.setup.config_schema import CodeflashConfig
|
|||
from codeflash.setup.config_writer import (
|
||||
_write_package_json,
|
||||
_write_pyproject_toml,
|
||||
create_pyproject_toml,
|
||||
remove_config,
|
||||
write_config,
|
||||
)
|
||||
|
|
@ -339,26 +338,3 @@ class TestRemoveConfig:
|
|||
data = json.load(f)
|
||||
assert data["name"] == "test" # Preserved
|
||||
assert "codeflash" not in data
|
||||
|
||||
|
||||
class TestCreatePyprojectToml:
|
||||
"""Tests for create_pyproject_toml function."""
|
||||
|
||||
def test_creates_minimal_pyproject(self, tmp_path):
|
||||
"""Should create minimal pyproject.toml."""
|
||||
success, message = create_pyproject_toml(tmp_path)
|
||||
|
||||
assert success is True
|
||||
assert (tmp_path / "pyproject.toml").exists()
|
||||
|
||||
content = (tmp_path / "pyproject.toml").read_text()
|
||||
assert "codeflash" in content.lower()
|
||||
|
||||
def test_fails_if_already_exists(self, tmp_path):
|
||||
"""Should fail if pyproject.toml already exists."""
|
||||
(tmp_path / "pyproject.toml").write_text('[project]\nname = "test"')
|
||||
|
||||
success, message = create_pyproject_toml(tmp_path)
|
||||
|
||||
assert success is False
|
||||
assert message == f"pyproject.toml already exists at {tmp_path / 'pyproject.toml'}"
|
||||
|
|
|
|||
Loading…
Reference in a new issue