From 6a7f56683602610c46d84198f8c4b733ed25b3e6 Mon Sep 17 00:00:00 2001 From: Goose Date: Sun, 27 Sep 2026 07:28:39 +0000 Subject: [PATCH] fix(formulas): resolve Python 3.6 legacy AST compatibility, cycle exception propagation, and safe zero-division handling --- custom_objects/formula_engine.py | 34 +++++++++++++++++++++++++------- tests/test_formula_engine.py | 5 +++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/custom_objects/formula_engine.py b/custom_objects/formula_engine.py index b572868..bd181a5 100644 --- a/custom_objects/formula_engine.py +++ b/custom_objects/formula_engine.py @@ -27,12 +27,21 @@ ALLOWED_AST_NODES = ( ast.And, ast.Or, ast.Not, - ast.Constant, ast.Name, ast.Attribute, ast.Call, ast.Load, ) +if hasattr(ast, 'Constant'): + ALLOWED_AST_NODES = ALLOWED_AST_NODES + (ast.Constant,) +if hasattr(ast, 'Num'): + ALLOWED_AST_NODES = ALLOWED_AST_NODES + (ast.Num,) +if hasattr(ast, 'Str'): + ALLOWED_AST_NODES = ALLOWED_AST_NODES + (ast.Str,) +if hasattr(ast, 'Bytes'): + ALLOWED_AST_NODES = ALLOWED_AST_NODES + (ast.Bytes,) +if hasattr(ast, 'NameConstant'): + ALLOWED_AST_NODES = ALLOWED_AST_NODES + (ast.NameConstant,) def fn_if(condition, true_val, false_val): return true_val if condition else false_val @@ -162,10 +171,7 @@ def topological_sort_formulas(formula_dict): graph = {name: [] for name in formula_dict} for name, expr in formula_dict.items(): - try: - ids = extract_formula_identifiers(expr) - except Exception: - ids = set() + ids = extract_formula_identifiers(expr) # Dependencies that are also formula fields f_deps = {dep for dep in ids if dep in formula_dict and dep != name} deps[name] = f_deps @@ -224,6 +230,18 @@ class ASTFormulaEvaluator(ast.NodeVisitor): def visit_Constant(self, node): return node.value + def visit_Num(self, node): + return node.n + + def visit_Str(self, node): + return node.s + + def visit_Bytes(self, node): + return node.s + + def visit_NameConstant(self, node): + return node.value + def visit_Name(self, node): name = node.id if name.upper() in BUILTIN_FUNCTIONS: @@ -259,10 +277,12 @@ class ASTFormulaEvaluator(ast.NodeVisitor): elif isinstance(node.op, ast.Mult): return (left or 0) * (right or 0) elif isinstance(node.op, ast.Div): - if right == 0: - raise ZeroDivisionError('Division by zero in formula evaluation.') + if right == 0 or right == 0.0 or right is None: + return 0 return (left or 0) / right elif isinstance(node.op, ast.Mod): + if right == 0 or right == 0.0 or right is None: + return 0 return (left or 0) % right raise ValidationError(f'Unsupported binary operator: {type(node.op).__name__}') diff --git a/tests/test_formula_engine.py b/tests/test_formula_engine.py index 8e1d0ad..992abf1 100644 --- a/tests/test_formula_engine.py +++ b/tests/test_formula_engine.py @@ -17,6 +17,11 @@ class TestFormulaEngine: result = evaluate_formula(ctx, 'amount * (1 + tax_rate)', return_type='currency') assert result == 105.0 + # Safe division by zero handling (Salesforce parity) + assert evaluate_formula(ctx, 'amount / 2', return_type='number') == 50.0 + assert evaluate_formula(ctx, 'amount / 0', return_type='number') == 0 + assert evaluate_formula(ctx, 'amount % 0', return_type='number') == 0 + def test_logical_and_conditional_functions(self): ctx = {'amount': 50000, 'stage': 'Closed Won'} res_if = evaluate_formula(ctx, 'IF(amount >= 50000, "Enterprise", "SMB")', return_type='text')