Skip to content

Commit 620a83d

Browse files
authored
Merge pull request #34 from codeflash-ai/ruff-fixes
ruff check & format
2 parents 69e43dd + e4b18c8 commit 620a83d

40 files changed

+191
-177
lines changed

codeflash/api/aiservice.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,9 @@ def generate_regression_tests(
196196
- Dict[str, str] | None: The generated regression tests and instrumented tests, or None if an error occurred.
197197
198198
"""
199-
assert test_framework in [
200-
"pytest",
201-
"unittest",
202-
], f"Invalid test framework, got {test_framework} but expected 'pytest' or 'unittest'"
199+
assert test_framework in ["pytest", "unittest"], (
200+
f"Invalid test framework, got {test_framework} but expected 'pytest' or 'unittest'"
201+
)
203202
payload = {
204203
"source_code_being_tested": source_code_being_tested,
205204
"function_to_optimize": function_to_optimize,

codeflash/cli_cmds/cmd_init.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def determine_dependency_manager(pyproject_data: dict[str, Any]) -> DependencyMa
462462
return DependencyManager.POETRY
463463

464464
# Check for uv
465-
if any(key.startswith("uv") for key in tool_section.keys()):
465+
if any(key.startswith("uv") for key in tool_section):
466466
return DependencyManager.UV
467467

468468
# Look for pip-specific markers
@@ -555,9 +555,7 @@ def customize_codeflash_yaml_content(
555555

556556
# Add codeflash command
557557
codeflash_cmd = get_codeflash_github_action_command(dep_manager)
558-
optimize_yml_content = optimize_yml_content.replace("{{ codeflash_command }}", codeflash_cmd)
559-
560-
return optimize_yml_content
558+
return optimize_yml_content.replace("{{ codeflash_command }}", codeflash_cmd)
561559

562560

563561
# Create or update the pyproject.toml file with the Codeflash dependency & configuration
@@ -596,8 +594,8 @@ def configure_pyproject_toml(setup_info: SetupInfo) -> None:
596594
formatter_cmds.append("disabled")
597595
if formatter in ["black", "ruff"]:
598596
try:
599-
result = subprocess.run([formatter], capture_output=True, check=False)
600-
except FileNotFoundError as e:
597+
subprocess.run([formatter], capture_output=True, check=False)
598+
except FileNotFoundError:
601599
click.echo(f"⚠️ Formatter not found: {formatter}, please ensure it is installed")
602600
codeflash_section["formatter-cmds"] = formatter_cmds
603601
# Add the 'codeflash' section, ensuring 'tool' section exists

codeflash/cli_cmds/console.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
from contextlib import contextmanager
55
from itertools import cycle
6-
from typing import TYPE_CHECKING, Generator
6+
from typing import TYPE_CHECKING
77

88
from rich.console import Console
99
from rich.logging import RichHandler
@@ -13,6 +13,8 @@
1313
from codeflash.cli_cmds.logging_config import BARE_LOGGING_FORMAT
1414

1515
if TYPE_CHECKING:
16+
from collections.abc import Generator
17+
1618
from rich.progress import TaskID
1719

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

codeflash/code_utils/code_extractor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ def get_code(functions_to_optimize: list[FunctionToOptimize]) -> tuple[str | Non
115115
or (functions_to_optimize[0].parents and functions_to_optimize[0].parents[0].type != "ClassDef")
116116
or (
117117
len(functions_to_optimize[0].parents) > 1
118-
or (len(functions_to_optimize) > 1)
119-
and len({fn.parents[0] for fn in functions_to_optimize}) != 1
118+
or ((len(functions_to_optimize) > 1) and len({fn.parents[0] for fn in functions_to_optimize}) != 1)
120119
)
121120
):
122121
return None, set()

codeflash/code_utils/code_replacer.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import ast
4-
import re
54
from collections import defaultdict
65
from functools import lru_cache
76
from typing import TYPE_CHECKING, Optional, TypeVar
@@ -91,10 +90,10 @@ def leave_ClassDef(self, node: cst.ClassDef) -> None:
9190
class OptimFunctionReplacer(cst.CSTTransformer):
9291
def __init__(
9392
self,
94-
modified_functions: dict[tuple[str | None, str], cst.FunctionDef] = None,
95-
new_functions: list[cst.FunctionDef] = None,
96-
new_class_functions: dict[str, list[cst.FunctionDef]] = None,
97-
modified_init_functions: dict[str, cst.FunctionDef] = None,
93+
modified_functions: Optional[dict[tuple[str | None, str], cst.FunctionDef]] = None,
94+
new_functions: Optional[list[cst.FunctionDef]] = None,
95+
new_class_functions: Optional[dict[str, list[cst.FunctionDef]]] = None,
96+
modified_init_functions: Optional[dict[str, cst.FunctionDef]] = None,
9897
) -> None:
9998
super().__init__()
10099
self.modified_functions = modified_functions if modified_functions is not None else {}

codeflash/code_utils/code_utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
def get_qualified_name(module_name: str, full_qualified_name: str) -> str:
1414
if not full_qualified_name:
15-
raise ValueError("full_qualified_name cannot be empty")
15+
msg = "full_qualified_name cannot be empty"
16+
raise ValueError(msg)
1617
if not full_qualified_name.startswith(module_name):
1718
msg = f"{full_qualified_name} does not start with {module_name}"
1819
raise ValueError(msg)
@@ -46,9 +47,9 @@ def file_name_from_test_module_name(test_module_name: str, base_dir: Path) -> Pa
4647
def get_imports_from_file(
4748
file_path: Path | None = None, file_string: str | None = None, file_ast: ast.AST | None = None
4849
) -> list[ast.Import | ast.ImportFrom]:
49-
assert (
50-
sum([file_path is not None, file_string is not None, file_ast is not None]) == 1
51-
), "Must provide exactly one of file_path, file_string, or file_ast"
50+
assert sum([file_path is not None, file_string is not None, file_ast is not None]) == 1, (
51+
"Must provide exactly one of file_path, file_string, or file_ast"
52+
)
5253
if file_path:
5354
with file_path.open(encoding="utf8") as file:
5455
file_string = file.read()

codeflash/code_utils/compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111

1212
SAFE_SYS_EXECUTABLE: str = Path(sys.executable).as_posix()
1313

14-
IS_POSIX = os.name != "nt"
14+
IS_POSIX = os.name != "nt"

codeflash/code_utils/concolic_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _split_top_level_args(self, args_str: str) -> list[str]:
6262

6363
return result
6464

65-
def __init__(self):
65+
def __init__(self) -> None:
6666
# Pre-compiling regular expressions for faster execution
6767
self.assert_re = re.compile(r"\s*assert\s+(.*?)(?:\s*==\s*.*)?$")
6868
self.unittest_re = re.compile(r"(\s*)self\.assert([A-Za-z]+)\((.*)\)$")

codeflash/code_utils/config_parser.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ def parse_config_file(config_file_path: Path | None = None) -> tuple[dict[str, A
8181
else: # Default to empty list
8282
config[key] = []
8383

84-
assert config["test-framework"] in [
85-
"pytest",
86-
"unittest",
87-
], "In pyproject.toml, Codeflash only supports the 'test-framework' as pytest and unittest."
84+
assert config["test-framework"] in ["pytest", "unittest"], (
85+
"In pyproject.toml, Codeflash only supports the 'test-framework' as pytest and unittest."
86+
)
8887
if len(config["formatter-cmds"]) > 0:
8988
assert config["formatter-cmds"][0] != "your-formatter $file", (
9089
"The formatter command is not set correctly in pyproject.toml. Please set the "

codeflash/code_utils/formatter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import shlex
55
import subprocess
6-
import sys
76
from typing import TYPE_CHECKING
87

98
import isort

0 commit comments

Comments
 (0)