|
19 | 19 | from pydantic import BaseModel, ConfigDict |
20 | 20 |
|
21 | 21 | import dspy.primitives.python_interpreter as python_interpreter |
22 | | -from dspy.primitives.code_interpreter import CodeExecutionError, CodeInterpreterError, FinalOutput |
| 22 | +from dspy.primitives.code_interpreter import CodeExecutionError, CodeInterpreterError, FinalOutput, _bind_interpreter |
23 | 23 | from dspy.primitives.python_interpreter import ( |
24 | 24 | LARGE_VAR_THRESHOLD, |
25 | 25 | PythonInterpreter, |
@@ -740,6 +740,35 @@ def test_submit_valid_runtime_edge_cases(field, code, expected): |
740 | 740 | assert interpreter.execute(code) == FinalOutput(expected) |
741 | 741 |
|
742 | 742 |
|
| 743 | +def test_bind_replaces_tools_and_submit_shape_without_resetting_state(pooled_interpreter): |
| 744 | + tools = {"tool": lambda: "ok"} |
| 745 | + fields = [{"name": "answer", "type": "str"}] |
| 746 | + pooled_interpreter.execute("state = 41") |
| 747 | + pooled_interpreter.bind(tools=tools, output_fields=fields) |
| 748 | + tools.clear() |
| 749 | + fields[0]["name"] = "changed" |
| 750 | + |
| 751 | + assert pooled_interpreter.execute("tool()") == "ok" |
| 752 | + assert pooled_interpreter.execute("state + 1") == 42 |
| 753 | + assert pooled_interpreter.execute("SUBMIT(answer='done')") == FinalOutput({"answer": "done"}) |
| 754 | + with pytest.raises(TypeError): |
| 755 | + pooled_interpreter.bind(tools={}, output_fields=[None]) |
| 756 | + assert pooled_interpreter.execute("tool()") == "ok" |
| 757 | + |
| 758 | + pooled_interpreter.bind(tools={}) |
| 759 | + assert pooled_interpreter.execute("SUBMIT('default')") == FinalOutput({"output": "default"}) |
| 760 | + |
| 761 | + |
| 762 | +def test_bind_fallback_invalidates_legacy_registration(): |
| 763 | + class LegacyPythonInterpreter(PythonInterpreter): |
| 764 | + bind = None |
| 765 | + |
| 766 | + with LegacyPythonInterpreter(tools={"old": lambda: "old"}) as interpreter: |
| 767 | + assert interpreter.execute("old()") == "old" |
| 768 | + _bind_interpreter(interpreter, {"new": lambda: "new"}) |
| 769 | + assert interpreter.execute("new()") == "new" |
| 770 | + |
| 771 | + |
743 | 772 | def test_tool_positional_args(configure_pooled_interpreter): |
744 | 773 | """Test that tools work with positional arguments.""" |
745 | 774 |
|
|
0 commit comments