Unverified Commit 858225d3 authored by Rishav Kundu's avatar Rishav Kundu Committed by GitHub
Browse files

Strip redundant parentheses from assignment exprs (#1906)

Fixes #1656
parent b06cd156
Loading
Loading
Loading
Loading
+6 −4
Changes for src/black/__init__.py: 6 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -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:
+2 −2
Changes for tests/data/pep_572.py: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -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
+38 −0
Changes for tests/data/pep_572_remove_parens.py: 38 added lines, 0 removed lines.
Original line number Diff line number Diff line
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))
+0 −2
Changes for tests/data/remove_parens.py: 0 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -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
+9 −0
Changes for tests/test_black.py: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -263,6 +263,15 @@ def test_pep_572(self) -> None:
        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)