Skip to content

Commit 0e24929

Browse files
Do not wrap types in safer_func (TransformerOptimus#1475)
1 parent 19eec13 commit 0e24929

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

src/smolagents/local_python_executor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ def safer_func(
217217
Returns:
218218
Callable: Safer function with return value check.
219219
"""
220+
# If the function is a type, return it directly without wrapping
221+
if isinstance(func, type):
222+
return func
220223

221224
@wraps(func)
222225
def _check_return(*args, **kwargs):

tests/test_local_python_executor.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,9 @@ def test_types_as_objects(self):
761761
code = "type_a = float(2); type_b = str; type_c = int"
762762
state = {}
763763
result, is_final_answer = evaluate_python_code(code, {"float": float, "str": str, "int": int}, state=state)
764-
# Result is wrapped by safer_func
765-
assert result.__wrapped__ is int
764+
# Type objects are not wrapped by safer_func
765+
assert not hasattr(result, "__wrapped__")
766+
assert result is int
766767

767768
def test_tuple_id(self):
768769
code = """
@@ -1134,9 +1135,10 @@ class TestClass:
11341135

11351136
assert result == (5, "test")
11361137
assert isinstance(state["TestClass"], type)
1137-
# Values are wrapped by safer_func
1138-
annotations = {key: value.__wrapped__ for key, value in state["TestClass"].__annotations__.items()}
1139-
assert annotations == {"x": int, "y": str}
1138+
# Type objects are not wrapped by safer_func
1139+
for value in state["TestClass"].__annotations__.values():
1140+
assert not hasattr(value, "__wrapped__")
1141+
assert state["TestClass"].__annotations__ == {"x": int, "y": str}
11401142
assert state["TestClass"].x == 5
11411143
assert state["TestClass"].y == "test"
11421144
assert isinstance(state["instance"], state["TestClass"])
@@ -1190,9 +1192,10 @@ class TestClass:
11901192

11911193
assert result == ("value", ["b", 30])
11921194
assert isinstance(state["TestClass"], type)
1193-
# Values are wrapped by safer_func
1194-
annotations = {key: value.__wrapped__ for key, value in state["TestClass"].__annotations__.items()}
1195-
assert annotations == {"key_data": dict, "index_data": list}
1195+
# Type objects are not wrapped by safer_func
1196+
for value in state["TestClass"].__annotations__.values():
1197+
assert not hasattr(value, "__wrapped__")
1198+
assert state["TestClass"].__annotations__ == {"key_data": dict, "index_data": list}
11961199
assert state["TestClass"].key_data == {"key": "value"}
11971200
assert state["TestClass"].index_data == ["a", "b", 30]
11981201

@@ -1863,6 +1866,16 @@ def target_function():
18631866
assert res.__name__ == "target_function"
18641867
assert res.__source__ == "def target_function():\n return 'Hello world'"
18651868

1869+
@pytest.mark.parametrize(
1870+
"code, expected_result",
1871+
[("isinstance(5, int)", True), ("isinstance('foo', str)", True), ("isinstance(5, str)", False)],
1872+
)
1873+
def test_isinstance_builtin_type(self, code, expected_result):
1874+
executor = LocalPythonExecutor([])
1875+
executor.send_tools({})
1876+
result, _, _ = executor(code)
1877+
assert result is expected_result
1878+
18661879

18671880
class TestLocalPythonExecutorSecurity:
18681881
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)