Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast.unparse: Wrong unparse with something like "() = []". #92671

Closed
NIKDISSV-Forever opened this issue May 11, 2022 · 3 comments
Closed

ast.unparse: Wrong unparse with something like "() = []". #92671

NIKDISSV-Forever opened this issue May 11, 2022 · 3 comments
Labels
3.11 3.12 type-bug An unexpected behavior, bug, or error

Comments

@NIKDISSV-Forever
Copy link

NIKDISSV-Forever commented May 11, 2022

Generates incorrect code if unparse passes an assignment with an empty tuple on the left side.

import ast
ast.unparse(ast.parse("() = []")) # " = []"
ast.unparse(ast.parse("[] = () = []")) # "[] = = []"

The simplest solution would be to remove the line
self.set_precedence(_Precedence.TUPLE, target)
in ast._Unparser.visit_Assign

Doesn't work properly in 3.11.0b1, 3.12.0a0
In 3.9.12, 3.10.4 - works correctly

@NIKDISSV-Forever NIKDISSV-Forever added the type-bug An unexpected behavior, bug, or error label May 11, 2022
@AlexWaygood
Copy link
Member

AlexWaygood commented May 11, 2022

Reproduced, and confirmed that these work fine on 3.10.

This one does work correctly on 3.12:

>>> import ast
>>> ast.unparse(ast.parse("[] = ()"))
'[] = ()'

cc. @pablogsal, @isidentical

@NIKDISSV-Forever
Copy link
Author

NIKDISSV-Forever commented May 16, 2022

Reproduced, and confirmed that these work fine on 3.10.

This one does work correctly on 3.12:

>>> import ast
>>> ast.unparse(ast.parse("[] = ()"))
'[] = ()'

cc. @pablogsal, @isidentical

It's not the same. The tuple must be on the left.

>>> import ast
>>> ast.unparse(ast.parse("() = []"))
' = []'

miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 16, 2022
…ythonGH-92673)

(cherry picked from commit f6fd8aa)

Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
miss-islington added a commit that referenced this issue May 16, 2022
(cherry picked from commit f6fd8aa)

Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
@AlexWaygood
Copy link
Member

AlexWaygood commented May 16, 2022

Fixed in #92673, and backported to 3.11. Thanks for the report, @NIKDISSV-Forever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 3.12 type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants