🐛 fix import error for extra dependencies

This commit is contained in:
yanyongyu 2021-12-23 14:29:21 +08:00
parent cda49551a8
commit 0839d6d062
3 changed files with 15 additions and 6 deletions

View File

@ -252,7 +252,7 @@ class ReverseDriver(Driver):
def combine_driver(driver: Type[Driver], *mixins: Type[ForwardMixin]) -> Type[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 @property
def type(self) -> str: def type(self) -> str:
return ( return (

View File

@ -5,15 +5,19 @@ AIOHTTP 驱动适配
本驱动仅支持客户端连接 本驱动仅支持客户端连接
""" """
import aiohttp
from nonebot.typing import overrides from nonebot.typing import overrides
from nonebot.drivers import Request, Response from nonebot.drivers import Request, Response
from nonebot.drivers._block_driver import BlockDriver from nonebot.drivers._block_driver import BlockDriver
from nonebot.drivers import WebSocket as BaseWebSocket from nonebot.drivers import WebSocket as BaseWebSocket
from nonebot.drivers import HTTPVersion, ForwardMixin, combine_driver 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): class AiohttpMixin(ForwardMixin):
@property @property

View File

@ -23,7 +23,6 @@ from nonebot.config import Env
from nonebot.typing import overrides from nonebot.typing import overrides
from nonebot.utils import escape_tag from nonebot.utils import escape_tag
from nonebot.drivers.httpx import HttpxMixin from nonebot.drivers.httpx import HttpxMixin
from nonebot.drivers.aiohttp import AiohttpMixin
from nonebot.config import Config as NoneBotConfig from nonebot.config import Config as NoneBotConfig
from nonebot.drivers import Request as BaseRequest from nonebot.drivers import Request as BaseRequest
from nonebot.drivers import WebSocket as BaseWebSocket from nonebot.drivers import WebSocket as BaseWebSocket
@ -35,6 +34,11 @@ from nonebot.drivers import (
combine_driver, combine_driver,
) )
try:
from nonebot.drivers.aiohttp import AiohttpMixin
except ImportError:
AiohttpMixin = None
class Config(BaseSettings): class Config(BaseSettings):
""" """
@ -316,4 +320,5 @@ class FastAPIWebSocket(BaseWebSocket):
FullDriver = combine_driver(Driver, HttpxMixin, WebSocketsMixin) FullDriver = combine_driver(Driver, HttpxMixin, WebSocketsMixin)
AiohttpDriver = combine_driver(Driver, AiohttpMixin) if AiohttpMixin:
AiohttpDriver = combine_driver(Driver, AiohttpMixin)