From: Rishav Kundu Date: Sun, 28 Feb 2021 01:20:23 +0000 (+0530) Subject: Strip redundant parentheses from assignment exprs (#1906) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/858225d34dc49ca353f9e573dd82dd5845766115 Strip redundant parentheses from assignment exprs (#1906) Fixes #1656 --- diff --git a/src/black/__init__.py b/src/black/__init__.py index 6919468..a1d16d9 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -5370,10 +5370,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): @@ -5545,6 +5542,7 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool: Returns whether the node should itself be wrapped in invisible parentheses. """ + if ( node.type != syms.atom or is_empty_tuple(node) @@ -5554,6 +5552,10 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool: ): return False + if is_walrus_assignment(node): + if parent.type in [syms.annassign, syms.expr_stmt]: + return False + first = node.children[0] last = node.children[-1] if first.type == token.LPAR and last.type == token.RPAR: diff --git a/tests/data/pep_572.py b/tests/data/pep_572.py index 637b3bb..c6867f2 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 @@ foo(c=(b := 2), a=1) while x := f(x): pass -while (x := f(x)): +while x := f(x): pass diff --git a/tests/data/pep_572_remove_parens.py b/tests/data/pep_572_remove_parens.py new file mode 100644 index 0000000..04cc75b --- /dev/null +++ b/tests/data/pep_572_remove_parens.py @@ -0,0 +1,38 @@ +if (foo := 0): + pass + +if (foo := 1): + pass + +if (y := 5 + 5): + pass + +y = (x := 0) + +y += (x := 0) + +(y := 5 + 5) + +test: int = (test2 := 2) + +a, b = (test := (1, 2)) + +# output +if foo := 0: + pass + +if foo := 1: + pass + +if y := 5 + 5: + pass + +y = (x := 0) + +y += (x := 0) + +(y := 5 + 5) + +test: int = (test2 := 2) + +a, b = (test := (1, 2)) diff --git a/tests/data/remove_parens.py b/tests/data/remove_parens.py index afc3401..abd5f71 100644 --- a/tests/data/remove_parens.py +++ b/tests/data/remove_parens.py @@ -54,7 +54,6 @@ def example7(): def example8(): return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) - # output x = 1 x = 1.2 @@ -141,4 +140,3 @@ def example7(): def example8(): return None - diff --git a/tests/test_black.py b/tests/test_black.py index 5d14ced..9c3cc64 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -263,6 +263,15 @@ class BlackTestCase(BlackBaseTestCase): if sys.version_info >= (3, 8): black.assert_equivalent(source, actual) + @patch("black.dump_to_file", dump_to_stderr) + def test_pep_572_remove_parens(self) -> None: + source, expected = read_data("pep_572_remove_parens") + actual = fs(source) + self.assertFormatEqual(expected, actual) + black.assert_stable(source, actual, DEFAULT_MODE) + if sys.version_info >= (3, 8): + black.assert_equivalent(source, actual) + def test_pep_572_version_detection(self) -> None: source, _ = read_data("pep_572") root = black.lib2to3_parse(source)