Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit e54f812

Browse files
authored
Convert negative literals to use unary minus (#9)
Python 2 represents negative numeric literals as Num(n=-NUMBER), but Python 3 represents them as UnaryOp(op=USub(), operand=Num(n=NUMBER)). This commit makes the conversions module actually make this change. Fixes python/mypy#1788.
1 parent 25b7b1f commit e54f812

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

typed_ast/conversions.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ def _copy_attributes(new_value, old_value):
2929

3030
class _AST2To3(ast27.NodeTransformer):
3131
# note: None, True, and False are *not* translated into NameConstants.
32-
# note: Negative numeric literals are not converted to use unary -
33-
3432
def __init__(self):
3533
pass
3634

@@ -212,3 +210,13 @@ def visit_Str(self, s):
212210
return ast35.Bytes(s.s)
213211
else:
214212
return ast35.Str(s.s)
213+
214+
def visit_Num(self, n):
215+
new = self.generic_visit(n)
216+
if new.n < 0:
217+
# Python 3 uses a unary - operator for negative literals.
218+
new.n = -new.n
219+
return ast35.UnaryOp(op=ast35.USub(),
220+
operand=new)
221+
else:
222+
return new

0 commit comments

Comments
 (0)