Skip to content

Commit

Permalink
fix testing for 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Jul 2, 2024
1 parent 68bb752 commit b02d1bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion qlasskit/ast2ast/constantfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
19 changes: 16 additions & 3 deletions test/test_ast2ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"
Expand All @@ -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):
Expand All @@ -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))

0 comments on commit b02d1bb

Please sign in to comment.