Skip to content

Commit beb1ee0

Browse files
committed
precommit mypy fix
1 parent e964ca6 commit beb1ee0

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

codeflash/api/aiservice.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ def get_aiservice_base_url(self) -> str:
5454
return "https://app.codeflash.ai"
5555

5656
def make_ai_service_request(
57-
self, endpoint: str, method: str = "POST", payload: dict[str, Any] | None = None, timeout: float | None = None
57+
self,
58+
endpoint: str,
59+
method: str = "POST",
60+
payload: dict[str, Any] | list[dict[str, Any]] | None = None,
61+
timeout: float | None = None,
5862
) -> requests.Response:
5963
"""Make an API request to the given endpoint on the AI service.
6064
@@ -292,7 +296,7 @@ def optimize_python_code_refinement(self, request: list[AIServiceRefinerRequest]
292296
OptimizedCandidate(
293297
source_code=opt["source_code"],
294298
explanation=opt["explanation"],
295-
optimization_id=opt["optimization_id"][:-4]+"refi",
299+
optimization_id=opt["optimization_id"][:-4] + "refi",
296300
)
297301
for opt in refined_optimizations
298302
]
@@ -336,7 +340,7 @@ def log_results( # noqa: D417
336340
"is_correct": is_correct,
337341
"codeflash_version": codeflash_version,
338342
"best_optimization_id": best_optimization_id,
339-
"optimized_line_profiler_results": optimized_line_profiler_results
343+
"optimized_line_profiler_results": optimized_line_profiler_results,
340344
}
341345
try:
342346
self.make_ai_service_request("/log_features", payload=payload, timeout=5)

codeflash/code_utils/code_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
ImportErrorPattern = re.compile(r"ModuleNotFoundError.*$", re.MULTILINE)
2121

22+
2223
def diff_length(a: str, b: str) -> int:
2324
"""Compute the length (in characters) of the unified diff between two strings.
2425
@@ -62,6 +63,7 @@ def create_rank_dictionary_compact(int_array: list[int]) -> dict[int, int]:
6263
# Create a dictionary mapping the original index to its rank (its position in the sorted list)
6364
return {original_index: rank for rank, original_index in enumerate(sorted_indices)}
6465

66+
6567
@contextmanager
6668
def custom_addopts() -> None:
6769
pyproject_file = find_pyproject_toml()

codeflash/optimization/function_optimizer.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ast
44
import concurrent.futures
5-
import difflib
65
import os
76
import random
87
import subprocess
@@ -32,13 +31,13 @@
3231
from codeflash.code_utils.code_utils import (
3332
ImportErrorPattern,
3433
cleanup_paths,
34+
create_rank_dictionary_compact,
35+
diff_length,
3536
file_name_from_test_module_name,
3637
get_run_tmp_file,
3738
has_any_async_functions,
3839
module_name_from_file_path,
3940
restore_conftest,
40-
diff_length,
41-
create_rank_dictionary_compact,
4241
)
4342
from codeflash.code_utils.config_consts import (
4443
INDIVIDUAL_TESTCASE_TIMEOUT,
@@ -363,7 +362,6 @@ def determine_best_candidate(
363362
exp_type: str,
364363
) -> BestOptimization | None:
365364
# TODO remove
366-
from codeflash.models.models import OptimizedCandidate
367365

368366
best_optimization: BestOptimization | None = None
369367
_best_runtime_until_now = original_code_baseline.runtime
@@ -482,7 +480,9 @@ def determine_best_candidate(
482480
original_helper_code=original_helper_code,
483481
candidate_index=candidate_index,
484482
)
485-
optimized_line_profiler_results[candidate.optimization_id]=line_profile_test_results['str_out']
483+
optimized_line_profiler_results[candidate.optimization_id] = line_profile_test_results[
484+
"str_out"
485+
]
486486
replay_perf_gain = {}
487487
if self.args.benchmark:
488488
test_results_by_benchmark = (
@@ -580,10 +580,12 @@ def determine_best_candidate(
580580
if not len(self.valid_optimizations):
581581
return None
582582
# need to figure out the best candidate here before we return best_optimization
583-
diff_lens_list = [] # character level diff
583+
diff_lens_list = [] # character level diff
584584
runtimes_list = []
585585
for valid_opt in self.valid_optimizations:
586-
diff_lens_list.append(diff_length(valid_opt.candidate.source_code, code_context.read_writable_code)) #char level diff
586+
diff_lens_list.append(
587+
diff_length(valid_opt.candidate.source_code, code_context.read_writable_code)
588+
) # char level diff
587589
runtimes_list.append(valid_opt.runtime)
588590
diff_lens_ranking = create_rank_dictionary_compact(diff_lens_list)
589591
runtimes_ranking = create_rank_dictionary_compact(runtimes_list)
@@ -598,7 +600,7 @@ def determine_best_candidate(
598600
optimized_runtime=optimized_runtimes,
599601
is_correct=is_correct,
600602
best_optimization_id=best_optimization.candidate.optimization_id,
601-
optimized_line_profiler_results= optimized_line_profiler_results
603+
optimized_line_profiler_results=optimized_line_profiler_results,
602604
)
603605
return best_optimization
604606

@@ -630,7 +632,7 @@ def refine_optimizations(
630632
fto_name=fto_name,
631633
)
632634
for opt in valid_optimizations
633-
] # TODO: multiple workers for this?
635+
] # TODO: multiple workers for this?
634636
future_refinement_results = executor.submit(ai_service_client.optimize_python_code_refinement, request=request)
635637
concurrent.futures.wait([future_refinement_results])
636638
return future_refinement_results.result()

0 commit comments

Comments
 (0)