Skip to content

Commit 989b1f3

Browse files
unit tests fixing
1 parent 07a9365 commit 989b1f3

File tree

9 files changed

+221
-152
lines changed

9 files changed

+221
-152
lines changed

codeflash/api/aiservice.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ def make_ai_service_request(
7373
url = f"{self.base_url}/ai{endpoint}"
7474
if method.upper() == "POST":
7575
json_payload = json.dumps(payload, indent=None, default=pydantic_encoder)
76-
print(f"========JSON PAYLOAD FOR {url}==============")
77-
print(f"Payload: {json_payload}")
78-
print("======================")
76+
logger.debug(f"========JSON PAYLOAD FOR {url}==============")
77+
logger.debug(json_payload)
78+
logger.debug("======================")
7979
headers = {**self.headers, "Content-Type": "application/json"}
8080
response = requests.post(url, data=json_payload, headers=headers, timeout=timeout)
8181
else:

codeflash/code_utils/code_replacer.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from pathlib import Path
2020

2121
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
22-
from codeflash.models.models import CodeOptimizationContext, OptimizedCandidate, ValidCode
22+
from codeflash.models.models import CodeOptimizationContext, CodeStringsMarkdown, OptimizedCandidate, ValidCode
2323

2424
ASTNodeT = TypeVar("ASTNodeT", bound=ast.AST)
2525

@@ -408,16 +408,17 @@ def replace_functions_and_add_imports(
408408

409409
def replace_function_definitions_in_module(
410410
function_names: list[str],
411-
optimized_code: str,
411+
optimized_code: CodeStringsMarkdown,
412412
module_abspath: Path,
413413
preexisting_objects: set[tuple[str, tuple[FunctionParent, ...]]],
414414
project_root_path: Path,
415415
) -> bool:
416416
source_code: str = module_abspath.read_text(encoding="utf8")
417+
code_to_apply = get_optimized_code_for_module(module_abspath.relative_to(project_root_path), optimized_code)
417418
new_code: str = replace_functions_and_add_imports(
418-
add_global_assignments(optimized_code, source_code),
419+
add_global_assignments(code_to_apply, source_code),
419420
function_names,
420-
optimized_code,
421+
code_to_apply,
421422
module_abspath,
422423
preexisting_objects,
423424
project_root_path,
@@ -428,6 +429,19 @@ def replace_function_definitions_in_module(
428429
return True
429430

430431

432+
def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStringsMarkdown) -> str:
433+
file_to_code_context = optimized_code.file_to_path()
434+
module_optimized_code = file_to_code_context.get(str(relative_path))
435+
if module_optimized_code is None:
436+
logger.warning(
437+
f"Optimized code not found for {relative_path} In the context\n-------\n{optimized_code}\n-------\n"
438+
"re-check your 'markdown code structure'"
439+
f"existing files are {file_to_code_context.keys()}"
440+
)
441+
module_optimized_code = ""
442+
return module_optimized_code
443+
444+
431445
def is_zero_diff(original_code: str, new_code: str) -> bool:
432446
return normalize_code(original_code) == normalize_code(new_code)
433447

codeflash/context/unused_definition_remover.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
import ast
44
from collections import defaultdict
55
from dataclasses import dataclass, field
6-
from typing import TYPE_CHECKING
7-
8-
if TYPE_CHECKING:
9-
from pathlib import Path
6+
from pathlib import Path
107
from typing import TYPE_CHECKING, Optional
118

129
import libcst as cst
1310

1411
from codeflash.cli_cmds.console import logger
1512
from codeflash.code_utils.code_replacer import replace_function_definitions_in_module
13+
from codeflash.models.models import CodeString, CodeStringsMarkdown
1614

1715
if TYPE_CHECKING:
1816
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
@@ -530,7 +528,11 @@ def revert_unused_helper_functions(
530528
helper_names = [helper.qualified_name for helper in helpers_in_file]
531529
reverted_code = replace_function_definitions_in_module(
532530
function_names=helper_names,
533-
optimized_code=original_code, # Use original code as the "optimized" code to revert
531+
optimized_code=CodeStringsMarkdown(
532+
code_strings=[
533+
CodeString(code=original_code, file_path=Path(file_path).relative_to(project_root))
534+
]
535+
), # Use original code as the "optimized" code to revert
534536
module_abspath=file_path,
535537
preexisting_objects=set(), # Empty set since we're reverting
536538
project_root_path=project_root,

codeflash/models/models.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ class CodeStringsMarkdown(BaseModel):
174174

175175
@property
176176
def flat(self) -> str:
177+
"""Returns the combined Python module from all code blocks.
178+
179+
Each block is prefixed by a file path comment to indicate its origin.
180+
This representation is syntactically valid Python code.
181+
182+
Returns:
183+
str: The concatenated code of all blocks with file path annotations.
184+
185+
"""
177186
if self._cache.get("flat") is not None:
178187
return self._cache["flat"]
179188
self._cache["flat"] = "\n".join(
@@ -183,7 +192,15 @@ def flat(self) -> str:
183192

184193
@property
185194
def markdown(self) -> str:
186-
"""Returns the markdown representation of the code, including the file path where possible."""
195+
"""Returns a Markdown-formatted string containing all code blocks.
196+
197+
Each block is enclosed in a triple-backtick code block with an optional
198+
file path suffix (e.g., ```python:filename.py).
199+
200+
Returns:
201+
str: Markdown representation of the code blocks.
202+
203+
"""
187204
return "\n".join(
188205
[
189206
f"```python{':' + str(code_string.file_path) if code_string.file_path else ''}\n{code_string.code.strip()}\n```"
@@ -192,6 +209,12 @@ def markdown(self) -> str:
192209
)
193210

194211
def file_to_path(self) -> dict[str, str]:
212+
"""Return a dictionary mapping file paths to their corresponding code blocks.
213+
214+
Returns:
215+
dict[str, str]: Mapping from file path (as string) to code.
216+
217+
"""
195218
if self._cache.get("file_to_path") is not None:
196219
return self._cache["file_to_path"]
197220
self._cache["file_to_path"] = {
@@ -201,6 +224,17 @@ def file_to_path(self) -> dict[str, str]:
201224

202225
@staticmethod
203226
def parse_markdown_code(markdown_code: str) -> CodeStringsMarkdown:
227+
"""Parse a Markdown string into a CodeStringsMarkdown object.
228+
229+
Extracts code blocks and their associated file paths and constructs a new CodeStringsMarkdown instance.
230+
231+
Args:
232+
markdown_code (str): The Markdown-formatted string to parse.
233+
234+
Returns:
235+
CodeStringsMarkdown: Parsed object containing code blocks.
236+
237+
"""
204238
matches = markdown_pattern.findall(markdown_code)
205239
results = CodeStringsMarkdown()
206240
for file_path, code in matches:

codeflash/optimization/function_optimizer.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -720,29 +720,13 @@ def replace_function_and_helpers_with_optimized_code(
720720
read_writable_functions_by_file_path[self.function_to_optimize.file_path].add(
721721
self.function_to_optimize.qualified_name
722722
)
723-
724-
file_to_code_context = optimized_code.file_to_path()
725-
726723
for helper_function in code_context.helper_functions:
727724
if helper_function.jedi_definition.type != "class":
728725
read_writable_functions_by_file_path[helper_function.file_path].add(helper_function.qualified_name)
729-
730726
for module_abspath, qualified_names in read_writable_functions_by_file_path.items():
731-
relative_module_path = str(module_abspath.relative_to(self.project_root))
732-
logger.debug(f"applying optimized code to: {relative_module_path}")
733-
734-
scoped_optimized_code = file_to_code_context.get(relative_module_path)
735-
if scoped_optimized_code is None:
736-
logger.warning(
737-
f"Optimized code not found for {relative_module_path} In the context\n-------\n{optimized_code}\n-------\n"
738-
"re-check your 'split markers'"
739-
f"existing files are {file_to_code_context.keys()}"
740-
)
741-
scoped_optimized_code = ""
742-
743727
did_update |= replace_function_definitions_in_module(
744728
function_names=list(qualified_names),
745-
optimized_code=scoped_optimized_code,
729+
optimized_code=optimized_code,
746730
module_abspath=module_abspath,
747731
preexisting_objects=code_context.preexisting_objects,
748732
project_root_path=self.project_root,

0 commit comments

Comments
 (0)