fix(formulas): resolve Python 3.6 legacy AST compatibility, cycle exception propagation, and safe zero-division handling
Some checks failed
Build, Test, and Deploy / build-and-deploy (push) Failing after 29s
Some checks failed
Build, Test, and Deploy / build-and-deploy (push) Failing after 29s
This commit is contained in:
parent
4def894801
commit
6a7f566836
2 changed files with 32 additions and 7 deletions
|
|
@ -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__}')
|
||||
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
Loading…
Reference in a new issue