Skip to content

Commit

Permalink
fix unary op detection (#1600)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszotten authored Aug 14, 2020
1 parent d1ad873 commit 820f387
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3297,12 +3297,12 @@ def do_match(self, line: Line) -> TMatchResult:
# 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,
Expand All @@ -3318,12 +3318,12 @@ def do_match(self, line: Line) -> TMatchResult:
)
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 (
Expand Down
2 changes: 2 additions & 0 deletions tests/data/percent_precedence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
b + ("" % a)
-("" % a)
b - ("" % a)
b + -("" % a)
~("" % a)
2 ** ("" % a)
await ("" % a)
Expand All @@ -32,6 +33,7 @@
b + "" % a
-("" % a)
b - "" % a
b + -("" % a)
~("" % a)
2 ** ("" % a)
await ("" % a)
Expand Down

0 comments on commit 820f387

Please sign in to comment.