Skip to content

Commit

Permalink
add ImportErrorReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c committed May 14, 2024
1 parent 2854904 commit 4416d4b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 23 deletions.
12 changes: 7 additions & 5 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 @@ -19,10 +19,12 @@
from grpc import ServicerContext
from expiringdict import ExpiringDict
except ImportError:
dill = None
grpc = None
ServicerContext = Any
ExpiringDict = None
from agentscope.utils.tools import ImportErrorReporter

dill = ImportErrorReporter("dill", "distribute")
grpc = ImportErrorReporter("grpcio", "distribute")
ServicerContext = ImportErrorReporter("grpcio", "distribute")
ExpiringDict = ImportErrorReporter("expiringdict", "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
15 changes: 9 additions & 6 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
from agentscope.utils.tools import ImportErrorReporter

RpcMsg = ImportErrorReporter("protobuf", "distribute") # type: ignore[misc]
RpcAgentServicer = ImportErrorReporter("grpcio", "distribute")
RpcAgentStub = ImportErrorReporter("grpcio", "distribute")
add_RpcAgentServicer_to_server = ImportErrorReporter(
"grpcio",
"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:
from agentscope.utils.tools import ImportErrorReporter

dill = ImportErrorReporter("dill", "distribute")
grpc = ImportErrorReporter("grpcio", "distribute")
RpcMsg = ImportErrorReporter("protobuf", "distribute")
RpcAgentStub = ImportErrorReporter("grpcio", "distribute")
RpcError = ImportError


class RpcAgentClient:
Expand Down
4 changes: 3 additions & 1 deletion src/agentscope/rpc/rpc_agent_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
try:
import grpc
except ImportError:
grpc = None
from agentscope.utils.tools import ImportErrorReporter

grpc = ImportErrorReporter("grpcio", "distribute")

import agentscope.rpc.rpc_agent_pb2 as rpc__agent__pb2

Expand Down
33 changes: 33 additions & 0 deletions src/agentscope/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,36 @@ 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, package_name: str, extras_require: str = None) -> None:
"""Init the ImportErrorReporter.
Args:
package_name (`str`): the name of the package to be imported.
extras_require (`str`): the extras requirement.
"""
self.package_name = package_name
self.extras_require = extras_require

def raise_error(self) -> Any:
"""Raise an ImportError."""
msg = f"Failed to import {self.package_name}."
if self.extras_require is not None:
msg += (
f" Please install [{self.extras_require}] version of"
" agentscope."
)
raise ImportError(msg)

def __call__(self, *args: Any, **kwds: Any) -> Any:
return self.raise_error()

def __getattr__(self, name: str) -> Any:
return self.raise_error()

0 comments on commit 4416d4b

Please sign in to comment.