Fix RecursionError when analyzing deeply nested code structures #401
+23
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Vulture crashes with
RecursionError: maximum recursion depth exceededwhen analyzing code containing deeply nested expressions, such as long chains of binary operations. This was reported when running vulture on the Bandit project, which uses AST-based static analysis patterns.The error manifests as:
Root Cause
Vulture's custom
visit()method callsgeneric_visit()before processing each node to enable recursive reachability analysis. With deeply nested AST structures (500+ levels), this exhausts Python's recursion limit (default: 1000).Solution
This PR adds graceful handling of
RecursionErrorwith minimal changes:Catch RecursionError during AST visiting - The
scan()method now catchesRecursionErrorexceptions and logs a clear, actionable error message instead of crashing.Preserve error exit codes - Fixed the
report()method to ensureInvalidInputexit code is not overridden byDeadCodewhen both occur.Comprehensive test coverage - Added
test_recursion_error()to verify the fix works correctly with deeply nested code.Behavior
Before
After
Exit code: 1 (InvalidInput)
The tool now:
Testing
Fixes the issue reported in the original bug report where vulture would crash when analyzing projects like Bandit.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.