Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix: fix mypy errors in `PythonInterpreter` (#200)

Tianqi Xubc963b73ba94
diff --git a/camel/agents/embodied_agent.py b/camel/agents/embodied_agent.py
index 9343635f4..4543abe7f 100644
--- a/camel/agents/embodied_agent.py
+++ b/camel/agents/embodied_agent.py
@@ -11,7 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from typing import Any, List, Optional
+from typing import Any, Dict, List, Optional
from colorama import Fore
@@ -120,7 +120,7 @@ class EmbodiedAgent(ChatAgent):
if codes is not None:
content = "\n> Executed Results:"
- action_space = {
+ action_space: Dict[str, Any] = {
action.name: action
for action in self.action_space
}
diff --git a/camel/utils/python_interpreter.py b/camel/utils/python_interpreter.py
index e9f0b8145..0d4006d2d 100644
--- a/camel/utils/python_interpreter.py
+++ b/camel/utils/python_interpreter.py
@@ -14,7 +14,8 @@
import ast
import difflib
import importlib
-from typing import Any, Dict, List, Mapping, Optional, Union
+import typing
+from typing import Any, Dict, List, Mapping, Optional
class InterpreterError(ValueError):
@@ -82,12 +83,12 @@ class PythonInterpreter():
import_white_list: Optional[List[str]] = None) -> None:
self.action_space = action_space
self.state = self.action_space.copy()
- self.fuzz_state = {}
+ self.fuzz_state: Dict[str, Any] = {}
self.import_white_list = import_white_list or []
- def execute(self, code: str, state: Dict[str, Any] = None,
- fuzz_state: Dict[str,
- Any] = None, keep_state: bool = True) -> Any:
+ def execute(self, code: str, state: Optional[Dict[str, Any]] = None,
+ fuzz_state: Optional[Dict[str, Any]] = None,
+ keep_state: bool = True) -> Any:
r""" Execute the input python codes in a security environment.
Args:
@@ -128,7 +129,7 @@ class PythonInterpreter():
except InterpreterError as e:
if not keep_state:
self.clear_state()
- msg = (f"Evaluation of the code stopped at line {idx}. "
+ msg = (f"Evaluation of the code stopped at node {idx}. "
f"See:\n{e}")
# More information can be provided by `ast.unparse()`,
# which is new in python 3.9.
@@ -146,6 +147,9 @@ class PythonInterpreter():
self.state = self.action_space.copy()
self.fuzz_state = {}
+ # ast.Index is deprecated after python 3.9, which cannot pass type check,
+ # but is still necessary for older versions.
+ @typing.no_type_check
def _execute_ast(self, expression: ast.AST) -> Any:
if isinstance(expression, ast.Assign):
# Assignement -> evaluate the assignement which should
@@ -166,9 +170,13 @@ class PythonInterpreter():
return expression.value
elif isinstance(expression, ast.Dict):
# Dict -> evaluate all keys and values
- keys = [self._execute_ast(k) for k in expression.keys]
- values = [self._execute_ast(v) for v in expression.values]
- return dict(zip(keys, values))
+ result: Dict = {}
+ for k, v in zip(expression.keys, expression.values):
+ if k is not None:
+ result[self._execute_ast(k)] = self._execute_ast(v)
+ else:
+ result.update(self._execute_ast(v))
+ return result
elif isinstance(expression, ast.Expr):
# Expression -> evaluate the content
return self._execute_ast(expression.value)
@@ -183,15 +191,13 @@ class PythonInterpreter():
return self._execute_if(expression)
elif isinstance(expression, ast.Import):
# Import -> add imported names in self.state and return None.
- for module in expression.names:
- self._execute_import(module)
+ self._execute_import(expression)
return None
elif isinstance(expression, ast.ImportFrom):
- module = expression.module
- for name in expression.names:
- self._execute_import(module, name)
+ self._execute_import_from(expression)
return None
elif hasattr(ast, "Index") and isinstance(expression, ast.Index):
+ # cannot pass type check
return self._execute_ast(expression.value)
elif isinstance(expression, ast.JoinedStr):
return "".join(
@@ -217,26 +223,30 @@ class PythonInterpreter():
f"{expression.__class__.__name__} is not supported.")
def _execute_assign(self, assign: ast.Assign) -> Any:
- var_names = assign.targets
+ targets = assign.targets
result = self._execute_ast(assign.value)
- for var_name in var_names:
- self._assign(var_name, result)
+ for target in targets:
+ self._assign(target, result)
return result
- def _assign(self, target: Union[ast.Name, ast.Tuple], value: Any):
+ def _assign(self, target: ast.expr, value: Any):
if isinstance(target, ast.Name):
self.state[target.id] = value
elif isinstance(target, ast.Tuple):
- if len(value) != len(target):
- raise InterpreterError(f"Expected {len(target)} values but got"
- f"{len(value)}.")
- for v, r in zip(target.ctx, value):
- self.state[v.id] = r
+ if not isinstance(value, tuple):
+ raise InterpreterError(f"Expected type tuple, but got"
+ f"{value.__class__.__name__} instead.")
+ if len(target.elts) != len(value):
+ raise InterpreterError(
+ f"Expected {len(target.elts)} values but got"
+ f" {len(value)}.")
+ for t, v in zip(target.elts, value):
+ self.state[self._execute_ast(t)] = v
else:
- raise InterpreterError(f"Unsupport variable type. Expected"
- f"ast.Name or ast.Tuple, got"
- f"{type(target)} instead.")
+ raise InterpreterError(f"Unsupported variable type. Expected "
+ f"ast.Name or ast.Tuple, got "
+ f"{target.__class__.__name__} instead.")
def _execute_call(self, call: ast.Call) -> Any:
callable_func = self._execute_ast(call.func)
@@ -249,9 +259,13 @@ class PythonInterpreter():
}
return callable_func(*args, **kwargs)
- def _execute_subscript(self, subscript):
+ def _execute_subscript(self, subscript: ast.Subscript):
index = self._execute_ast(subscript.slice)
value = self._execute_ast(subscript.value)
+ if not isinstance(subscript.ctx, ast.Load):
+ raise InterpreterError(
+ f"{subscript.ctx.__class__.__name__} is not supported for "
+ "subscript.")
if isinstance(value, (list, tuple)):
return value[int(index)]
if index in value:
@@ -265,7 +279,12 @@ class PythonInterpreter():
raise InterpreterError(f"Could not index {value} with '{index}'.")
def _execute_name(self, name: ast.Name):
- return self._get_value_from_state(name.id)
+ if isinstance(name.ctx, ast.Store):
+ return name.id
+ elif isinstance(name.ctx, ast.Load):
+ return self._get_value_from_state(name.id)
+ else:
+ raise InterpreterError(f"{name.ctx} is not supported.")
def _execute_condition(self, condition):
if len(condition.ops) > 1:
@@ -324,31 +343,35 @@ class PythonInterpreter():
return result
- def _execute_import(self, module: Union[str, ast.alias],
- import_name: Optional[ast.alias] = None) -> None:
- module_name = module if isinstance(module, str) else module.name
- found_name = False
+ def _execute_import(self, import_module: ast.Import) -> None:
+ for module in import_module.names:
+ self._validate_import(module.name)
+ alias = module.asname or module.name
+ self.state[alias] = importlib.import_module(module.name)
+
+ def _execute_import_from(self, import_from: ast.ImportFrom):
+ if import_from.module is None:
+ raise InterpreterError("\"from . import\" is not supported.")
+ for import_name in import_from.names:
+ full_name = import_from.module + f".{import_name.name}"
+ self._validate_import(full_name)
+ imported_module = importlib.import_module(import_from.module)
+ alias = import_name.asname or import_name.name
+ self.state[alias] = getattr(imported_module, import_name.name)
+
+ def _validate_import(self, full_name: str):
tmp_name = ""
- full_name = (module_name if import_name is None else module_name +
- f".{import_name.name}")
+ found_name = False
for name in full_name.split("."):
tmp_name += name if tmp_name == "" else f".{name}"
if tmp_name in self.import_white_list:
found_name = True
- break
+ return
if not found_name:
raise InterpreterError(f"It is not permitted to import modules "
f"than module white list (try to import "
- f"{module_name}).")
-
- if import_name is None:
- alias = (module.asname or module_name)
- self.state[alias] = importlib.import_module(module_name)
- else:
- imported_module = importlib.import_module(module_name)
- alias = import_name.asname or import_name.name
- self.state[alias] = getattr(imported_module, import_name.name)
+ f"{full_name}).")
def _execute_binop(self, binop: ast.BinOp):
left = self._execute_ast(binop.left)
diff --git a/test/utils/test_python_interpreter.py b/test/utils/test_python_interpreter.py
index 9e0d4f522..631d89eba 100644
--- a/test/utils/test_python_interpreter.py
+++ b/test/utils/test_python_interpreter.py
@@ -26,39 +26,33 @@ def action_function():
@pytest.fixture()
def interpreter():
action_space = {"action1": action_function}
- white_list = ["torch", "numpy.array"]
+ white_list = ["torch", "numpy.array", "openai"]
return PythonInterpreter(action_space=action_space,
import_white_list=white_list)
def test_import_success0(interpreter):
- code = """import torch
-a = torch.tensor([[1., -1.], [1., -1.]])"""
- rnt = interpreter.execute(code)
+ code = """import torch as pt, openai
+a = pt.tensor([[1., -1.], [1., -1.]])
+openai.__version__"""
+ execution_res = interpreter.execute(code)
assert torch.equal(interpreter.state["a"],
torch.tensor([[1., -1.], [1., -1.]]))
- assert torch.equal(rnt, torch.tensor([[1., -1.], [1., -1.]]))
+ assert isinstance(execution_res, str)
def test_import_success1(interpreter):
code = """from torch import tensor
a = tensor([[1., -1.], [1., -1.]])"""
- rnt = interpreter.execute(code)
- assert torch.equal(rnt, torch.tensor([[1., -1.], [1., -1.]]))
+ execution_res = interpreter.execute(code)
+ assert torch.equal(execution_res, torch.tensor([[1., -1.], [1., -1.]]))
def test_import_success2(interpreter):
- code = """import torch as pytorch
-a = pytorch.tensor([[1., -1.], [1., -1.]])"""
- rnt = interpreter.execute(code)
- assert torch.equal(rnt, torch.tensor([[1., -1.], [1., -1.]]))
-
-
-def test_import_success3(interpreter):
code = """from numpy import array
x = array([[1, 2, 3], [4, 5, 6]])"""
- rnt = interpreter.execute(code)
- assert np.equal(rnt, np.array([[1, 2, 3], [4, 5, 6]])).all()
+ execution_res = interpreter.execute(code)
+ assert np.equal(execution_res, np.array([[1, 2, 3], [4, 5, 6]])).all()
def test_import_fail0(interpreter):
@@ -67,7 +61,7 @@ os.mkdir("/tmp/test")"""
with pytest.raises(InterpreterError) as e:
interpreter.execute(code)
exec_msg = e.value.args[0]
- assert exec_msg == ("Evaluation of the code stopped at line 0. See:\n"
+ assert exec_msg == ("Evaluation of the code stopped at node 0. See:\n"
"It is not permitted to import modules than module"
" white list (try to import os).")
@@ -78,24 +72,24 @@ x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)"""
with pytest.raises(InterpreterError) as e:
interpreter.execute(code)
exec_msg = e.value.args[0]
- assert exec_msg == ("Evaluation of the code stopped at line 0. See:\n"
+ assert exec_msg == ("Evaluation of the code stopped at node 0. See:\n"
"It is not permitted to import modules than module"
" white list (try to import numpy).")
def test_action_space(interpreter):
- code = "rnt = action1()"
- rnt = interpreter.execute(code)
- assert rnt == "access action function"
+ code = "res = action1()"
+ execution_res = interpreter.execute(code)
+ assert execution_res == "access action function"
def test_fuzz_space(interpreter):
from PIL import Image
fuzz_state = {"image": Image.new("RGB", (256, 256))}
code = "output_image = input_image.crop((20, 20, 100, 100))"
- rnt = interpreter.execute(code, fuzz_state=fuzz_state)
- assert rnt.width == 80
- assert rnt.height == 80
+ execution_res = interpreter.execute(code, fuzz_state=fuzz_state)
+ assert execution_res.width == 80
+ assert execution_res.height == 80
def test_keep_state0(interpreter):
@@ -103,30 +97,54 @@ def test_keep_state0(interpreter):
code2 = "b = a"
code3 = "c = b"
- rnt = interpreter.execute(code1, keep_state=True)
- assert rnt == 42
- rnt = interpreter.execute(code2, keep_state=False)
- assert rnt == 42
+ execution_res = interpreter.execute(code1, keep_state=True)
+ assert execution_res == 42
+ execution_res = interpreter.execute(code2, keep_state=False)
+ assert execution_res == 42
with pytest.raises(InterpreterError) as e:
interpreter.execute(code3, keep_state=False)
exec_msg = e.value.args[0]
- assert exec_msg == ("Evaluation of the code stopped at line 0. See:\n"
+ assert exec_msg == ("Evaluation of the code stopped at node 0. See:\n"
"The variable `b` is not defined.")
def test_keep_state1(interpreter):
code1 = "from torch import tensor"
code2 = "a = tensor([[1., -1.], [1., -1.]])"
- rnt = interpreter.execute(code1, keep_state=True)
- rnt = interpreter.execute(code2, keep_state=False)
- assert torch.equal(rnt, torch.tensor([[1., -1.], [1., -1.]]))
+ execution_res = interpreter.execute(code1, keep_state=True)
+ execution_res = interpreter.execute(code2, keep_state=False)
+ assert torch.equal(execution_res, torch.tensor([[1., -1.], [1., -1.]]))
with pytest.raises(InterpreterError) as e:
interpreter.execute(code2, keep_state=False)
exec_msg = e.value.args[0]
- assert exec_msg == ("Evaluation of the code stopped at line 0. See:\n"
+ assert exec_msg == ("Evaluation of the code stopped at node 0. See:\n"
"The variable `tensor` is not defined.")
+def test_assign0(interpreter):
+ code = "a = b = 1"
+ interpreter.execute(code)
+ assert interpreter.state["a"] == 1
+ assert interpreter.state["b"] == 1
+
+
+def test_assign1(interpreter):
+ code = "a, b = c = 2, 3"
+ interpreter.execute(code)
+ assert interpreter.state["a"] == 2
+ assert interpreter.state["b"] == 3
+ assert interpreter.state["c"] == (2, 3)
+
+
+def test_assign_fail(interpreter):
+ code = "x = a, b, c = 2, 3"
+ with pytest.raises(InterpreterError) as e:
+ interpreter.execute(code, keep_state=False)
+ exec_msg = e.value.args[0]
+ assert exec_msg == ("Evaluation of the code stopped at node 0. See:\n"
+ "Expected 3 values but got 2.")
+
+
def test_if(interpreter):
code = """a = 0
b = 1
@@ -146,12 +164,23 @@ def test_for(interpreter):
sum = 0
for i in l:
sum = sum + i"""
- rnt = interpreter.execute(code)
- assert rnt == 28
+ execution_res = interpreter.execute(code)
+ assert execution_res == 28
-def test_subscript(interpreter):
+def test_subscript_access(interpreter):
code = """l = [2, 3, 5, 7, 11]
-rnt = l[3]"""
- rnt = interpreter.execute(code)
- assert rnt == 7
+res = l[3]"""
+ execution_res = interpreter.execute(code)
+ assert execution_res == 7
+
+
+def test_subscript_assign(interpreter):
+ code = """l = [2, 3, 5, 7, 11]
+l[3] = 1"""
+ with pytest.raises(InterpreterError) as e:
+ interpreter.execute(code, keep_state=False)
+ exec_msg = e.value.args[0]
+ assert exec_msg == ("Evaluation of the code stopped at node 1. See:\n"
+ "Unsupported variable type. Expected ast.Name or "
+ "ast.Tuple, got Subscript instead.")