Skip to content

Commit 9282ae3

Browse files
committed
found a case where jedi's full name did not start with the module name. we need this check so it doesn't error out
1 parent 96e2a2e commit 9282ae3

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

codeflash/context/code_context_extractor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from jedi.api.classes import Name
1212
from libcst import CSTNode
1313

14-
from codeflash.cli_cmds.console import logger
14+
from codeflash.cli_cmds.console import code_print, logger
1515
from codeflash.code_utils.code_extractor import add_needed_imports_from_module, find_preexisting_objects
1616
from codeflash.code_utils.code_utils import get_qualified_name, path_belongs_to_site_packages
1717
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
@@ -73,6 +73,7 @@ def get_code_optimization_context(
7373

7474
# Handle token limits
7575
tokenizer = tiktoken.encoding_for_model("gpt-4o")
76+
code_print(final_read_writable_code)
7677
final_read_writable_tokens = len(tokenizer.encode(final_read_writable_code))
7778
if final_read_writable_tokens > optim_token_limit:
7879
raise ValueError("Read-writable code has exceeded token limit, cannot proceed")
@@ -356,6 +357,7 @@ def get_function_to_optimize_as_function_source(
356357
name.type == "function"
357358
and name.full_name
358359
and name.name == function_to_optimize.function_name
360+
and name.full_name.startswith(name.module_name)
359361
and get_qualified_name(name.module_name, name.full_name) == function_to_optimize.qualified_name
360362
):
361363
function_source = FunctionSource(
@@ -410,6 +412,7 @@ def get_function_sources_from_jedi(
410412
and definition.full_name
411413
and definition.type == "function"
412414
and not belongs_to_function_qualified(definition, qualified_function_name)
415+
and definition.full_name.startswith(definition.module_name)
413416
# Avoid nested functions or classes. Only class.function is allowed
414417
and len((qualified_name := get_qualified_name(definition.module_name, definition.full_name)).split(".")) <= 2
415418
):

codeflash/optimization/function_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def belongs_to_class(name: Name, class_name: str) -> bool:
3131
def belongs_to_function_qualified(name: Name, qualified_function_name: str) -> bool:
3232
"""Check if the given jedi Name is a direct child of the specified function, matched by qualified function name."""
3333
try:
34-
if get_qualified_name(name.module_name, name.full_name) == qualified_function_name:
34+
if name.full_name.startswith(name.module_name) and get_qualified_name(name.module_name, name.full_name) == qualified_function_name:
3535
# Handles function definition and recursive function calls
3636
return False
3737
if name := name.parent():

0 commit comments

Comments
 (0)