From 23198b4f44a9fcfcbdda0a94f389f13fb4aa4745 Mon Sep 17 00:00:00 2001 From: "Davide Gessa (dakk)" Date: Mon, 1 Jul 2024 19:29:43 +0200 Subject: [PATCH] habndle list and tuples --- qlasskit/ast2ast/constantfolder.py | 12 ++++++++++++ test/test_ast2ast.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/qlasskit/ast2ast/constantfolder.py b/qlasskit/ast2ast/constantfolder.py index 4389aa18..6ab330bf 100644 --- a/qlasskit/ast2ast/constantfolder.py +++ b/qlasskit/ast2ast/constantfolder.py @@ -121,3 +121,15 @@ def visit_IfExp(self, node): if isinstance(node.test, ast.Constant): return node.body if node.test.value else node.orelse return node + + def visit_List(self, node): + elts = [self.visit(elt) for elt in node.elts] + if all(isinstance(elt, ast.Constant) for elt in elts): + return ast.Constant(value=[elt.value for elt in elts]) + return ast.List(elts=elts, ctx=node.ctx) + + def visit_Tuple(self, node): + elts = [self.visit(elt) for elt in node.elts] + if all(isinstance(elt, ast.Constant) for elt in elts): + return ast.Constant(value=tuple(elt.value for elt in elts)) + return ast.Tuple(elts=elts, ctx=node.ctx) \ No newline at end of file diff --git a/test/test_ast2ast.py b/test/test_ast2ast.py index 4dd778bd..6c17c7ec 100644 --- a/test/test_ast2ast.py +++ b/test/test_ast2ast.py @@ -63,7 +63,7 @@ def setUp(self): [ ("a + (13 - 12 + 1)", "a + 2"), # ( "a + 13 - 12 + 1", "a + 2" ), - # ( "a + len([12])", "a + 1" ), + ( "a + len([12])", "a + 1" ), ("if True: a \nelse: b", "a"), ("a if False else b", "b"), ]