Skip to content

Commit

Permalink
Remove parentheses around some walrus operators (#6173)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
charliermarsh authored Jul 29, 2023
1 parent 1d7ad30 commit 5d9814d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,28 @@
y0 = (y1 := f(x))

f(x:=y, z=True)

assert (x := 1)


def f():
return (x := 1)


for x in (y := [1, 2, 3]):
pass

async for x in (y := [1, 2, 3]):
pass

del (x := 1)

try:
pass
except (e := Exception):
if x := 1:
pass

(x := 1)

(x := 1) + (y := 2)
18 changes: 16 additions & 2 deletions crates/ruff_python_formatter/src/expression/expr_named_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,25 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
impl NeedsParentheses for ExprNamedExpr {
fn needs_parentheses(
&self,
_parent: AnyNodeRef,
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
if parent.is_stmt_ann_assign()
|| parent.is_stmt_assign()
|| parent.is_stmt_aug_assign()
|| parent.is_stmt_assert()
|| parent.is_stmt_return()
|| parent.is_except_handler_except_handler()
|| parent.is_with_item()
|| parent.is_stmt_delete()
|| parent.is_stmt_for()
|| parent.is_stmt_async_for()
{
OptionalParentheses::Always
} else {
OptionalParentheses::Never
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ def f():
@relaxed_decorator[0]
def f():
...
@@ -13,8 +12,10 @@
...
-@extremely_long_variable_name_that_doesnt_fit := complex.expression(
- with_long="arguments_value_that_wont_fit_at_the_end_of_the_line"
+@(
+ extremely_long_variable_name_that_doesnt_fit := complex.expression(
+ with_long="arguments_value_that_wont_fit_at_the_end_of_the_line"
+ )
)
def f():
...
```

## Ruff Output
Expand All @@ -64,10 +51,8 @@ def f():
...
@(
extremely_long_variable_name_that_doesnt_fit := complex.expression(
with_long="arguments_value_that_wont_fit_at_the_end_of_the_line"
)
@extremely_long_variable_name_that_doesnt_fit := complex.expression(
with_long="arguments_value_that_wont_fit_at_the_end_of_the_line"
)
def f():
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ if (
y0 = (y1 := f(x))
f(x:=y, z=True)
assert (x := 1)
def f():
return (x := 1)
for x in (y := [1, 2, 3]):
pass
async for x in (y := [1, 2, 3]):
pass
del (x := 1)
try:
pass
except (e := Exception):
if x := 1:
pass
(x := 1)
(x := 1) + (y := 2)
```

## Output
Expand All @@ -32,6 +57,31 @@ if (
y0 = (y1 := f(x))
f(x := y, z=True)
assert (x := 1)
def f():
return (x := 1)
for x in (y := [1, 2, 3]):
pass
async for x in (y := [1, 2, 3]):
pass
del (x := 1)
try:
pass
except (e := Exception):
if x := 1:
pass
(x := 1)
(x := 1) + (y := 2)
```


Expand Down

0 comments on commit 5d9814d

Please sign in to comment.