Skip to content

Commit

Permalink
Python: Support assignments of the form [x,y,z] = w
Browse files Browse the repository at this point in the history
Surprisingly, the new parser did not support these constructs (and the
relevant test was missing this case), so on files that required the new
parser we were unable to parse this construct.

To fix it, we add `list_pattern` (not to be confused with
`pattern_list`) as a `tree-sitter-python` node that results in a `List`
node in the AST.
  • Loading branch information
tausbn committed Oct 22, 2024
1 parent 89ea4b8 commit 4f60494
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/extractor/tests/parser/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@

s, *t = u

[v, *w] = x

o,p, = q,r,
7 changes: 7 additions & 0 deletions python/extractor/tsg-python/python.tsg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
[ (expression_list) (tuple) (tuple_pattern) (pattern_list) ] @tuple
{ let @tuple.node = (ast-node @tuple "Tuple") }

(list_pattern) @list
{ let @list.node = (ast-node @list "List") }

(call) @call { let @call.node = (ast-node @call "Call") }

(for_statement) @for
Expand Down Expand Up @@ -3436,6 +3439,9 @@
; Left hand side of an assignment such as `foo, bar = ...`
(pattern_list element: (_) @elt) @parent

; Left hand side of an assignment such as `[foo, bar] = ...`
(list_pattern element: (_) @elt) @parent

; An unadorned tuple (such as in `x = y, z`)
(expression_list element: (_) @elt) @parent

Expand Down Expand Up @@ -3472,6 +3478,7 @@
(tuple element: (_) @elt)
(tuple_pattern element: (_) @elt)
(pattern_list element: (_) @elt)
(list_pattern element: (_) @elt)
(expression_list element: (_) @elt)
(parenthesized_expression inner: (_) @elt)
(set element: (_) @elt)
Expand Down

0 comments on commit 4f60494

Please sign in to comment.