-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Generator always parenthesizes element in list comprehension #5777
Comments
Omg, too much macro use in our This code decides whether to parenthesize an expression or not ruff/crates/ruff_python_ast/src/source_code/generator.rs Lines 874 to 881 in 58a79c1
The Now, the generator adds parentheses if the parent's precedence is higher. We use the ruff/crates/ruff_python_ast/src/source_code/generator.rs Lines 1010 to 1019 in 58a79c1
The fix requires identifying the right precedence and passing it through. A second (unrelated) refactor could be to use an enum for the precedence instead of an Originally posted by @MichaReiser in #4880 (comment) |
## Summary This PR adds a new precedence level for the comprehension element. This fixes the generator to not add parentheses around the comprehension element every time. The new precedence level is `COMPREHENSION_ELEMENT` and it should occur after the `NAMED_EXPR` precedence level because named expressions are always parenthesized. This matches the behavior of Python `ast.unparse` and tested with the following snippet: ```python import ast code = "" ast.unparse(ast.parse(code)) ``` ## Test Plan Add a bunch of test cases for all the valid nodes at that position. fixes: #5777
Yeah, this seems like a bug:
Originally posted by @dhruvmanila in #4880 (comment)
The text was updated successfully, but these errors were encountered: