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
6 changes: 3 additions & 3 deletions codeflash/code_utils/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import subprocess
import tempfile
from pathlib import Path
from typing import Optional, Union
from typing import Any, Optional, Union

import isort

Expand Down Expand Up @@ -163,10 +163,10 @@ def format_code(
return formatted_code


def sort_imports(code: str, *, float_to_top: bool = False) -> str:
def sort_imports(code: str, **kwargs: Any) -> str: # noqa : ANN401
try:
# Deduplicate and sort imports, modify the code in memory, not on disk
sorted_code = isort.code(code=code, float_to_top=float_to_top)
sorted_code = isort.code(code, **kwargs)
except Exception: # this will also catch the FileSkipComment exception, use this fn everywhere
logger.exception("Failed to sort imports with isort.")
return code # Fall back to original code if isort fails
Expand Down
5 changes: 2 additions & 3 deletions codeflash/code_utils/instrument_existing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path
from typing import TYPE_CHECKING

import isort
import libcst as cst

from codeflash.cli_cmds.console import logger
Expand Down Expand Up @@ -741,7 +740,7 @@ def inject_async_profiling_into_existing_test(
new_imports.append(ast.Import(names=[ast.alias(name="timeout_decorator")]))

tree.body = [*new_imports, *tree.body]
return True, isort.code(ast.unparse(tree), float_to_top=True)
return True, sort_imports(ast.unparse(tree), float_to_top=True)


def inject_profiling_into_existing_test(
Expand Down Expand Up @@ -789,7 +788,7 @@ def inject_profiling_into_existing_test(
additional_functions = [create_wrapper_function(mode)]

tree.body = [*new_imports, *additional_functions, *tree.body]
return True, isort.code(ast.unparse(tree), float_to_top=True)
return True, sort_imports(ast.unparse(tree), float_to_top=True)


def create_wrapper_function(mode: TestingMode = TestingMode.BEHAVIOR) -> ast.FunctionDef:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_instrumentation_run_results_aiservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import isort
from code_to_optimize.bubble_sort_method import BubbleSorter
from codeflash.code_utils.code_utils import get_run_tmp_file
from codeflash.code_utils.formatter import sort_imports
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.models.models import FunctionParent, TestFile, TestFiles, TestingMode, TestType, VerificationType
from codeflash.optimization.optimizer import Optimizer
Expand Down Expand Up @@ -115,7 +116,7 @@ def test_single_element_list():
)
"""
)
instrumented_behavior_test_source = isort.code(
instrumented_behavior_test_source = sort_imports(
instrumented_behavior_test_source, config=isort.Config(float_to_top=True)
)

Expand Down Expand Up @@ -257,7 +258,7 @@ def test_single_element_list():
)
"""
)
instrumented_behavior_test_source = isort.code(
instrumented_behavior_test_source = sort_imports(
instrumented_behavior_test_source, config=isort.Config(float_to_top=True)
)

Expand Down
Loading