summaryrefslogtreecommitdiffstats
path: root/Lib/ast.py
diff options
context:
space:
mode:
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>2020-05-16 21:46:11 (GMT)
committerGitHub <noreply@github.com>2020-05-16 21:46:11 (GMT)
commitce4a753dcb3eef3d68e892a6515490b1aa219651 (patch)
tree62557bc0b0fdc51f93363dd3b68a8fb2e71908af /Lib/ast.py
parentd5b3f6b7f9fc74438009af63f1de01bd77be9385 (diff)
downloadcpython-ce4a753dcb3eef3d68e892a6515490b1aa219651.zip
cpython-ce4a753dcb3eef3d68e892a6515490b1aa219651.tar.gz
cpython-ce4a753dcb3eef3d68e892a6515490b1aa219651.tar.bz2
bpo-38870: Do not separate factor prefixes in ast.unparse (GH-20133)
Diffstat (limited to 'Lib/ast.py')
-rw-r--r--Lib/ast.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/Lib/ast.py b/Lib/ast.py
index 7a43581..1de37b9 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -1190,10 +1190,10 @@ class _Unparser(NodeVisitor):
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):
@@ -1201,7 +1201,10 @@ class _Unparser(NodeVisitor):
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)