diff --git a/examples/distributed_simulation/run_simlation.sh b/examples/distributed_simulation/run_simulation.sh similarity index 100% rename from examples/distributed_simulation/run_simlation.sh rename to examples/distributed_simulation/run_simulation.sh diff --git a/src/agentscope/agents/rpc_agent.py b/src/agentscope/agents/rpc_agent.py index b7c3441bc..47d32ce3a 100644 --- a/src/agentscope/agents/rpc_agent.py +++ b/src/agentscope/agents/rpc_agent.py @@ -9,7 +9,7 @@ import base64 import traceback import asyncio -from typing import Any, Type, Optional, Union, Sequence +from typing import Type, Optional, Union, Sequence from concurrent import futures from loguru import logger @@ -18,11 +18,13 @@ import grpc from grpc import ServicerContext from expiringdict import ExpiringDict -except ImportError: - dill = None - grpc = None - ServicerContext = Any - ExpiringDict = None +except ImportError as import_error: + from agentscope.utils.tools import ImportErrorReporter + + dill = ImportErrorReporter(import_error, "distribute") + grpc = ImportErrorReporter(import_error, "distribute") + ServicerContext = ImportErrorReporter(import_error, "distribute") + ExpiringDict = ImportErrorReporter(import_error, "distribute") from agentscope._init import init_process, _INIT_SETTINGS from agentscope.agents.agent import AgentBase diff --git a/src/agentscope/models/dashscope_model.py b/src/agentscope/models/dashscope_model.py index 4fd380de3..c4183aa85 100644 --- a/src/agentscope/models/dashscope_model.py +++ b/src/agentscope/models/dashscope_model.py @@ -11,7 +11,7 @@ try: import dashscope -except ModuleNotFoundError: +except ImportError: dashscope = None from .model import ModelWrapperBase, ModelResponse diff --git a/src/agentscope/rpc/__init__.py b/src/agentscope/rpc/__init__.py index 03cf58169..2c7703a90 100644 --- a/src/agentscope/rpc/__init__.py +++ b/src/agentscope/rpc/__init__.py @@ -5,16 +5,19 @@ try: from .rpc_agent_pb2 import RpcMsg # pylint: disable=E0611 -except ModuleNotFoundError: - RpcMsg = Any # type: ignore[misc] -try: from .rpc_agent_pb2_grpc import RpcAgentServicer from .rpc_agent_pb2_grpc import RpcAgentStub from .rpc_agent_pb2_grpc import add_RpcAgentServicer_to_server -except ImportError: - RpcAgentServicer = object - RpcAgentStub = Any - add_RpcAgentServicer_to_server = Any +except ImportError as import_error: + from agentscope.utils.tools import ImportErrorReporter + + RpcMsg = ImportErrorReporter(import_error, "distribute") # type: ignore[misc] + RpcAgentServicer = ImportErrorReporter(import_error, "distribute") + RpcAgentStub = ImportErrorReporter(import_error, "distribute") + add_RpcAgentServicer_to_server = ImportErrorReporter( + import_error, + "distribute", + ) __all__ = [ diff --git a/src/agentscope/rpc/rpc_agent_client.py b/src/agentscope/rpc/rpc_agent_client.py index ab9f1a565..189e0895f 100644 --- a/src/agentscope/rpc/rpc_agent_client.py +++ b/src/agentscope/rpc/rpc_agent_client.py @@ -3,24 +3,23 @@ import threading import base64 -from typing import Any, Optional +from typing import Optional from loguru import logger try: import dill import grpc from grpc import RpcError -except ImportError: - dill = None - grpc = None - RpcError = None - -try: from agentscope.rpc.rpc_agent_pb2 import RpcMsg # pylint: disable=E0611 from agentscope.rpc.rpc_agent_pb2_grpc import RpcAgentStub -except ModuleNotFoundError: - RpcMsg = Any # type: ignore[misc] - RpcAgentStub = Any +except ImportError as import_error: + from agentscope.utils.tools import ImportErrorReporter + + dill = ImportErrorReporter(import_error, "distribute") + grpc = ImportErrorReporter(import_error, "distribute") + RpcMsg = ImportErrorReporter(import_error, "distribute") + RpcAgentStub = ImportErrorReporter(import_error, "distribute") + RpcError = ImportError class RpcAgentClient: diff --git a/src/agentscope/rpc/rpc_agent_pb2_grpc.py b/src/agentscope/rpc/rpc_agent_pb2_grpc.py index 93ee27369..4099c7027 100644 --- a/src/agentscope/rpc/rpc_agent_pb2_grpc.py +++ b/src/agentscope/rpc/rpc_agent_pb2_grpc.py @@ -3,8 +3,10 @@ """Client and server classes corresponding to protobuf-defined services.""" try: import grpc -except ImportError: - grpc = None +except ImportError as import_error: + from agentscope.utils.tools import ImportErrorReporter + + grpc = ImportErrorReporter(import_error, "distribute") import agentscope.rpc.rpc_agent_pb2 as rpc__agent__pb2 diff --git a/src/agentscope/utils/tools.py b/src/agentscope/utils/tools.py index e99bc29a9..8ebd23777 100644 --- a/src/agentscope/utils/tools.py +++ b/src/agentscope/utils/tools.py @@ -296,3 +296,39 @@ def _join_str_with_comma_and(elements: List[str]) -> str: return " and ".join(elements) else: return ", ".join(elements[:-1]) + f", and {elements[-1]}" + + +class ImportErrorReporter: + """Used as a placeholder for missing packages. + When called, an ImportError will be raised, prompting the user to install + the specified extras requirement. + """ + + def __init__(self, error: ImportError, extras_require: str = None) -> None: + """Init the ImportErrorReporter. + + Args: + error (`ImportError`): the original ImportError. + extras_require (`str`): the extras requirement. + """ + self.error = error + self.extras_require = extras_require + + def __call__(self, *args: Any, **kwds: Any) -> Any: + return self._raise_import_error() + + def __getattr__(self, name: str) -> Any: + return self._raise_import_error() + + def __getitem__(self, __key: Any) -> Any: + return self._raise_import_error() + + def _raise_import_error(self) -> Any: + """Raise the ImportError""" + err_msg = f"ImportError occorred: [{self.error.msg}]." + if self.extras_require is not None: + err_msg += ( + f" Please install [{self.extras_require}] version" + " of agentscope." + ) + raise ImportError(err_msg)