Skip to content

Commit f05cc95

Browse files
authored
Merge pull request #483 from NVIDIA/fix/colang_2_dict_expression_evaluation
Fix dictionary expression evaluation bug
2 parents 84977a0 + afd3e00 commit f05cc95

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

nemoguardrails/colang/v2_x/runtime/eval.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,22 @@ def eval_expression(expr: str, context: dict) -> Any:
168168
functions=functions,
169169
names=expr_locals,
170170
)
171-
return s.eval(updated_expr)
171+
172+
result = s.eval(updated_expr)
173+
174+
# Assign back changed values to dictionary variables
175+
for var_name, val in expr_locals.items():
176+
if isinstance(val, AttributeDict):
177+
var_name = var_name[4:]
178+
global_var_name = f"_global_{var_name}"
179+
if global_var_name in context:
180+
context[global_var_name].clear()
181+
context[global_var_name].update(val)
182+
else:
183+
context[var_name].clear()
184+
context[var_name].update(val)
185+
186+
return result
172187
except Exception as e:
173188
raise ColangValueError(f"Error evaluating '{expr}'") from e
174189

tests/v2_x/test_slide_mechanics.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -918,5 +918,36 @@ def test_out_flow_variables():
918918
)
919919

920920

921+
def test_expression_evaluation():
922+
"""Test the different ways of expression evaluations."""
923+
924+
content = """
925+
flow main
926+
$dict = {"val": 2 + 3}
927+
start bot say number ($dict["val"])
928+
($dict.update({"val":10}))
929+
bot say number ($dict["val"] + 1)
930+
931+
flow bot say number $number
932+
await UtteranceBotAction(script="{$number}")
933+
"""
934+
935+
config = _init_state(content)
936+
state = run_to_completion(config, start_main_flow_event)
937+
assert is_data_in_events(
938+
state.outgoing_events,
939+
[
940+
{
941+
"type": "StartUtteranceBotAction",
942+
"script": "5",
943+
},
944+
{
945+
"type": "StartUtteranceBotAction",
946+
"script": "11",
947+
},
948+
],
949+
)
950+
951+
921952
if __name__ == "__main__":
922-
test_when_or_core_mechanics()
953+
test_expression_evaluation()

0 commit comments

Comments
 (0)