Skip to content

Commit 7d53ae3

Browse files
committed
🐛 version 0.60.3
fix rule's depend inject
1 parent 75fe522 commit 7d53ae3

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

src/nonebot_plugin_alconna/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
from .uniseg import patch_matcher_send as patch_matcher_send
142142
from .uniseg import patch_saa as patch_saa
143143

144-
__version__ = "0.60.2"
144+
__version__ = "0.60.3"
145145
__supported_adapters__ = set(m.value for m in SupportAdapterModule.__members__.values()) # noqa: C401
146146
__plugin_meta__ = PluginMetadata(
147147
name="Alconna 插件",

src/nonebot_plugin_alconna/params.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from contextlib import AsyncExitStack
12
import inspect
23
from typing import Annotated, Any, ClassVar, Literal, Optional, TypeVar, Union, overload
3-
from typing_extensions import get_args
4+
from typing_extensions import Self, get_args, override
45

56
from arclet.alconna import Alconna, Arparma, Duplication, Empty
67
from arclet.alconna.builtin import generate_duplication
@@ -10,7 +11,8 @@
1011
from nonebot.internal.adapter import Bot, Event
1112
from nonebot.internal.matcher import Matcher
1213
from nonebot.internal.params import Depends
13-
from nonebot.typing import T_State
14+
from nonebot.typing import T_DependencyCache, T_State
15+
from nonebot.utils import generic_check_issubclass
1416
from tarina import run_always_await
1517
from tarina.generic import get_origin
1618

@@ -349,3 +351,47 @@ async def __call__(self, _state: T_State, event: Event, bot: Bot) -> bool:
349351
self.result = None
350352
return True
351353
return False
354+
355+
356+
class StackParam(Param):
357+
"""上下文栈注入参数。
358+
359+
本注入解析 AsyncExitStack 实例,用于在依赖注入中管理异步上下文。
360+
"""
361+
362+
def __repr__(self) -> str:
363+
return "_StackParam()"
364+
365+
@classmethod
366+
@override
367+
def _check_param(cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]) -> Optional[Self]:
368+
if param.annotation == AsyncExitStack:
369+
return cls(..., type=AsyncExitStack)
370+
if generic_check_issubclass(param.annotation, AsyncExitStack):
371+
return cls(..., type=AsyncExitStack, default=None)
372+
373+
@override
374+
async def _solve(self, stack: Optional[AsyncExitStack] = None, **kwargs: Any) -> Any:
375+
return stack
376+
377+
378+
class DependencyCacheParam(Param):
379+
"""依赖缓存注入参数。
380+
381+
本注入解析 T_DependencyCache 实例,用于在依赖注入中管理依赖缓存。
382+
"""
383+
384+
def __repr__(self) -> str:
385+
return "_DependencyCacheParam()"
386+
387+
@classmethod
388+
@override
389+
def _check_param(cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]) -> Optional[Self]:
390+
if param.annotation == T_DependencyCache:
391+
return cls(..., type=T_DependencyCache)
392+
if generic_check_issubclass(param.annotation, T_DependencyCache):
393+
return cls(..., type=T_DependencyCache, default=None)
394+
395+
@override
396+
async def _solve(self, dependency_cache: Optional[T_DependencyCache] = None, **kwargs: Any) -> Any:
397+
return dependency_cache

src/nonebot_plugin_alconna/rule.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .extension import ExtensionExecutor, SelectedExtensions, _DependentExecutor
2222
from .i18n import Lang
2323
from .model import CommandResult, CompConfig
24+
from .params import DependencyCacheParam, StackParam
2425
from .uniseg import UniMessage, UniMsg
2526
from .uniseg.constraint import UNISEG_MESSAGE
2627

@@ -292,8 +293,8 @@ async def __call__(
292293
bot: Bot,
293294
event: Event,
294295
state: T_State,
295-
stack: Optional[AsyncExitStack] = None,
296-
dependency_cache: Optional[dict[_DependentCallable[Any], DependencyCache]] = None,
296+
stack: Optional[AsyncExitStack] = StackParam(), # type: ignore
297+
dependency_cache: Optional[dict[_DependentCallable[Any], DependencyCache]] = DependencyCacheParam(), # type: ignore
297298
) -> bool:
298299

299300
if self.before_rules.checkers and not await self.before_rules(bot, event, state, stack, dependency_cache):

src/nonebot_plugin_alconna/uniseg/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
from .tools import image_fetch as image_fetch
6565
from .tools import reply_fetch as reply_fetch
6666

67-
__version__ = "0.60.2"
67+
__version__ = "0.60.3"
6868

6969
__plugin_meta__ = PluginMetadata(
7070
name="Universal Segment 插件",

0 commit comments

Comments
 (0)