diff --git a/qlasskit/ast2ast/constantfolder.py b/qlasskit/ast2ast/constantfolder.py index f0846869..b5ecffae 100644 --- a/qlasskit/ast2ast/constantfolder.py +++ b/qlasskit/ast2ast/constantfolder.py @@ -51,7 +51,7 @@ def visit_Compare(self, node): }.get(type(node.ops[0])) if op: result = op(node.left.value, node.comparators[0].value) - return ast.Constant(value=result) + return ast.Constant(result) return node def visit_UnaryOp(self, node): diff --git a/test/test_ast2ast.py b/test/test_ast2ast.py index 07935721..dea72eae 100644 --- a/test/test_ast2ast.py +++ b/test/test_ast2ast.py @@ -21,6 +21,19 @@ from qlasskit.ast2ast import ASTRewriter, ConstantFolder +class AddKind(ast.NodeTransformer): + def visit_Constant(self, n): + n.kind = None + return n + +def add_kind(n): + if sys.version_info >= (3, 9): + return n + + return AddKind().visit(n) + + + class TestASTRewriter(unittest.TestCase): def setUp(self): @@ -37,7 +50,7 @@ def test_exponentiation_transformation(self, code, expected_code): tree = ast.parse(code) new_tree = self.rewriter.visit(tree) expected_tree = ast.parse(expected_code) - self.assertEqual(ast.dump(new_tree), ast.dump(expected_tree)) + self.assertEqual(ast.dump(add_kind(new_tree)), ast.dump(expected_tree)) def test_exponentiation_with_zero(self): code = "a = b ** 0" @@ -52,7 +65,7 @@ def test_exponentiation_with_zero(self): if sys.version_info >= (3, 9): expected_code = "a = 1" expected_tree = ast.parse(expected_code) - self.assertEqual(ast.dump(new_tree), ast.dump(expected_tree)) + self.assertEqual(ast.dump(add_kind(new_tree)), ast.dump(expected_tree)) class TestASTConstantFolder(unittest.TestCase): @@ -72,4 +85,4 @@ def test_expected_code(self, code, expected_code): tree = ast.parse(code) new_tree = self.rewriter.visit(tree) expected_tree = ast.parse(expected_code) - self.assertEqual(ast.dump(new_tree), ast.dump(expected_tree)) + self.assertEqual(ast.dump(add_kind(new_tree)), ast.dump(expected_tree))