berkerpeksag / astor

Python AST read/write
https://pypi.org/project/astor/
BSD 3-Clause "New" or "Revised" License
810 stars 102 forks source link

Incorrect result for tuple expansion #92

Closed ryanorendorff closed 6 years ago

ryanorendorff commented 6 years ago

Hello,

There seems to be an error turning an ast back into the correct form when expanding tuples. For example, take the following code.

a = (1, 2)
b = (*a, 3)

Running rtrip on this code fragment leads to the following

a = 1, 2
b = *a, 3

which unfortunately is not valid python.

berkerpeksag commented 6 years ago

Thank you for your report. This is indeed a bug. Would you like to submit a PR?

radomirbosak commented 6 years ago

According to my tests, both of the forms (with and without parentheses) are valid in python3.5+:

$ python3.5 -c 'a = 1, 2; b = *a, 3'

and are invalid in python3.4 and older:

$ python3.4 -c 'a = 1, 2; b = *a, 3'
  File "<string>", line 1
SyntaxError: can use starred expression only as assignment target

This unpacking feature became available with PEP 448 in python3.5.

I don't think that this is a bug - we should close it.

berkerpeksag commented 6 years ago

Good catch, @radomirbosak, thank you!

@ryanorendorff I'm going to close this for now, but we can always reopen if you have time to give us a little bit more information to understand the issue.