Skip to content

Commit

Permalink
Merge pull request #54 from ryanhoangt/fix-bug-duplicate-class-fns
Browse files Browse the repository at this point in the history
Fix bug - class methods appear in top level index
  • Loading branch information
Marti2203 authored Jul 18, 2024
2 parents bf14eaf + 58fa2e4 commit 759a732
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/search/search_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 759a732

Please sign in to comment.