Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
Fixes bug in action.bind()
elijahbenizzyf7141519ff6a
diff --git a/burr/core/action.py b/burr/core/action.pyindex 60aa979f..e2743483 100644--- a/burr/core/action.py+++ b/burr/core/action.py@@ -1011,6 +1011,18 @@ class FunctionRepresentingAction(Protocol[C]):...+def copy_func(f: types.FunctionType) -> types.FunctionType:+ """Copies a function. This is used internally to bind parameters to a function+ so we don't accidentally overwrite them++ :param f:+ :return:+ """+ fn = types.FunctionType(f.__code__, f.__globals__, f.__name__, f.__defaults__, f.__closure__)+ fn.__dict__.update(f.__dict__)+ return fn++def bind(self: FunctionRepresentingAction, **kwargs: Any) -> FunctionRepresentingAction:"""Binds an action to the given parameters. This is functionally equivalent tofunctools.partial, but is more explicit and is meant to be used in the API. This only works with@@ -1028,6 +1040,7 @@ def bind(self: FunctionRepresentingAction, **kwargs: Any) -> FunctionRepresentin:param kwargs: The keyword arguments to bind:return: The decorated function with the given parameters bound"""+ self = copy_func(self) # we have to bind to a copy of the function, otherwise it will overrideself.action_function = self.action_function.with_params(**kwargs)return self@@ -1092,6 +1105,7 @@ def streaming_action("""def wrapped(fn) -> FunctionRepresentingAction:+ fn = copy_func(fn)setattr(fn,FunctionBasedAction.ACTION_FUNCTION,diff --git a/tests/core/test_action.py b/tests/core/test_action.pyindex 499f98d0..b623b598 100644--- a/tests/core/test_action.py+++ b/tests/core/test_action.py@@ -236,6 +236,34 @@ def test_function_based_action_with_inputs():assert result == {"output_variable": 111}+def test_function_based_action_multiple_binds():+ # This ensures that bind doesn't impact the original function+ @action(reads=["input_variable"], writes=["output_variable"])+ def my_action(state: State, bound_input: int, unbound_input: int) -> Tuple[dict, State]:+ res = state["input_variable"] + bound_input + unbound_input+ return {"output_variable": res}, state.update(output_variable=res)++ # The binding has to happen before the actions are created+ # Otherwise it'll get the right value+ # This simulates how the ApplicationBuilder creates them+ bound_1 = my_action.bind(bound_input=10)+ bound_2 = my_action.bind(bound_input=20)++ fn_based_action = cast(SingleStepAction, create_action(bound_1, name="my_action"))+ fn_based_action_2 = cast(SingleStepAction, create_action(bound_2, name="my_action"))+ assert fn_based_action.inputs == (["unbound_input"], [])+ result, state = fn_based_action.run_and_update(State({"input_variable": 1}), unbound_input=100)+ assert state.get_all() == {"input_variable": 1, "output_variable": 111}+ assert result == {"output_variable": 111}++ assert fn_based_action_2.inputs == (["unbound_input"], [])+ result, state = fn_based_action_2.run_and_update(+ State({"input_variable": 1}), unbound_input=100+ )+ assert state.get_all() == {"input_variable": 1, "output_variable": 121}+ assert result == {"output_variable": 121}++def test_function_based_action_with_defaults():@action(reads=["input_variable"], writes=["output_variable"])def my_action(