From 3c56d93d732b0114b2b8b7804ee54fa108281e72 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Sat, 7 Jun 2025 20:14:25 +0200 Subject: [PATCH] added check for Constant, moved check for Str/Num --- slic/utils/eval.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/slic/utils/eval.py b/slic/utils/eval.py index ebe71ea7..9c8218ab 100644 --- a/slic/utils/eval.py +++ b/slic/utils/eval.py @@ -40,10 +40,6 @@ def arithmetic_eval(s): def ast_node_eval(node): if isinstance(node, ast.Expression): return ast_node_eval(node.body) - elif isinstance(node, ast.Str): - return node.s - elif isinstance(node, ast.Num): - return node.n elif isinstance(node, ast.BinOp): op = get_operator(node, BIN_OPS) left = ast_node_eval(node.left) @@ -53,6 +49,15 @@ def ast_node_eval(node): op = get_operator(node, UNARY_OPS) operand = ast_node_eval(node.operand) return op(operand) + # from >=3.8, Constant can replace Str/Num + # from >=3.14, Str/Num are deprecated and removed + # check Constant first then fall back to Str/Num for <3.8 + elif isinstance(node, ast.Constant): + return node.value + elif isinstance(node, ast.Str): + return node.s + elif isinstance(node, ast.Num): + return node.n else: tn = typename(node) raise ArithmeticEvalError(f"Unsupported node type {tn}")