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

Remove unnecessary walrus operator parentheses #5781

Closed
Tracked by #4798 ...
MichaReiser opened this issue Jul 15, 2023 · 2 comments · Fixed by #6173
Closed
Tracked by #4798 ...

Remove unnecessary walrus operator parentheses #5781

MichaReiser opened this issue Jul 15, 2023 · 2 comments · Fixed by #6173
Labels
formatter Related to the formatter help wanted Contributions especially welcome

Comments

@MichaReiser
Copy link
Member

MichaReiser commented Jul 15, 2023

Our formatter currently always adds parentheses around the walrus (a := 5) operator in for/if/... cases

impl NeedsParentheses for ExprNamedExpr {
fn needs_parentheses(
&self,
_parent: AnyNodeRef,
_context: &PyFormatContext,
) -> OptionalParentheses {
// Unlike tuples, named expression parentheses are not part of the range even when
// mandatory. See [PEP 572](https://peps.python.org/pep-0572/) for details.
OptionalParentheses::Always
}

The goal of this issue is to implement black's parentheses behavior (inside NeedsParentheses) and only preserve parentheses that are necessary.

https://github.com/psf/black/blob/b4dca26c7d93f930bbd5a7b552807370b60d4298/src/black/linegen.py#L1387-L1401

@MichaReiser MichaReiser added the help wanted Contributions especially welcome label Jul 15, 2023
@zanieb
Copy link
Member

zanieb commented Jul 15, 2023

Related discussion suggesting we may not want to always remove them(?) #4870

@MichaReiser MichaReiser added the formatter Related to the formatter label Jul 15, 2023
@MichaReiser
Copy link
Member Author

MichaReiser commented Jul 15, 2023

Related discussion suggesting we may not want to always remove them(?) #4870

Oh thanks. Our formatter preserves parentheses for nested expression but removes them in if/for/... headers. I should have stated this more clearly

charliermarsh added a commit that referenced this issue Jul 29, 2023
## Summary

Closes #5781

## Test Plan

Added cases to
`crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/named_expr.py`
one-by-one and adjusted the condition as needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants