Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Fixes regression in which builtins were detected as variables for expr transition

elijahbenizzyb1da4f7d2a06
diff --git a/burr/core/action.py b/burr/core/action.py
index 7014254e..f9d09699 100644
--- a/burr/core/action.py
+++ b/burr/core/action.py
@@ -1,5 +1,6 @@
import abc
import ast
+import builtins
import copy
import inspect
import sys
@@ -204,6 +205,7 @@ class Condition(Function):
:return: A condition that evaluates the given expression
"""
tree = ast.parse(expr, mode="eval")
+ all_builtins = builtins.__dict__
# Visitor class to collect variable names
class NameVisitor(ast.NodeVisitor):
@@ -211,7 +213,8 @@ class Condition(Function):
self.names = set()
def visit_Name(self, node):
- self.names.add(node.id)
+ if node.id not in all_builtins:
+ self.names.add(node.id)
# Visit the nodes and collect variable names
visitor = NameVisitor()
@@ -261,6 +264,9 @@ class Condition(Function):
name = f"{', '.join(f'{key}={value}' for key, value in sorted(kwargs.items()))}"
return Condition(keys, condition_func, name=name)
+ def __repr__(self):
+ return f"condition: {self._name}"
+
@classmethod
@property
def default(self) -> "Condition":
@@ -786,6 +792,7 @@ class AsyncStreamingResultContainer(typing.AsyncIterator[dict]):
def pass_through(results: dict, final_state: State) -> "AsyncStreamingResultContainer":
"""Creates a streaming result container that just passes through the given results.
This is not a public facing API."""
+
async def just_results() -> AsyncGeneratorReturnType:
yield results, final_state
diff --git a/tests/core/test_action.py b/tests/core/test_action.py
index 02747163..5647b16b 100644
--- a/tests/core/test_action.py
+++ b/tests/core/test_action.py
@@ -120,8 +120,8 @@ def test_condition_expr():
def test_condition_expr_complex():
- cond = Condition.expr("foo == 'bar' and baz == 'qux'")
- assert cond.name == "foo == 'bar' and baz == 'qux'"
+ cond = Condition.expr("foo == 'bar' and len(baz) == 3")
+ assert cond.name == "foo == 'bar' and len(baz) == 3"
assert sorted(cond.reads) == ["baz", "foo"]
assert cond.run(State({"foo": "bar", "baz": "qux"})) == {Condition.KEY: True}
assert cond.run(State({"foo": "baz", "baz": "qux"})) == {Condition.KEY: False}