Skip to content
Open
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
2 changes: 1 addition & 1 deletion codeflash/api/aiservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
if TYPE_CHECKING:
from pathlib import Path

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.models.ExperimentMetadata import ExperimentMetadata
from codeflash.models.models import (
AIServiceAdaptiveOptimizeRequest,
AIServiceCodeRepairRequest,
AIServiceRefinerRequest,
)
from codeflash.result.explanation import Explanation
from codeflash_core.models import FunctionToOptimize


class AiServiceClient:
Expand Down
4 changes: 2 additions & 2 deletions codeflash/benchmarking/function_ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

from codeflash.cli_cmds.console import logger
from codeflash.code_utils.config_consts import DEFAULT_IMPORTANCE_THRESHOLD
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.tracing.profile_stats import ProfileStats
from codeflash_core.models import FunctionToOptimize

if TYPE_CHECKING:
from pathlib import Path

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.java.jfr_parser import JfrProfile
from codeflash_core.models import FunctionToOptimize

pytest_patterns = {
"<frozen", # Frozen modules like runpy
Expand Down
2 changes: 1 addition & 1 deletion codeflash/benchmarking/instrument_codeflash_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from libcst import BaseStatement, ClassDef, FlattenSentinel, FunctionDef, RemovalSentinel

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash_core.models import FunctionToOptimize


class AddDecoratorTransformer(cst.CSTTransformer):
Expand Down
2 changes: 1 addition & 1 deletion codeflash/cli_cmds/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@

from rich.progress import TaskID

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.base import DependencyResolver, IndexResult
from codeflash.lsp.lsp_message import LspMessage
from codeflash.models.models import TestResults
from codeflash_core.models import FunctionToOptimize

DEBUG_MODE = logging.getLogger().getEffectiveLevel() == logging.DEBUG

Expand Down
2 changes: 1 addition & 1 deletion codeflash/code_utils/instrument_existing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from codeflash.cli_cmds.console import logger
from codeflash.code_utils.code_utils import get_run_tmp_file, module_name_from_file_path
from codeflash.code_utils.formatter import sort_imports
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.models.models import FunctionParent, TestingMode, VerificationType
from codeflash_core.models import FunctionToOptimize

if TYPE_CHECKING:
from collections.abc import Iterable
Expand Down
12 changes: 5 additions & 7 deletions codeflash/discovery/discover_unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import TYPE_CHECKING, Callable, Optional, final

if TYPE_CHECKING:
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash_core.models import FunctionToOptimize
from pydantic.dataclasses import dataclass
from rich.panel import Panel
from rich.text import Text
Expand All @@ -33,7 +33,7 @@
from codeflash.models.models import CodePosition, FunctionCalledInTest, TestsInFile, TestType

if TYPE_CHECKING:
from codeflash.verification.verification_utils import TestConfig
from codeflash_core.config import TestConfig


def existing_unit_test_count(
Expand Down Expand Up @@ -638,9 +638,7 @@ def discover_tests_for_language(
for func in funcs:
all_functions.append(func)
# Map simple qualified_name to full qualified_name_with_modules_from_root
simple_to_full_name[func.qualified_name] = func.qualified_name_with_modules_from_root(
cfg.project_root_path
)
simple_to_full_name[func.qualified_name] = func.qualified_name_with_modules_from_root(cfg.project_root)

# Use language support to discover tests
test_map = lang_support.discover_tests(cfg.tests_root, all_functions)
Expand Down Expand Up @@ -714,7 +712,7 @@ def discover_tests_pytest(
functions_to_optimize: list[FunctionToOptimize] | None = None,
) -> tuple[dict[str, set[FunctionCalledInTest]], int, int]:
tests_root = cfg.tests_root
project_root = cfg.project_root_path
project_root = cfg.project_root

tmp_pickle_path = get_run_tmp_file("collected_tests.pkl")
with custom_addopts():
Expand Down Expand Up @@ -863,7 +861,7 @@ def process_test_files(
) -> tuple[dict[str, set[FunctionCalledInTest]], int, int]:
import jedi

project_root_path = cfg.project_root_path
project_root_path = cfg.project_root
test_framework = cfg.test_framework

if functions_to_optimize:
Expand Down
7 changes: 2 additions & 5 deletions codeflash/discovery/functions_to_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@
from codeflash.languages.language_enum import Language
from codeflash.languages.registry import get_language_support, get_supported_extensions, is_language_supported
from codeflash.lsp.helpers import is_LSP_enabled
from codeflash.models.function_types import FunctionParent, FunctionToOptimize
from codeflash.telemetry.posthog_cf import ph

# Re-export for backward compatibility
__all__ = ["FunctionParent", "FunctionToOptimize"]

if TYPE_CHECKING:
from argparse import Namespace

from codeflash.models.models import CodeOptimizationContext
from codeflash.verification.verification_utils import TestConfig
from codeflash_core.config import TestConfig
from codeflash_core.models import FunctionToOptimize


@dataclass(frozen=True)
Expand Down
5 changes: 0 additions & 5 deletions codeflash/languages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@

# Lazy imports to avoid circular imports
def __getattr__(name: str):
if name == "FunctionInfo":
from codeflash.discovery.functions_to_optimize import FunctionToOptimize

return FunctionToOptimize
if name == "JavaScriptSupport":
from codeflash.languages.javascript.support import JavaScriptSupport

Expand All @@ -90,7 +86,6 @@ def __getattr__(name: str):
__all__ = [
"CodeContext",
"DependencyResolver",
"FunctionInfo",
"HelperFunction",
"IndexResult",
"Language",
Expand Down
46 changes: 6 additions & 40 deletions codeflash/languages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,20 @@
from collections.abc import Callable, Iterable, Sequence
from pathlib import Path

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.models.call_graph import CallGraph
from codeflash.models.models import FunctionSource, GeneratedTestsList, InvocationId, ValidCode
from codeflash.verification.verification_utils import TestConfig
from codeflash_core.config import TestConfig
from codeflash_core.models import FunctionToOptimize

from codeflash.languages.language_enum import Language
from codeflash.models.function_types import FunctionParent
from codeflash_core.models import FunctionParent, HelperFunction

__all__ = ["FunctionParent", "HelperFunction"]

# Backward compatibility aliases - ParentInfo is now FunctionParent
ParentInfo = FunctionParent


# Lazy import for FunctionInfo to avoid circular imports
# This allows `from codeflash.languages.base import FunctionInfo` to work at runtime
def __getattr__(name: str) -> Any:
if name == "FunctionInfo":
from codeflash.discovery.functions_to_optimize import FunctionToOptimize

return FunctionToOptimize
msg = f"module {__name__!r} has no attribute {name!r}"
raise AttributeError(msg)


@dataclass(frozen=True)
class IndexResult:
file_path: Path
Expand All @@ -50,31 +41,6 @@ class IndexResult:
error: bool


@dataclass
class HelperFunction:
"""A helper function that is a dependency of the target function.

Helper functions are functions called by the target function that are
within the same module/project (not external libraries).

Attributes:
name: The simple function name.
qualified_name: Full qualified name including parent scopes.
file_path: Path to the file containing the helper.
source_code: The source code of the helper function.
start_line: Starting line number.
end_line: Ending line number.

"""

name: str
qualified_name: str
file_path: Path
source_code: str
start_line: int
end_line: int


@dataclass
class CodeContext:
"""Code context extracted for optimization.
Expand Down Expand Up @@ -282,7 +248,7 @@ class PythonSupport(LanguageSupport):
def language(self) -> Language:
return Language.PYTHON

def discover_functions(self, source: str, file_path: Path, ...) -> list[FunctionInfo]:
def discover_functions(self, source: str, file_path: Path, ...) -> list[FunctionToOptimize]:
# Python-specific implementation using LibCST
...

Expand Down
2 changes: 1 addition & 1 deletion codeflash/languages/code_replacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
if TYPE_CHECKING:
from pathlib import Path

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.base import LanguageSupport
from codeflash.models.models import CodeStringsMarkdown
from codeflash_core.models import FunctionToOptimize

# Permissive criteria for discovering functions in code snippets (no export/return filtering)
_SOURCE_CRITERIA = FunctionFilterCriteria(require_return=False, require_export=False)
Expand Down
7 changes: 3 additions & 4 deletions codeflash/languages/function_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@
from argparse import Namespace
from typing import Any

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.either import Result
from codeflash.languages.base import DependencyResolver
from codeflash.models.function_types import FunctionParent
from codeflash.models.models import (
BenchmarkKey,
CodeOptimizationContext,
Expand All @@ -130,7 +128,8 @@
TestDiff,
TestFileReview,
)
from codeflash.verification.verification_utils import TestConfig
from codeflash_core.config import TestConfig
from codeflash_core.models import FunctionParent, FunctionToOptimize


def log_optimization_context(function_name: str, code_context: CodeOptimizationContext) -> None:
Expand Down Expand Up @@ -475,7 +474,7 @@ def __init__(
call_graph: DependencyResolver | None = None,
effort_override: str | None = None,
) -> None:
self.project_root = test_cfg.project_root_path.resolve()
self.project_root = test_cfg.project_root.resolve()
self.test_cfg = test_cfg
self.aiservice_client = aiservice_client if aiservice_client else AiServiceClient()
resolved_file_path = function_to_optimize.file_path.resolve()
Expand Down
7 changes: 4 additions & 3 deletions codeflash/languages/java/concurrency_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
if TYPE_CHECKING:
from pathlib import Path

from codeflash.languages.base import FunctionInfo
from codeflash_core.models import FunctionToOptimize


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -131,7 +132,7 @@ def __init__(self, analyzer=None) -> None:
"""
self.analyzer = analyzer

def analyze_function(self, func: FunctionInfo, source: str | None = None) -> ConcurrencyInfo:
def analyze_function(self, func: FunctionToOptimize, source: str | None = None) -> ConcurrencyInfo:
"""Analyze a function for concurrency patterns.

Args:
Expand Down Expand Up @@ -305,7 +306,7 @@ def get_optimization_suggestions(concurrency_info: ConcurrencyInfo) -> list[str]
return suggestions


def analyze_function_concurrency(func: FunctionInfo, source: str | None = None, analyzer=None) -> ConcurrencyInfo:
def analyze_function_concurrency(func: FunctionToOptimize, source: str | None = None, analyzer=None) -> ConcurrencyInfo:
"""Analyze a function for concurrency patterns.

Convenience function that creates a JavaConcurrencyAnalyzer and analyzes the function.
Expand Down
2 changes: 1 addition & 1 deletion codeflash/languages/java/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

from tree_sitter import Node

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.java.parser import JavaAnalyzer, JavaMethodNode
from codeflash_core.models import FunctionToOptimize

logger = logging.getLogger(__name__)

Expand Down
3 changes: 1 addition & 2 deletions codeflash/languages/java/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
from pathlib import Path
from typing import TYPE_CHECKING

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.base import FunctionFilterCriteria
from codeflash.languages.java.parser import get_java_analyzer
from codeflash.models.function_types import FunctionParent
from codeflash_core.models import FunctionParent, FunctionToOptimize

if TYPE_CHECKING:
from tree_sitter import Node
Expand Down
2 changes: 1 addition & 1 deletion codeflash/languages/java/instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from pathlib import Path
from typing import Any

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.java.parser import JavaAnalyzer
from codeflash_core.models import FunctionToOptimize

_WORD_RE = re.compile(r"^\w+$")

Expand Down
11 changes: 7 additions & 4 deletions codeflash/languages/java/line_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
if TYPE_CHECKING:
from tree_sitter import Node

from codeflash.languages.base import FunctionInfo
from codeflash_core.models import FunctionToOptimize


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -74,7 +75,7 @@ def __init__(self, output_file: Path, warmup_iterations: int = DEFAULT_WARMUP_IT
# === Agent-based profiling (bytecode instrumentation) ===

def generate_agent_config(
self, source: str, file_path: Path, functions: list[FunctionInfo], config_output_path: Path
self, source: str, file_path: Path, functions: list[FunctionToOptimize], config_output_path: Path
) -> Path:
"""Generate config JSON for the profiler agent.

Expand Down Expand Up @@ -141,7 +142,7 @@ def build_javaagent_arg(self, config_path: Path) -> str:
# === Source-level instrumentation ===

def instrument_source(
self, source: str, file_path: Path, functions: list[FunctionInfo], analyzer: Any = None
self, source: str, file_path: Path, functions: list[FunctionToOptimize], analyzer: Any = None
) -> str:
"""Instrument Java source code with line profiling.

Expand Down Expand Up @@ -326,7 +327,9 @@ class {self.profiler_class} {{
}}
"""

def instrument_function(self, func: FunctionInfo, lines: list[str], file_path: Path, analyzer: Any) -> list[str]:
def instrument_function(
self, func: FunctionToOptimize, lines: list[str], file_path: Path, analyzer: Any
) -> list[str]:
"""Instrument a single function with line profiling.

Args:
Expand Down
2 changes: 1 addition & 1 deletion codeflash/languages/java/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from pathlib import Path

from codeflash.models.models import TestFiles
from codeflash.verification.verification_utils import TestConfig
from codeflash_core.config import TestConfig

start_pattern = re.compile(r"!\$######([^:]*):([^:]*):([^:]*):([^:]*):([^:]+)######\$!")
end_pattern = re.compile(r"!######([^:]*):([^:]*):([^:]*):([^:]*):([^:]+):([^:]+)######!")
Expand Down
2 changes: 1 addition & 1 deletion codeflash/languages/java/remove_asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
if TYPE_CHECKING:
from tree_sitter import Node

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.java.parser import JavaAnalyzer
from codeflash_core.models import FunctionToOptimize

_ASSIGN_RE = re.compile(r"(\w+(?:<[^>]+>)?)\s+(\w+)\s*=\s*$")

Expand Down
2 changes: 1 addition & 1 deletion codeflash/languages/java/replacement.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any

from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.languages.java.parser import get_java_analyzer
from codeflash_core.models import FunctionToOptimize

if TYPE_CHECKING:
from codeflash.languages.java.parser import JavaAnalyzer
Expand Down
Loading
Loading