Force parentheses between unary op and binary power. (#909)

This commit is contained in:
Brandt Bucher 2019-06-29 09:35:16 -07:00 committed by Jelle Zijlstra
parent 7d213c6d43
commit b073c9a4e9
3 changed files with 20 additions and 3 deletions

View File

@ -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.

View File

@ -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
@ -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]
@ -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]

View File

@ -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
@ -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