Skip to content

Commit dc4daf4

Browse files
committed
🍻 fix version 0.60.3
use Rule.HANDLER_PARAM_TYPES
1 parent 7d53ae3 commit dc4daf4

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

src/nonebot_plugin_alconna/matcher.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,19 @@
3838
from .extension import Extension, ExtensionExecutor
3939
from .i18n import Lang
4040
from .model import CompConfig
41-
from .params import CHECK, MIDDLEWARE, AlconnaParam, Check, ExtensionParam, _Dispatch, _seminal, assign, merge_path
41+
from .params import (
42+
CHECK,
43+
MIDDLEWARE,
44+
AlconnaParam,
45+
Check,
46+
DependencyCacheParam,
47+
ExtensionParam,
48+
StackParam,
49+
_Dispatch,
50+
_seminal,
51+
assign,
52+
merge_path,
53+
)
4254
from .rule import AlconnaRule
4355
from .uniseg import Segment, Text, UniMessage, get_message_id, get_target
4456
from .uniseg.fallback import FallbackStrategy
@@ -1007,6 +1019,13 @@ def on_alconna(
10071019
command.formatter.add(command)
10081020
except ValueError:
10091021
pass
1022+
Rule.HANDLER_PARAM_TYPES = [
1023+
*Matcher.HANDLER_PARAM_TYPES[:-1],
1024+
StackParam,
1025+
DependencyCacheParam,
1026+
AlconnaParam,
1027+
DefaultParam,
1028+
]
10101029
_rule = AlconnaRule(
10111030
command,
10121031
skip_for_unmatch,
@@ -1024,6 +1043,8 @@ def on_alconna(
10241043
executor.params = params = (
10251044
ExtensionParam.new(executor),
10261045
*Matcher.HANDLER_PARAM_TYPES[:-1],
1046+
StackParam,
1047+
DependencyCacheParam,
10271048
AlconnaParam,
10281049
DefaultParam,
10291050
)

src/nonebot_plugin_alconna/params.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
from nonebot.dependencies import Param
1111
from nonebot.internal.adapter import Bot, Event
1212
from nonebot.internal.matcher import Matcher
13-
from nonebot.internal.params import Depends
14-
from nonebot.typing import T_DependencyCache, T_State
13+
from nonebot.internal.params import DependencyCache, Depends
14+
from nonebot.typing import T_State, _DependentCallable
1515
from nonebot.utils import generic_check_issubclass
1616
from tarina import run_always_await
17-
from tarina.generic import get_origin
17+
from tarina.generic import get_origin, is_optional
1818

1919
from .consts import ALCONNA_ARG_KEY, ALCONNA_ARG_KEYS, ALCONNA_EXEC_RESULT, ALCONNA_EXTENSION, ALCONNA_RESULT
2020
from .extension import Extension, ExtensionExecutor, SelectedExtensions
@@ -367,8 +367,10 @@ def __repr__(self) -> str:
367367
def _check_param(cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]) -> Optional[Self]:
368368
if param.annotation == AsyncExitStack:
369369
return cls(..., type=AsyncExitStack)
370+
if param.annotation == Optional[AsyncExitStack]:
371+
return cls(None, type=AsyncExitStack)
370372
if generic_check_issubclass(param.annotation, AsyncExitStack):
371-
return cls(..., type=AsyncExitStack, default=None)
373+
return cls(None, type=AsyncExitStack)
372374

373375
@override
374376
async def _solve(self, stack: Optional[AsyncExitStack] = None, **kwargs: Any) -> Any:
@@ -387,11 +389,15 @@ def __repr__(self) -> str:
387389
@classmethod
388390
@override
389391
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)
392+
if param.annotation == dict[_DependentCallable[Any], DependencyCache]:
393+
return cls(..., type=dict[_DependentCallable[Any], DependencyCache])
394+
if param.annotation == Optional[dict[_DependentCallable[Any], DependencyCache]]:
395+
return cls(None, type=dict[_DependentCallable[Any], DependencyCache])
396+
if is_optional(param.annotation, dict[_DependentCallable[Any], DependencyCache]):
397+
return cls(None, type=dict[_DependentCallable[Any], DependencyCache])
394398

395399
@override
396-
async def _solve(self, dependency_cache: Optional[T_DependencyCache] = None, **kwargs: Any) -> Any:
400+
async def _solve(
401+
self, dependency_cache: Optional[dict[_DependentCallable[Any], DependencyCache]] = None, **kwargs: Any
402+
) -> Any:
397403
return dependency_cache

src/nonebot_plugin_alconna/rule.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
from contextlib import AsyncExitStack
3-
from typing import Any, Literal, Optional, Union
3+
from typing import Any, ClassVar, Literal, Optional, Union
44
import weakref
55

66
from arclet.alconna import Alconna, Arparma, CompSession, command_manager, output_manager
@@ -21,7 +21,6 @@
2121
from .extension import ExtensionExecutor, SelectedExtensions, _DependentExecutor
2222
from .i18n import Lang
2323
from .model import CommandResult, CompConfig
24-
from .params import DependencyCacheParam, StackParam
2524
from .uniseg import UniMessage, UniMsg
2625
from .uniseg.constraint import UNISEG_MESSAGE
2726

@@ -214,7 +213,10 @@ def destroy(self) -> None:
214213

215214
@property
216215
def rule(self) -> Rule:
217-
return Rule(self)
216+
class _Rule(Rule):
217+
HANDLER_PARAM_TYPES: ClassVar = list(self.executor.params)
218+
219+
return _Rule(self)
218220

219221
def __repr__(self) -> str:
220222
return f"Alconna(command={self.command()!r})"
@@ -293,10 +295,9 @@ async def __call__(
293295
bot: Bot,
294296
event: Event,
295297
state: T_State,
296-
stack: Optional[AsyncExitStack] = StackParam(), # type: ignore
297-
dependency_cache: Optional[dict[_DependentCallable[Any], DependencyCache]] = DependencyCacheParam(), # type: ignore
298+
stack: Optional[AsyncExitStack] = None,
299+
dependency_cache: Optional[dict[_DependentCallable[Any], DependencyCache]] = None,
298300
) -> bool:
299-
300301
if self.before_rules.checkers and not await self.before_rules(bot, event, state, stack, dependency_cache):
301302
return False
302303
if event.get_type() == "meta_event":

0 commit comments

Comments
 (0)