From 820f38708fd41a1b992716b1f65c9b0656f589d0 Mon Sep 17 00:00:00 2001 From: David Szotten Date: Fri, 14 Aug 2020 17:17:56 +0100 Subject: [PATCH] fix unary op detection (#1600) --- src/black/__init__.py | 20 ++++++++++---------- tests/data/percent_precedence.py | 2 ++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index b660e47..391233e 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -3297,12 +3297,12 @@ class StringParenStripper(StringTransformer): # if the leaves in the parsed string include a PERCENT, we need to # make sure the initial LPAR is NOT preceded by an operator with # higher or equal precedence to PERCENT - if ( - is_valid_index(idx - 2) - and token.PERCENT in {leaf.type for leaf in LL[idx - 1 : next_idx]} - and ( + if is_valid_index(idx - 2): + # mypy can't quite follow unless we name this + before_lpar = LL[idx - 2] + if token.PERCENT in {leaf.type for leaf in LL[idx - 1 : next_idx]} and ( ( - LL[idx - 2].type + before_lpar.type in { token.STAR, token.AT, @@ -3318,12 +3318,12 @@ class StringParenStripper(StringTransformer): ) or ( # only unary PLUS/MINUS - not is_valid_index(idx - 3) - and (LL[idx - 2].type in {token.PLUS, token.MINUS}) + before_lpar.parent + and before_lpar.parent.type == syms.factor + and (before_lpar.type in {token.PLUS, token.MINUS}) ) - ) - ): - continue + ): + continue # Should be followed by a non-empty RPAR... if ( diff --git a/tests/data/percent_precedence.py b/tests/data/percent_precedence.py index 44d30f1..b895443 100644 --- a/tests/data/percent_precedence.py +++ b/tests/data/percent_precedence.py @@ -12,6 +12,7 @@ b + ("" % a) -("" % a) b - ("" % a) +b + -("" % a) ~("" % a) 2 ** ("" % a) await ("" % a) @@ -32,6 +33,7 @@ b(("" % a)) b + "" % a -("" % a) b - "" % a +b + -("" % a) ~("" % a) 2 ** ("" % a) await ("" % a) -- 2.39.5