This repository was archived by the owner on Sep 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
Fix multiple Python TSG grammar scoping errors #498
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c6390ae
feat(python): Chained assignments support
Lexxxzy e30db7f
fix(python): Improve lambda function handling
Lexxxzy 49573e2
feat(python): Support typed splat parameters
Lexxxzy 0c7b639
fix(python): Superclass keyword arguments
Lexxxzy d9f30d0
test(python): Correct the scope of lambda params definitions
Lexxxzy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,6 +195,9 @@ attribute node_symbol = node => symbol = (source-text node), source_n | |
| ; with item | ||
| (with_item) | ||
|
|
||
| ; assignment | ||
| (assignment) | ||
|
|
||
| ; pattern list | ||
| (pattern_list) | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
| } | ||
|
|
||
| (function_definition | ||
| name: (identifier) @name | ||
| body: (_) @body | ||
|
|
@@ -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
|
||
| (parameter/list_splat_pattern | ||
| (_) @name) @param | ||
| (parameter/dictionary_splat_pattern | ||
|
|
@@ -933,7 +960,7 @@ inherit .parent_module | |
|
|
||
| (class_definition | ||
| superclasses: (argument_list | ||
| (_) @superclass)) @class | ||
| (expression) @superclass)) @class | ||
| { | ||
| edge @class.super_scope -> @superclass.output | ||
| } | ||
|
|
@@ -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 | ||
| } | ||
|
|
||
12 changes: 12 additions & 0 deletions
12
languages/tree-sitter-stack-graphs-python/test/assignments.py
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
| 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 |
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
| 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 |
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
9 changes: 9 additions & 0 deletions
9
languages/tree-sitter-stack-graphs-python/test/typed_splat_params.py
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
| 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 |
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.
There was a problem hiding this comment.
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.