Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3eb0290

Browse files
committed
Add parentheses around numeric literals, to avoid turning 3 .bit_length() into 3.bit_length().
1 parent 82c8d93 commit 3eb0290

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

Demo/parser/test_unparse.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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")

Demo/parser/unparse.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff 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("[")

0 commit comments

Comments
 (0)