Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force parentheses between unary op and binary power. #909

Merged
merged 4 commits into from
Jun 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,19 @@ def visit_atom(self, node: Node) -> Iterator[Line]:
node.children[2].value = ""
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.
Expand Down
8 changes: 5 additions & 3 deletions tests/data/expression.diff
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
True
False
1
@@ -29,62 +29,83 @@
@@ -29,63 +29,84 @@
~great
+value
-1
~int and not v1 ^ 123 + v2 | True
(~int) and (not ((v1 ^ (123 + v2)) | True))
-+really ** -confusing ** ~operator ** -precedence
-flags & ~ select.EPOLLIN and waiters.write_task is not None
++(really ** -(confusing ** ~(operator ** -precedence)))
+flags & ~select.EPOLLIN and waiters.write_task is not None
lambda arg: None
lambda a=True: a
Expand Down Expand Up @@ -116,7 +118,7 @@
call(**self.screen_kwargs)
call(b, **self.screen_kwargs)
lukasz.langa.pl
@@ -93,23 +114,25 @@
@@ -94,23 +115,25 @@
1.0 .real
....__class__
list[str]
Expand Down Expand Up @@ -147,7 +149,7 @@
slice[0:1:2]
slice[:]
slice[:-1]
@@ -133,113 +156,171 @@
@@ -134,113 +157,171 @@
numpy[-(c + 1) :, d]
numpy[:, l[-2]]
numpy[:, ::-1]
Expand Down
2 changes: 2 additions & 0 deletions tests/data/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
-1
~int and not v1 ^ 123 + v2 | True
(~int) and (not ((v1 ^ (123 + v2)) | True))
+really ** -confusing ** ~operator ** -precedence
flags & ~ select.EPOLLIN and waiters.write_task is not None
lambda arg: None
lambda a=True: a
Expand Down Expand Up @@ -280,6 +281,7 @@ async def f():
-1
~int and not v1 ^ 123 + v2 | True
(~int) and (not ((v1 ^ (123 + v2)) | True))
+(really ** -(confusing ** ~(operator ** -precedence)))
flags & ~select.EPOLLIN and waiters.write_task is not None
lambda arg: None
lambda a=True: a
Expand Down