diff --git a/nonebot/drivers/__init__.py b/nonebot/drivers/__init__.py index eeb44a09..44868897 100644 --- a/nonebot/drivers/__init__.py +++ b/nonebot/drivers/__init__.py @@ -252,7 +252,7 @@ class ReverseDriver(Driver): def combine_driver(driver: Type[Driver], *mixins: Type[ForwardMixin]) -> Type[Driver]: - class CombinedDriver(driver, *mixins): # type: ignore + class CombinedDriver(driver, *mixins, ForwardDriver): # type: ignore @property def type(self) -> str: return ( diff --git a/nonebot/drivers/aiohttp.py b/nonebot/drivers/aiohttp.py index cb7f7bbe..031e3b57 100644 --- a/nonebot/drivers/aiohttp.py +++ b/nonebot/drivers/aiohttp.py @@ -5,15 +5,19 @@ AIOHTTP 驱动适配 本驱动仅支持客户端连接 """ - -import aiohttp - from nonebot.typing import overrides from nonebot.drivers import Request, Response from nonebot.drivers._block_driver import BlockDriver from nonebot.drivers import WebSocket as BaseWebSocket from nonebot.drivers import HTTPVersion, ForwardMixin, combine_driver +try: + import aiohttp +except ImportError: + raise ImportError( + "Please install aiohttp first to use this driver. `pip install nonebot2[aiohttp]`" + ) from None + class AiohttpMixin(ForwardMixin): @property diff --git a/nonebot/drivers/fastapi.py b/nonebot/drivers/fastapi.py index 98803aa6..0e36bdda 100644 --- a/nonebot/drivers/fastapi.py +++ b/nonebot/drivers/fastapi.py @@ -23,7 +23,6 @@ from nonebot.config import Env from nonebot.typing import overrides from nonebot.utils import escape_tag from nonebot.drivers.httpx import HttpxMixin -from nonebot.drivers.aiohttp import AiohttpMixin from nonebot.config import Config as NoneBotConfig from nonebot.drivers import Request as BaseRequest from nonebot.drivers import WebSocket as BaseWebSocket @@ -35,6 +34,11 @@ from nonebot.drivers import ( combine_driver, ) +try: + from nonebot.drivers.aiohttp import AiohttpMixin +except ImportError: + AiohttpMixin = None + class Config(BaseSettings): """ @@ -316,4 +320,5 @@ class FastAPIWebSocket(BaseWebSocket): FullDriver = combine_driver(Driver, HttpxMixin, WebSocketsMixin) -AiohttpDriver = combine_driver(Driver, AiohttpMixin) +if AiohttpMixin: + AiohttpDriver = combine_driver(Driver, AiohttpMixin)