From a835141dfb73469fce26c15df740bba6af85a6f0 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Tue, 14 Apr 2020 17:07:44 -0700 Subject: [PATCH] Fix literal attribute access on Python 3.6 and 3.7 Fixes #50 --- lib/astunparse/unparser.py | 5 ++++- tests/common.py | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/astunparse/unparser.py b/lib/astunparse/unparser.py index 0ef6fd8..829835d 100644 --- a/lib/astunparse/unparser.py +++ b/lib/astunparse/unparser.py @@ -708,7 +708,10 @@ def _Attribute(self,t): # Special case: 3.__abs__() is a syntax error, so if t.value # is an integer literal then we need to either parenthesize # it or add an extra space to get 3 .__abs__(). - if isinstance(t.value, getattr(ast, 'Constant', getattr(ast, 'Num', None))) and isinstance(t.value.n, int): + ast_Num = getattr(ast, 'Num', None) + ast_Constant = getattr(ast, 'Constant', None) + if ((ast_Num and isinstance(t.value, ast_Num) and isinstance(t.value.n, int)) + or (ast_Constant and isinstance(t.value, ast_Constant) and isinstance(t.value.value, int))): self.write(" ") self.write(".") self.write(t.attr) diff --git a/tests/common.py b/tests/common.py index 95b9755..c8db903 100644 --- a/tests/common.py +++ b/tests/common.py @@ -215,7 +215,6 @@ def test_unary_parens(self): self.check_roundtrip("not True or False") self.check_roundtrip("True or not False") - @unittest.skipUnless(sys.version_info < (3, 6), "Only works for Python < 3.6") def test_integer_parens(self): self.check_roundtrip("3 .__abs__()")