Commit 18119d38 authored by Jelle Zijlstra's avatar Jelle Zijlstra Committed by Łukasz Langa
Browse files

Remove unnecessary parens around yield (#834)

parent c2c2f720
Loading
Loading
Loading
Loading
+4 −4
Changes for black.py: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -2728,7 +2728,7 @@ def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
    for index, child in enumerate(list(node.children)):
        if check_lpar:
            if child.type == syms.atom:
                if maybe_make_parens_invisible_in_atom(child):
                if maybe_make_parens_invisible_in_atom(child, parent=node):
                    lpar = Leaf(token.LPAR, "")
                    rpar = Leaf(token.RPAR, "")
                    index = child.remove() or 0
@@ -2839,7 +2839,7 @@ def generate_ignored_nodes(leaf: Leaf) -> Iterator[LN]:
        container = container.next_sibling


def maybe_make_parens_invisible_in_atom(node: LN) -> bool:
def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
    """If it's safe, make the parens in the atom `node` invisible, recursively.

    Returns whether the node should itself be wrapped in invisible parentheses.
@@ -2849,7 +2849,7 @@ def maybe_make_parens_invisible_in_atom(node: LN) -> bool:
        node.type != syms.atom
        or is_empty_tuple(node)
        or is_one_tuple(node)
        or is_yield(node)
        or (is_yield(node) and parent.type != syms.expr_stmt)
        or max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
    ):
        return False
@@ -2861,7 +2861,7 @@ def maybe_make_parens_invisible_in_atom(node: LN) -> bool:
        first.value = ""  # type: ignore
        last.value = ""  # type: ignore
        if len(node.children) > 1:
            maybe_make_parens_invisible_in_atom(node.children[1])
            maybe_make_parens_invisible_in_atom(node.children[1], parent=parent)
        return False

    return True
+8 −3
Changes for tests/data/expression.diff: 8 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -147,7 +147,7 @@
 slice[0:1:2]
 slice[:]
 slice[:-1]
@@ -133,111 +156,169 @@
@@ -133,113 +156,171 @@
 numpy[-(c + 1) :, d]
 numpy[:, l[-2]]
 numpy[:, ::-1]
@@ -214,9 +214,14 @@
+
 def gen():
     yield from outside_of_generator
     a = (yield)
 
-    a = (yield)
-    b = ((yield))
-    c = (((yield)))
+    a = yield
+    b = yield
+    c = yield
+
 
 async def f():
     await some.complicated[0].call(with_args=(True or (1 is not 1)))
-print(* [] or [1])
+5 −1
Changes for tests/data/expression.py: 5 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -169,6 +169,8 @@
def gen():
    yield from outside_of_generator
    a = (yield)
    b = ((yield))
    c = (((yield)))

async def f():
    await some.complicated[0].call(with_args=(True or (1 is not 1)))
@@ -458,7 +460,9 @@ async def f():

def gen():
    yield from outside_of_generator
    a = (yield)
    a = yield
    b = yield
    c = yield


async def f():