Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
9 changes: 6 additions & 3 deletions 9 Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,18 +1190,21 @@ def visit_Tuple(self, node):

unop = {"Invert": "~", "Not": "not", "UAdd": "+", "USub": "-"}
unop_precedence = {
"~": _Precedence.FACTOR,
"not": _Precedence.NOT,
"~": _Precedence.FACTOR,
"+": _Precedence.FACTOR,
"-": _Precedence.FACTOR
"-": _Precedence.FACTOR,
}

def visit_UnaryOp(self, node):
operator = self.unop[node.op.__class__.__name__]
operator_precedence = self.unop_precedence[operator]
with self.require_parens(operator_precedence, node):
self.write(operator)
self.write(" ")
# factor prefixes (+, -, ~) shouldn't be seperated
# from the value they belong, (e.g: +1 instead of + 1)
if operator_precedence is not _Precedence.FACTOR:
self.write(" ")
self.set_precedence(operator_precedence, node.operand)
self.traverse(node.operand)

Expand Down
8 changes: 7 additions & 1 deletion 8 Lib/test/test_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def test_simple_expressions_parens(self):
self.check_src_roundtrip("(1 + 2) / 3")
self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2)")
self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2) ** 2")
self.check_src_roundtrip("~ x")
self.check_src_roundtrip("~x")
self.check_src_roundtrip("x and y")
self.check_src_roundtrip("x and y and z")
self.check_src_roundtrip("x and (y and x)")
Expand Down Expand Up @@ -401,6 +401,12 @@ def test_docstrings_negative_cases(self):
self.check_ast_roundtrip(src)
self.check_src_dont_roundtrip(src)

def test_unary_op_factor(self):
for prefix in ("+", "-", "~"):
self.check_src_roundtrip(f"{prefix}1")
for prefix in ("not",):
self.check_src_roundtrip(f"{prefix} 1")

class DirectoryTestCase(ASTTestCase):
"""Test roundtrip behaviour on all files in Lib and Lib/test."""

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.