Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(python): Improve lambda function handling
  • Loading branch information
lexdotdev committed Sep 4, 2025
commit e30db7fbafe442804ec5e0c0fda2f9f37f1aa2e3
38 changes: 28 additions & 10 deletions languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg
Original file line number Diff line number Diff line change
Expand Up @@ -765,16 +765,10 @@ inherit .parent_module

(with_clause) {}

[
(function_definition
parameters: (_) @params
body: (_) @body
) @func
(lambda
parameters: (_) @params
body: (_) @body
)@func
] {
(function_definition
parameters: (_) @params
body: (_) @body
) @func {
node @func.call
node return_value
node drop_scope
Expand All @@ -790,6 +784,30 @@ inherit .parent_module
let @func.function_returns = return_value
}

(lambda
body: (_) @body
) @lam {
node @lam.call
node return_value
node @body.after_scope

edge @lam.call -> return_value
edge @body.before_scope -> @body.after_scope
edge @body.after_scope -> @lam.bottom
attr (@lam.call) pop_scoped_symbol = "()"
attr (return_value) is_exported
let @lam.function_returns = return_value
}

(lambda
parameters: (_) @params
body: (_) @body
) {
edge @body.before_scope -> @params.after_scope
edge @params.before_scope -> JUMP_TO_SCOPE_NODE
edge @params.after_scope -> @body.after_scope
}
Comment on lines +787 to +809

Copilot AI Sep 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The lambda handling is split into two separate rules that could lead to duplication. Consider consolidating the common scope setup logic or adding a comment explaining why this separation is necessary for the grammar to work correctly.

Copilot uses AI. Check for mistakes.

(function_definition
name: (identifier) @name
body: (_) @body
Expand Down
13 changes: 11 additions & 2 deletions languages/tree-sitter-stack-graphs-python/test/lambdas.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
sorted([1, 2, 3], key=lambda x: x)
# ^ defined: 1
y = x = 1

no_params = lambda: y
# ^ defined: 1

sorted([1, 2, 3], key=lambda y: x + y)
# ^ defined: 1

def uses_default(fn=lambda: 1):
fn()
# ^ defined: 9