Skip to content

Commit

Permalink
habndle list and tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Jul 1, 2024
1 parent 02d6943 commit 23198b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions qlasskit/ast2ast/constantfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion test/test_ast2ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
Expand Down

0 comments on commit 23198b4

Please sign in to comment.