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

[conversions] Convert negative literals to use unary minus #9

Merged
merged 1 commit into from
Jul 1, 2016
Merged
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
12 changes: 10 additions & 2 deletions typed_ast/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def _copy_attributes(new_value, old_value):

class _AST2To3(ast27.NodeTransformer):
# note: None, True, and False are *not* translated into NameConstants.
# note: Negative numeric literals are not converted to use unary -

def __init__(self):
pass

Expand Down Expand Up @@ -212,3 +210,13 @@ def visit_Str(self, s):
return ast35.Bytes(s.s)
else:
return ast35.Str(s.s)

def visit_Num(self, n):
new = self.generic_visit(n)
if new.n < 0:
# Python 3 uses a unary - operator for negative literals.
new.n = -new.n
return ast35.UnaryOp(op=ast35.USub(),
operand=new)
else:
return new