You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The custom generic_visit appends to ast_path but does not explicitly call super().generic_visit(node), which may prevent traversal of child nodes. Verify that recursion still occurs as intended.
Nested async functions aren’t excluded because only visit_FunctionDef was changed. Consider overriding visit_AsyncFunctionDef similarly to skip nested async defs.
defvisit_FunctionDef(self, node: FunctionDef) ->None:
# Check if the function has a return statement and add it to the listiffunction_has_return_statement(node) andnotfunction_is_a_property(node):
self.functions.append(
Ensure that after appending to ast_path you actually traverse the node’s children and then pop the entry to avoid an ever-growing stack and to continue visiting other nodes. Without calling super().generic_visit, child nodes won’t be visited, and without pop, your path tracking will break.
Why: The suggestion restores AST child traversal and ensures the path stack is properly popped, fixing a critical bug where nodes were neither visited nor cleaned up.
High
General
guarantee path cleanup
Wrap the append/pop logic in a try/finally block to guarantee that ast_path.pop() runs even if an exception occurs during traversal. This prevents the stack from becoming inconsistent on errors.
Why: Wrapping traversal in a try/finally block ensures ast_path.pop() always runs, preventing stack corruption even on errors.
Medium
selectively traverse non–function nodes
If you want to skip nested functions but still traverse other statements inside a function, manually visit only non-function children. This preserves other analyses without counting nested defs.
def visit_FunctionDef(self, node: FunctionDef) -> None:
# Check if the function has a return statement and add it to the list
if function_has_return_statement(node) and not function_is_a_property(node):
self.functions.append(
FunctionToOptimize(function_name=node.name, file_path=self.file_path, parents=self.ast_path[:])
)
+ for child in node.body:+ if not isinstance(child, (FunctionDef, AsyncFunctionDef)):+ self.visit(child)
Suggestion importance[1-10]: 7
__
Why: Manually iterating over node.body and skipping nested functions restores necessary traversal without counting nested defs, improving overall analysis.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
PR Type
Bug fix
Description
Remove nested function visits in FunctionDef
Prevent nested functions from being counted
Simplify visitor logic for FunctionDef nodes
Changes walkthrough 📝
functions_to_optimize.py
Skip nested functions in visitorcodeflash/discovery/functions_to_optimize.py