Skip to content

Commit

Permalink
Strip redundant parentheses from assignment exprs
Browse files Browse the repository at this point in the history
Addresses psf#1656
  • Loading branch information
xrisk committed Feb 23, 2021
1 parent e1c86f9 commit 84f7ba6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5368,10 +5368,7 @@ def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
check_lpar = True

if check_lpar:
if is_walrus_assignment(child):
pass

elif child.type == syms.atom:
if child.type == syms.atom:
if maybe_make_parens_invisible_in_atom(child, parent=node):
wrap_in_parentheses(node, child, visible=False)
elif is_one_tuple(child):
Expand Down Expand Up @@ -5549,6 +5546,9 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
or is_one_tuple(node)
or (is_yield(node) and parent.type != syms.expr_stmt)
or max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
or parent.type == syms.expr_stmt
and parent.children[1].type == token.EQUAL
and is_walrus_assignment(node)
):
return False

Expand Down
4 changes: 2 additions & 2 deletions tests/data/pep_572.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(a := a)
if (match := pattern.search(data)) is None:
pass
if (match := pattern.search(data)):
if match := pattern.search(data):
pass
[y := f(x), y ** 2, y ** 3]
filtered_data = [y for x in data if (y := f(x)) is None]
Expand Down Expand Up @@ -43,5 +43,5 @@ def foo(answer: (p := 42) = 5):

while x := f(x):
pass
while (x := f(x)):
while x := f(x):
pass
7 changes: 7 additions & 0 deletions tests/data/remove_parens.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def example7():
def example8():
return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

def example9():
if (foo := 0):
pass

# output
x = 1
Expand Down Expand Up @@ -142,3 +145,7 @@ def example7():
def example8():
return None


def example9():
if foo := 0:
pass

0 comments on commit 84f7ba6

Please sign in to comment.