File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -84,6 +84,9 @@ def test_unary_parens(self):
8484 self .check_roundtrip ("not True or False" )
8585 self .check_roundtrip ("True or not False" )
8686
87+ def test_integer_parens (self ):
88+ self .check_roundtrip ("3 .__abs__()" )
89+
8790 def test_chained_comparisons (self ):
8891 self .check_roundtrip ("1 < 4 <= 5" )
8992 self .check_roundtrip ("a is b is c is not d" )
Original file line number Diff line number Diff line change @@ -302,16 +302,17 @@ def _Repr(self, t):
302302 self .write ("`" )
303303
304304 def _Num (self , t ):
305- # There are no negative numeric literals in Python; however,
306- # some optimizations produce a negative Num in the AST. Add
307- # parentheses to avoid turning (-1)**2 into -1**2.
308- strnum = repr (t .n )
309- if strnum .startswith ("-" ):
310- self .write ("(" )
311- self .write (strnum )
312- self .write (")" )
313- else :
314- self .write (strnum )
305+ # Add parentheses around numeric literals to avoid:
306+ #
307+ # (1) turning (-1)**2 into -1**2, and
308+ # (2) turning 3 .__abs__() into 3.__abs__()
309+ #
310+ # For (1), note that Python doesn't actually have negative
311+ # numeric literals, but (at least in Python 2.x) there's a CST
312+ # transformation that can produce negative Nums in the AST.
313+ self .write ("(" )
314+ self .write (repr (t .n ))
315+ self .write (")" )
315316
316317 def _List (self , t ):
317318 self .write ("[" )
You can’t perform that action at this time.
0 commit comments