Skip to content

add ast tree and test - #49

Merged
dchou1618 merged 3 commits into
masterfrom
feature/ast_tree
Jul 14, 2026
Merged

dchou1618 merged 3 commits into
masterfrom
feature/ast_tree

Conversation

@dchou1618

Copy link
Copy Markdown
Owner

No description provided.

@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Implement AST call-finder and add unit tests

✨ Enhancement 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Extend AST Node to carry an explicit node type.
• Implement AST traversal to find calls to a target function name.
• Add pytest coverage to validate call detection across nested AST structures.
Diagram

graph TD
A["AST Root (Node)"] --> B["find_calls(root,name)"] --> C["DFS traverse"] --> D{"node_type == 'Call'?"}
D -->|"yes"| E{"first child is Name & matches"}
E -->|"yes"| F["append to results"]
E -->|"no"| C
D -->|"no"| C
F --> C
C --> G["return calls[]"]

subgraph Legend
direction LR
_db[(Database)] ~~~ _proc["Process"] ~~~ _dec{"Decision"}
end
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Iterative traversal (explicit stack)
  • ➕ Avoids Python recursion depth limits on very large ASTs
  • ➕ Often easier to instrument with debugging/metrics
  • ➖ Slightly more verbose than recursion for simple trees
  • ➖ More bookkeeping (push/pop) for equivalent behavior
2. Visitor pattern / callback-based walker
  • ➕ Scales to multiple analyses (calls, identifiers, literals) without duplicating traversal
  • ➕ Encapsulates node-type-specific logic cleanly
  • ➖ More abstraction than needed for a single query
  • ➖ Requires designing/maintaining visitor interfaces

Recommendation: The current recursive DFS approach is appropriate for the current scope and keeps the API simple. If this module is expected to process very large trees or grow into multiple analyses, consider migrating traversal into a reusable visitor/walker (or switching to an explicit stack) to avoid recursion depth issues and reduce repeated traversal code.

Files changed (2) +68 / -2

Enhancement (1) +14 / -2
ast_tree.pyAdd typed AST nodes and implement find_calls traversal +14/-2

Add typed AST nodes and implement find_calls traversal

• Updates Node to include a required node_type field. Implements find_calls using a recursive depth-first walk that collects Call nodes whose first child is a matching Name node.

Algorithms/static_analysis/ast_tree.py

Tests (1) +54 / -0
test_ast_tree.pyAdd parametrized pytest for call detection +54/-0

Add parametrized pytest for call detection

• Introduces a pytest parametrized test that builds a small AST with multiple Call nodes (including nested structures) and asserts the number of matches for a target function name.

tests/algorithms/test_ast_tree.py

@qodo-code-review

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Informational

1. Recursive walk can overflow 🐞 Bug ☼ Reliability
Description
find_calls() uses unbounded recursion (gather_calls -> gather_calls) so a sufficiently
deep/chain-like AST can raise RecursionError and abort analysis. This makes the traversal unreliable
for large or adversarially deep inputs.
Code

Algorithms/static_analysis/ast_tree.py[R14-23]

+    calls = []
+    def gather_calls(root, function_name):
+        if root:
+            if root.node_type == "Call":
+                if root.children and root.children[0].node_type == "Name" and \
+                    root.children[0].value == function_name:
+                    calls.append(root)
+            for child in root.children:
+                gather_calls(child, function_name)
+    gather_calls(root, function_name)
Relevance

⭐ Low

Similar “avoid RecursionError by replacing recursion with iterative traversal” suggestion was
rejected in PR #41.

PR-#41

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The new implementation recursively calls gather_calls for every child without any depth
protection. The codebase already includes an explicit stack-based traversal variant for dependency
cycle detection, indicating iterative traversals are an established approach here.

Algorithms/static_analysis/ast_tree.py[14-23]
Algorithms/static_analysis/package_dependency.py[38-69]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`find_calls()` traverses the AST recursively, which can hit Python recursion limits for very deep ASTs and raise `RecursionError`, aborting the analysis.

### Issue Context
The current implementation defines an inner `gather_calls()` that calls itself for each child.

### Fix Focus Areas
- Algorithms/static_analysis/ast_tree.py[14-23]

### Suggested fix
Rewrite the traversal iteratively (e.g., `stack = [root]` and `while stack: node = stack.pop(); stack.extend(node.children)`), keeping the same matching logic for `Call` nodes. Optionally add a visited-set guard if cycles are possible in constructed trees.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Missing EOF newline 🐞 Bug ⚙ Maintainability
Description
Algorithms/static_analysis/ast_tree.py has no trailing newline at end-of-file, which can cause
persistent formatting/diff noise and may fail strict editor/lint settings. This is a small repo
hygiene issue introduced/retained in this change.
Code

Algorithms/static_analysis/ast_tree.py[R23-24]

+    gather_calls(root, function_name)
+    return calls
Relevance

⭐ Low

Trailing newline/  No newline at end of file hygiene suggestion was rejected previously (PR #33).

PR-#33

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The file ends at line 24 with no trailing blank line/newline, matching the PR diff’s end-of-file
marker for missing newline.

Algorithms/static_analysis/ast_tree.py[1-24]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`Algorithms/static_analysis/ast_tree.py` is missing a trailing newline at end-of-file.

### Issue Context
The PR diff marks this file with `No newline at end of file`.

### Fix Focus Areas
- Algorithms/static_analysis/ast_tree.py[23-24]

### Suggested fix
Ensure the file ends with a single newline character (most editors have a setting for this).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

@dchou1618
dchou1618 merged commit 90073e6 into master Jul 14, 2026
5 checks passed
@dchou1618
dchou1618 deleted the feature/ast_tree branch July 14, 2026 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant