From c6390ae4da5b49cb0e2a787f2892b74519de759c Mon Sep 17 00:00:00 2001 From: Lexxxzy Date: Thu, 4 Sep 2025 20:04:09 +0300 Subject: [PATCH 1/5] feat(python): Chained assignments support --- .../src/stack-graphs.tsg | 26 ++++++++++++------- .../test/assignments.py | 12 +++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 languages/tree-sitter-stack-graphs-python/test/assignments.py diff --git a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg index 5fb407ae8..09dd2fa5c 100644 --- a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg +++ b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg @@ -195,6 +195,9 @@ attribute node_symbol = node => symbol = (source-text node), source_n ; with item (with_item) + ; assignment + (assignment) + ; pattern list (pattern_list) @@ -1285,16 +1288,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 } diff --git a/languages/tree-sitter-stack-graphs-python/test/assignments.py b/languages/tree-sitter-stack-graphs-python/test/assignments.py new file mode 100644 index 000000000..bc9913ed8 --- /dev/null +++ b/languages/tree-sitter-stack-graphs-python/test/assignments.py @@ -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 From e30db7fbafe442804ec5e0c0fda2f9f37f1aa2e3 Mon Sep 17 00:00:00 2001 From: Lexxxzy Date: Thu, 4 Sep 2025 20:04:09 +0300 Subject: [PATCH 2/5] fix(python): Improve lambda function handling --- .../src/stack-graphs.tsg | 38 ++++++++++++++----- .../test/lambdas.py | 13 ++++++- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg index 09dd2fa5c..81d60d11c 100644 --- a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg +++ b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg @@ -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 @@ -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 +} + (function_definition name: (identifier) @name body: (_) @body diff --git a/languages/tree-sitter-stack-graphs-python/test/lambdas.py b/languages/tree-sitter-stack-graphs-python/test/lambdas.py index 9812df4de..13e10fa07 100644 --- a/languages/tree-sitter-stack-graphs-python/test/lambdas.py +++ b/languages/tree-sitter-stack-graphs-python/test/lambdas.py @@ -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 From 49573e2bc7af3601f1143067aba1b5d55e4b5a4f Mon Sep 17 00:00:00 2001 From: Lexxxzy Date: Thu, 4 Sep 2025 20:04:09 +0300 Subject: [PATCH 3/5] feat(python): Support typed splat parameters --- .../tree-sitter-stack-graphs-python/src/stack-graphs.tsg | 8 +++++++- .../test/typed_splat_params.py | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 languages/tree-sitter-stack-graphs-python/test/typed_splat_params.py diff --git a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg index 81d60d11c..faf4f6396 100644 --- a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg +++ b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg @@ -881,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 (parameter/list_splat_pattern (_) @name) @param (parameter/dictionary_splat_pattern diff --git a/languages/tree-sitter-stack-graphs-python/test/typed_splat_params.py b/languages/tree-sitter-stack-graphs-python/test/typed_splat_params.py new file mode 100644 index 000000000..4cb4010fe --- /dev/null +++ b/languages/tree-sitter-stack-graphs-python/test/typed_splat_params.py @@ -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 From 0c7b6395923d4caec47fcb7b4c026aac709b0d86 Mon Sep 17 00:00:00 2001 From: Lexxxzy Date: Thu, 4 Sep 2025 20:04:09 +0300 Subject: [PATCH 4/5] fix(python): Superclass keyword arguments --- .../tree-sitter-stack-graphs-python/src/stack-graphs.tsg | 2 +- .../tree-sitter-stack-graphs-python/test/superclasses.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg index faf4f6396..26d0cb0f6 100644 --- a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg +++ b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg @@ -960,7 +960,7 @@ inherit .parent_module (class_definition superclasses: (argument_list - (_) @superclass)) @class + (expression) @superclass)) @class { edge @class.super_scope -> @superclass.output } diff --git a/languages/tree-sitter-stack-graphs-python/test/superclasses.py b/languages/tree-sitter-stack-graphs-python/test/superclasses.py index 849a192a6..7631bd3ba 100644 --- a/languages/tree-sitter-stack-graphs-python/test/superclasses.py +++ b/languages/tree-sitter-stack-graphs-python/test/superclasses.py @@ -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 From d9f30d04a7ced04e2063cf05281f76dfd559ecac Mon Sep 17 00:00:00 2001 From: Lexxxzy Date: Thu, 4 Sep 2025 20:26:58 +0300 Subject: [PATCH 5/5] test(python): Correct the scope of lambda params definitions --- languages/tree-sitter-stack-graphs-python/test/lambdas.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/languages/tree-sitter-stack-graphs-python/test/lambdas.py b/languages/tree-sitter-stack-graphs-python/test/lambdas.py index 13e10fa07..bdcae17c5 100644 --- a/languages/tree-sitter-stack-graphs-python/test/lambdas.py +++ b/languages/tree-sitter-stack-graphs-python/test/lambdas.py @@ -5,7 +5,8 @@ sorted([1, 2, 3], key=lambda y: x + y) # ^ defined: 1 + # ^ defined: 1, 6 def uses_default(fn=lambda: 1): fn() -# ^ defined: 9 +# ^ defined: 10