From 84f7ba686a2b27e302ea6eae29d7dc9d0023a2a0 Mon Sep 17 00:00:00 2001 From: Rishav Kundu Date: Tue, 5 Jan 2021 22:20:34 +0530 Subject: [PATCH] Strip redundant parentheses from assignment exprs Addresses #1656 --- src/black/__init__.py | 8 ++++---- tests/data/pep_572.py | 4 ++-- tests/data/remove_parens.py | 7 +++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 07251d55fb5..a81f35acbc1 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -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): @@ -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 diff --git a/tests/data/pep_572.py b/tests/data/pep_572.py index 637b3bb38c6..c6867f26258 100644 --- a/tests/data/pep_572.py +++ b/tests/data/pep_572.py @@ -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] @@ -43,5 +43,5 @@ def foo(answer: (p := 42) = 5): while x := f(x): pass -while (x := f(x)): +while x := f(x): pass diff --git a/tests/data/remove_parens.py b/tests/data/remove_parens.py index afc34010c30..7054467c9ab 100644 --- a/tests/data/remove_parens.py +++ b/tests/data/remove_parens.py @@ -54,6 +54,9 @@ def example7(): def example8(): return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) +def example9(): + if (foo := 0): + pass # output x = 1 @@ -142,3 +145,7 @@ def example7(): def example8(): return None + +def example9(): + if foo := 0: + pass