baron icon indicating copy to clipboard operation
baron copied to clipboard

Extended unpacking unsupported in many contexts

Open lordmauve opened this issue 5 years ago • 2 comments

Baron only supports PEP-3132 extended unpacking in assignment context, ie. it can parse a, *b = xs etc

This syntax also valid in various other contexts. For example, for-loop context:

>>> xs = [range(10), range(0, 20, 2)]
>>> for a, *b in xs:
...     print(b)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8, 10, 12, 14, 16, 18]

but I get an error in baron==0.9

>>> baron.parse('for a, *b in rs:\n    print(b)\n')
Traceback (most recent call last):
...
baron.parser.ParsingError: Error, got an unexpected token STAR here:

   1 for a, *<---- here

I've tested this syntax and found it is valid in comprehensions...

[b for a, *b in xs]

...in with-blocks...

... @contextmanager
... def ctx():
...     yield range(10)
...
>>> with ctx() as (a, *b):
>>>     print(b)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

...and is also available recursively...

>>> (a, *b), (c, *d), (e, *f) = [range(3)] * 3
>>> a, b, c, d, e, f
(0, [1, 2], 0, [1, 2], 0, [1, 2])
>>> a, *(*b, c) = range(5)
>>> a, b, c
(0, [1, 2, 3], 4)

...all of which are currently unsupported by baron.

lordmauve avatar Mar 27 '19 13:03 lordmauve

+1 on this. Probably the simplest case I can find is:

(1, *(2, 3))
ParsingError: Error, got an unexpected token STAR here:

   1 [b for a, *<---- here

DylanLukes avatar Jan 25 '20 05:01 DylanLukes

+1 for me also

asterbini avatar Jul 29 '20 16:07 asterbini