Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hot Fix] Add ImportErrorReporter to optimize the error message when packages are missing #224

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/agentscope/agents/rpc_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/agentscope/models/dashscope_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

try:
import dashscope
except ModuleNotFoundError:
except ImportError:
dashscope = None

from .model import ModelWrapperBase, ModelResponse
Expand Down
17 changes: 10 additions & 7 deletions src/agentscope/rpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand Down
19 changes: 9 additions & 10 deletions src/agentscope/rpc/rpc_agent_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/agentscope/rpc/rpc_agent_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions src/agentscope/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)