diff --git a/app/search/search_utils.py b/app/search/search_utils.py index 73eb8300..c648e7a9 100644 --- a/app/search/search_utils.py +++ b/app/search/search_utils.py @@ -149,6 +149,7 @@ def parse_python_file(file_full_path: str) -> tuple[list, dict, list] | None: # (3) get top-level functions in the file (exclues functions defined in classes) top_level_funcs = [] + function_nodes_in_class = [] for node in ast.walk(tree): if isinstance(node, ast.ClassDef): ## class part (1): collect class info @@ -159,14 +160,15 @@ def parse_python_file(file_full_path: str) -> tuple[list, dict, list] | None: classes.append((class_name, start_lineno, end_lineno)) ## class part (2): collect function info inside this class - class_funcs = [ - (n.name, n.lineno, n.end_lineno) - for n in ast.walk(node) - if isinstance(n, ast.FunctionDef) - ] + class_funcs = [] + for n in ast.walk(node): + if isinstance(n, ast.FunctionDef): + class_funcs.append((n.name, n.lineno, n.end_lineno)) + function_nodes_in_class.append(n) class_to_funcs[class_name] = class_funcs - elif isinstance(node, ast.FunctionDef): + # top-level functions, excluding functions defined in classes + elif isinstance(node, ast.FunctionDef) and node not in function_nodes_in_class: function_name = node.name start_lineno = node.lineno end_lineno = node.end_lineno