From 15c5464069ce76b840b3959d3923558a2d4fd3a2 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:47:29 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Feature:=20=E8=B7=B3=E8=BF=87?= =?UTF-8?q?=E9=83=A8=E5=88=86=E9=9D=9E=E5=BF=85=E8=A6=81=E7=9A=84=20task?= =?UTF-8?q?=20group=20=E5=88=9B=E5=BB=BA=20(#3095)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/dependencies/__init__.py | 18 ++++++++++++------ nonebot/internal/driver/abstract.py | 6 ++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/nonebot/dependencies/__init__.py b/nonebot/dependencies/__init__.py index da02f531..7c6f58c0 100644 --- a/nonebot/dependencies/__init__.py +++ b/nonebot/dependencies/__init__.py @@ -182,13 +182,17 @@ class Dependent(Generic[R]): return cls(call, params, parameterless_params) async def check(self, **params: Any) -> None: - async with anyio.create_task_group() as tg: - for param in self.parameterless: - tg.start_soon(partial(param._check, **params)) + if self.parameterless: + async with anyio.create_task_group() as tg: + for param in self.parameterless: + tg.start_soon(partial(param._check, **params)) - async with anyio.create_task_group() as tg: - for param in self.params: - tg.start_soon(partial(cast(Param, param.field_info)._check, **params)) + if self.params: + async with anyio.create_task_group() as tg: + for param in self.params: + tg.start_soon( + partial(cast(Param, param.field_info)._check, **params) + ) async def _solve_field(self, field: ModelField, params: dict[str, Any]) -> Any: param = cast(Param, field.field_info) @@ -205,6 +209,8 @@ class Dependent(Generic[R]): # solve param values result: dict[str, Any] = {} + if not self.params: + return result async def _solve_field(field: ModelField, params: dict[str, Any]) -> None: value = await self._solve_field(field, params) diff --git a/nonebot/internal/driver/abstract.py b/nonebot/internal/driver/abstract.py index 257fc5e9..994abc5d 100644 --- a/nonebot/internal/driver/abstract.py +++ b/nonebot/internal/driver/abstract.py @@ -158,6 +158,9 @@ class Driver(abc.ABC): raise RuntimeError(f"Duplicate bot connection with id {bot.self_id}") self._bots[bot.self_id] = bot + if not self._bot_connection_hook: + return + def handle_exception(exc_group: BaseExceptionGroup) -> None: for exc in flatten_exception_group(exc_group): logger.opt(colors=True, exception=exc).error( @@ -186,6 +189,9 @@ class Driver(abc.ABC): if bot.self_id in self._bots: del self._bots[bot.self_id] + if not self._bot_disconnection_hook: + return + def handle_exception(exc_group: BaseExceptionGroup) -> None: for exc in flatten_exception_group(exc_group): logger.opt(colors=True, exception=exc).error(