From 18119d38466652ae818436cb497f601294ed4558 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 7 May 2019 09:49:50 -0400 Subject: [PATCH 1/1] Remove unnecessary parens around yield (#834) --- black.py | 8 ++++---- tests/data/expression.diff | 11 ++++++++--- tests/data/expression.py | 6 +++++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/black.py b/black.py index 9ecfbe1..cada4d0 100644 --- a/black.py +++ b/black.py @@ -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 diff --git a/tests/data/expression.diff b/tests/data/expression.diff index b26d931..adca2c8 100644 --- a/tests/data/expression.diff +++ b/tests/data/expression.diff @@ -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]) diff --git a/tests/data/expression.py b/tests/data/expression.py index 71cf1d9..b889bfc 100644 --- a/tests/data/expression.py +++ b/tests/data/expression.py @@ -169,6 +169,8 @@ mapping = { 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 @@ mapping = { def gen(): yield from outside_of_generator - a = (yield) + a = yield + b = yield + c = yield async def f(): -- 2.39.5