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.

Addresses default inputs case

elijahbenizzy2d9df1755fa3
diff --git a/burr/core/action.py b/burr/core/action.py
index b3593dbf..d10cd024 100644
--- a/burr/core/action.py
+++ b/burr/core/action.py
@@ -380,11 +380,13 @@ class FunctionBasedAction(SingleStepAction):
@property
def inputs(self) -> list[str]:
- return [
- param
- for param in inspect.signature(self._fn).parameters
- if param != "state" and param not in self._bound_params
- ]
+ sig = inspect.signature(self._fn)
+ out = []
+ for param_name, param in sig.parameters.items():
+ if param_name != "state" and param_name not in self._bound_params:
+ if param.default is inspect.Parameter.empty:
+ out.append(param_name)
+ return out
def with_params(self, **kwargs: Any) -> "FunctionBasedAction":
"""Binds parameters to the function.
@@ -469,6 +471,11 @@ def action(reads: List[str], writes: List[str]) -> Callable[[Callable], Function
Note that, in the future, with typed state, we may not need this for
all cases.
+ If parameters are not bound, they will be interpreted as inputs and must
+ be passed in at runtime. If they have default values, they will *not* be
+ interpreted as inputs (all inputs are required to be present). They can
+ still be bound, however.
+
:param reads: Items to read from the state
:param writes: Items to write to the state
:return: The decorator to assign the function as an action
diff --git a/tests/core/test_action.py b/tests/core/test_action.py
index cee43c2c..682402df 100644
--- a/tests/core/test_action.py
+++ b/tests/core/test_action.py
@@ -182,6 +182,23 @@ def test_function_based_action_with_inputs():
assert result == {"output_variable": 111}
+def test_function_based_action_with_defaults():
+ @action(reads=["input_variable"], writes=["output_variable"])
+ def my_action(
+ state: State, bound_input: int, unbound_input: int, unbound_default_input: int = 1000
+ ) -> Tuple[dict, State]:
+ res = state["input_variable"] + bound_input + unbound_input + unbound_default_input
+ return {"output_variable": res}, state.update(output_variable=res)
+
+ fn_based_action: SingleStepAction = create_action(
+ my_action.bind(bound_input=10), 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": 1111}
+ assert result == {"output_variable": 1111}
+
+
def test_create_action_class_api():
raw_action = BasicAction()
created_action = create_action(raw_action, name="my_action")
diff --git a/tests/core/test_application.py b/tests/core/test_application.py
index edd05782..8391fefc 100644
--- a/tests/core/test_application.py
+++ b/tests/core/test_application.py
@@ -318,7 +318,7 @@ def test_app_step_with_inputs_missing():
state=State({"count": 0, "tracker": []}),
initial_step="counter",
)
- with pytest.raises(ValueError, match="Missing inputs"):
+ with pytest.raises(ValueError, match="Missing the following inputs"):
app.step(inputs={})
@@ -386,7 +386,7 @@ async def test_app_astep_with_inputs_missing():
state=State({"count": 0, "tracker": []}),
initial_step="counter_async",
)
- with pytest.raises(ValueError, match="Missing inputs"):
+ with pytest.raises(ValueError, match="Missing the following inputs"):
await app.astep(inputs={})