From 6dca5278a3159cb35e8b9076bf81eacd35c1f0a7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Langa?= Date: Mon, 28 Oct 2019 14:53:25 +0100 Subject: [PATCH] Fix regression: unexpected parentheses around non-mathematical powers This was caused by an overly liberal application of parentheses in #909 that fixed #646. Fixes #1041 --- black.py | 30 +++++++++++++++++------------- tests/data/function2.py | 6 ++++-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/black.py b/black.py index f6f687b..5e357ba 100644 --- a/black.py +++ b/black.py @@ -1717,19 +1717,6 @@ class LineGenerator(Visitor[Line]): self.current_line.append(node) yield from super().visit_default(node) - def visit_factor(self, node: Node) -> Iterator[Line]: - """Force parentheses between a unary op and a binary power: - - -2 ** 8 -> -(2 ** 8) - """ - child = node.children[1] - if child.type == syms.power and len(child.children) == 3: - lpar = Leaf(token.LPAR, "(") - rpar = Leaf(token.RPAR, ")") - index = child.remove() or 0 - node.insert_child(index, Node(syms.atom, [lpar, child, rpar])) - yield from self.visit_default(node) - def visit_INDENT(self, node: Node) -> Iterator[Line]: """Increase indentation level, maybe yield a line.""" # In blib2to3 INDENT never holds comments. @@ -1829,6 +1816,23 @@ class LineGenerator(Visitor[Line]): yield from self.line() yield from self.visit_default(leaf) + def visit_factor(self, node: Node) -> Iterator[Line]: + """Force parentheses between a unary op and a binary power: + + -2 ** 8 -> -(2 ** 8) + """ + _operator, operand = node.children + if ( + operand.type == syms.power + and len(operand.children) == 3 + and operand.children[1].type == token.DOUBLESTAR + ): + lpar = Leaf(token.LPAR, "(") + rpar = Leaf(token.RPAR, ")") + index = operand.remove() or 0 + node.insert_child(index, Node(syms.atom, [lpar, operand, rpar])) + yield from self.visit_default(node) + def __attrs_post_init__(self) -> None: """You are in a twisty little maze of passages.""" v = self.visit_stmt diff --git a/tests/data/function2.py b/tests/data/function2.py index f57a3f5..a6773d4 100644 --- a/tests/data/function2.py +++ b/tests/data/function2.py @@ -7,9 +7,10 @@ def f( result = ( CliRunner().invoke(black.main, [str(src1), str(src2), "--diff", "--check"]) ) + limited.append(-limited.pop()) # negate top return A( very_long_argument_name1=very_long_value_for_the_argument, - very_long_argument_name2=very_long_value_for_the_argument, + very_long_argument_name2=-very.long.value.for_the_argument, **kwargs, ) def g(): @@ -30,9 +31,10 @@ def f(a, **kwargs,) -> A: result = CliRunner().invoke( black.main, [str(src1), str(src2), "--diff", "--check"] ) + limited.append(-limited.pop()) # negate top return A( very_long_argument_name1=very_long_value_for_the_argument, - very_long_argument_name2=very_long_value_for_the_argument, + very_long_argument_name2=-very.long.value.for_the_argument, **kwargs, ) -- 2.39.5