- Fix is_zero_diff checker by using ast.unparse rather than ast.dump
- If only the imports are different then it is still 0 diff - add test cases - Codeflash does not support Python 3.13 yet. I saw pydantic crash while installing codeflash on 3.13 python
This commit is contained in:
parent
0ddef46323
commit
71bcd07be3
4 changed files with 55 additions and 5 deletions
|
|
@ -253,8 +253,18 @@ def replace_function_definitions_in_module(
|
||||||
contextual_functions,
|
contextual_functions,
|
||||||
project_root_path,
|
project_root_path,
|
||||||
)
|
)
|
||||||
if ast.dump(ast.parse(new_code)) == ast.dump(ast.parse(source_code)):
|
if is_zero_diff(source_code, new_code):
|
||||||
return False
|
return False
|
||||||
with open(module_abspath, "w", encoding="utf8") as file:
|
with open(module_abspath, "w", encoding="utf8") as file:
|
||||||
file.write(new_code)
|
file.write(new_code)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def is_zero_diff(original_code: str, new_code: str) -> bool:
|
||||||
|
def normalize_for_diff(tree: ast.AST):
|
||||||
|
tree.body = [node for node in tree.body if not isinstance(node, (ast.Import, ast.ImportFrom))]
|
||||||
|
return tree
|
||||||
|
|
||||||
|
original_code_unparsed = ast.unparse(normalize_for_diff(ast.parse(original_code)))
|
||||||
|
new_code_unparsed = ast.unparse(normalize_for_diff(ast.parse(new_code)))
|
||||||
|
return original_code_unparsed == new_code_unparsed
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
# These version placeholders will be replaced by poetry-dynamic-versioning during `poetry build`.
|
# These version placeholders will be replaced by poetry-dynamic-versioning during `poetry build`.
|
||||||
__version__ = "0.6.11"
|
__version__ = "0.6.12"
|
||||||
__version_tuple__ = (0, 6, 11)
|
__version_tuple__ = (0, 6, 12)
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ keywords = ["codeflash", "performance", "optimization", "ai", "code", "machine l
|
||||||
# poetry self add poetry-dynamic-versioning
|
# poetry self add poetry-dynamic-versioning
|
||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = ">=3.9,<4.0"
|
python = ">=3.9,<3.13"
|
||||||
unidiff = ">=0.7.4"
|
unidiff = ">=0.7.4"
|
||||||
pytest = ">=7.0.0"
|
pytest = ">=7.0.0"
|
||||||
gitpython = ">=3.1.31"
|
gitpython = ">=3.1.31"
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,11 @@ from argparse import Namespace
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from codeflash.code_utils.code_replacer import replace_functions_and_add_imports, replace_functions_in_file
|
from codeflash.code_utils.code_replacer import (
|
||||||
|
is_zero_diff,
|
||||||
|
replace_functions_and_add_imports,
|
||||||
|
replace_functions_in_file,
|
||||||
|
)
|
||||||
from codeflash.discovery.functions_to_optimize import FunctionParent, FunctionToOptimize
|
from codeflash.discovery.functions_to_optimize import FunctionParent, FunctionToOptimize
|
||||||
from codeflash.optimization.optimizer import Optimizer
|
from codeflash.optimization.optimizer import Optimizer
|
||||||
|
|
||||||
|
|
@ -1499,3 +1503,39 @@ def cosine_similarity_top_k(
|
||||||
return ret_idxs, scores
|
return ret_idxs, scores
|
||||||
'''
|
'''
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_0_diff_code_replacement():
|
||||||
|
original_code = """from __future__ import annotations
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
def functionA():
|
||||||
|
return np.array([1, 2, 3])
|
||||||
|
"""
|
||||||
|
optim_code_a = """from __future__ import annotations
|
||||||
|
import numpy as np
|
||||||
|
def functionA():
|
||||||
|
return np.array([1, 2, 3])"""
|
||||||
|
|
||||||
|
assert is_zero_diff(original_code, optim_code_a)
|
||||||
|
|
||||||
|
optim_code_b = """
|
||||||
|
import numpy as np
|
||||||
|
def functionA():
|
||||||
|
return np.array([1, 2, 3])"""
|
||||||
|
|
||||||
|
assert is_zero_diff(original_code, optim_code_b)
|
||||||
|
|
||||||
|
optim_code_c = """
|
||||||
|
def functionA():
|
||||||
|
return np.array([1, 2, 3])"""
|
||||||
|
|
||||||
|
assert is_zero_diff(original_code, optim_code_c)
|
||||||
|
|
||||||
|
optim_code_d = """from __future__ import annotations
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
def functionA():
|
||||||
|
return np.array([1, 2, 3, 4])
|
||||||
|
"""
|
||||||
|
assert not is_zero_diff(original_code, optim_code_d)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue