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 all commits
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
74 changes: 52 additions & 22 deletions languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ attribute node_symbol = node => symbol = (source-text node), source_n
; with item
(with_item)

; assignment
(assignment)

; pattern list
(pattern_list)

Expand Down Expand Up @@ -762,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 @@ -787,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
Copy link

Copilot AI Sep 4, 2025

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 Expand Up @@ -860,7 +881,13 @@ inherit .parent_module

[
(parameter/typed_parameter
. (_) @name) @param
. (identifier) @name) @param
(parameter/typed_parameter
. (list_splat_pattern
(_) @name)) @param
(parameter/typed_parameter
. (dictionary_splat_pattern
(_) @name)) @param
Comment on lines 883 to +890
Copy link

Copilot AI Sep 4, 2025

Choose a reason for hiding this comment

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

[nitpick] The parameter pattern matching rules are repetitive. Consider using a more concise pattern that captures all these cases, or add comments explaining the specific requirements for each pattern type.

Copilot uses AI. Check for mistakes.
(parameter/list_splat_pattern
(_) @name) @param
(parameter/dictionary_splat_pattern
Expand Down Expand Up @@ -933,7 +960,7 @@ inherit .parent_module

(class_definition
superclasses: (argument_list
(_) @superclass)) @class
(expression) @superclass)) @class
{
edge @class.super_scope -> @superclass.output
}
Expand Down Expand Up @@ -1285,16 +1312,19 @@ inherit .parent_module
edge @case.new_bindings -> @pattern.new_bindings
}

[
(assignment
left: (_) @pattern
right: (_) @value)
(with_item
value:
(as_pattern
(_) @value
alias: (as_pattern_target (_) @pattern)))
]
(assignment
left: (_) @pattern
right: (_) @value) @assignment
{
edge @pattern.input -> @value.output
edge @assignment.output -> @value.output
}

(with_item
value:
(as_pattern
(_) @value
alias: (as_pattern_target (_) @pattern)))
{
edge @pattern.input -> @value.output
}
Expand Down
12 changes: 12 additions & 0 deletions languages/tree-sitter-stack-graphs-python/test/assignments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exponent = exp = 3
digits = decimals = abs(exp)
# ^ defined: 1

print digits
# ^ defined: 2

print decimals
# ^ defined: 2

print exponent
# ^ defined: 1
14 changes: 12 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,12 @@
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
# ^ defined: 1, 6

def uses_default(fn=lambda: 1):
fn()
# ^ defined: 10
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ def some_method(self):
def other(self):
super().some_method()
# ^ defined: 5

def __init_subclass__(cls, **kwargs):
cls._c = kwargs.pop("c", True)
super().__init_subclass__(**kwargs)

class C(B, c=False):
def subclass(self):
self.some_method()
# ^ defined: 5, 14
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Any

def build(*args: Any, some: str | None = None, **kwargs: Any):
print args
# ^ defined: 3
print kwargs
# ^ defined: 3
print some
# ^ defined: 3