2022-02-06 14:52:50 +08:00
|
|
|
|
import asyncio
|
|
|
|
|
import inspect
|
2024-04-16 00:33:48 +08:00
|
|
|
|
from typing_extensions import Self, get_args, override, get_origin
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from contextlib import AsyncExitStack, contextmanager, asynccontextmanager
|
2023-08-29 18:45:12 +08:00
|
|
|
|
from typing import (
|
|
|
|
|
TYPE_CHECKING,
|
|
|
|
|
Any,
|
|
|
|
|
Union,
|
|
|
|
|
Literal,
|
|
|
|
|
Callable,
|
|
|
|
|
Optional,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
Annotated,
|
2023-08-29 18:45:12 +08:00
|
|
|
|
cast,
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
from pydantic.fields import FieldInfo as PydanticFieldInfo
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
from nonebot.dependencies import Param, Dependent
|
2022-05-22 19:42:30 +08:00
|
|
|
|
from nonebot.dependencies.utils import check_field_type
|
2024-01-26 11:12:57 +08:00
|
|
|
|
from nonebot.compat import FieldInfo, ModelField, PydanticUndefined, extract_field_info
|
2024-05-09 15:08:49 +08:00
|
|
|
|
from nonebot.typing import (
|
|
|
|
|
_STATE_FLAG,
|
|
|
|
|
T_State,
|
|
|
|
|
T_Handler,
|
|
|
|
|
T_DependencyCache,
|
|
|
|
|
origin_is_annotated,
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.utils import (
|
|
|
|
|
get_name,
|
|
|
|
|
run_sync,
|
|
|
|
|
is_gen_callable,
|
|
|
|
|
run_sync_ctx_manager,
|
|
|
|
|
is_async_gen_callable,
|
|
|
|
|
is_coroutine_callable,
|
|
|
|
|
generic_check_issubclass,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from nonebot.matcher import Matcher
|
|
|
|
|
from nonebot.adapters import Bot, Event
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DependsInner:
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
dependency: Optional[T_Handler] = None,
|
|
|
|
|
*,
|
|
|
|
|
use_cache: bool = True,
|
2024-01-26 11:12:57 +08:00
|
|
|
|
validate: Union[bool, PydanticFieldInfo] = False,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
) -> None:
|
|
|
|
|
self.dependency = dependency
|
|
|
|
|
self.use_cache = use_cache
|
2023-08-29 18:45:12 +08:00
|
|
|
|
self.validate = validate
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
dep = get_name(self.dependency)
|
|
|
|
|
cache = "" if self.use_cache else ", use_cache=False"
|
2023-08-29 18:45:12 +08:00
|
|
|
|
validate = f", validate={self.validate}" if self.validate else ""
|
|
|
|
|
return f"DependsInner({dep}{cache}{validate})"
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def Depends(
|
|
|
|
|
dependency: Optional[T_Handler] = None,
|
|
|
|
|
*,
|
|
|
|
|
use_cache: bool = True,
|
2024-01-26 11:12:57 +08:00
|
|
|
|
validate: Union[bool, PydanticFieldInfo] = False,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
) -> Any:
|
|
|
|
|
"""子依赖装饰器
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
dependency: 依赖函数。默认为参数的类型注释。
|
|
|
|
|
use_cache: 是否使用缓存。默认为 `True`。
|
2023-08-29 18:45:12 +08:00
|
|
|
|
validate: 是否使用 Pydantic 类型校验。默认为 `False`。
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
用法:
|
|
|
|
|
```python
|
|
|
|
|
def depend_func() -> Any:
|
|
|
|
|
return ...
|
|
|
|
|
|
|
|
|
|
def depend_gen_func():
|
|
|
|
|
try:
|
|
|
|
|
yield ...
|
|
|
|
|
finally:
|
|
|
|
|
...
|
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
async def handler(
|
|
|
|
|
param_name: Any = Depends(depend_func),
|
|
|
|
|
gen: Any = Depends(depend_gen_func),
|
|
|
|
|
):
|
2022-02-06 14:52:50 +08:00
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
"""
|
2023-08-29 18:45:12 +08:00
|
|
|
|
return DependsInner(dependency, use_cache=use_cache, validate=validate)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DependParam(Param):
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""子依赖注入参数。
|
|
|
|
|
|
|
|
|
|
本注入解析所有子依赖注入,然后将它们的返回值作为参数值传递给父依赖。
|
|
|
|
|
|
|
|
|
|
本注入应该具有最高优先级,因此应该在其他参数之前检查。
|
|
|
|
|
"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
def __init__(
|
|
|
|
|
self, *args, dependent: Dependent, use_cache: bool, **kwargs: Any
|
|
|
|
|
) -> None:
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.dependent = dependent
|
|
|
|
|
self.use_cache = use_cache
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return f"Depends({self.dependent}, use_cache={self.use_cache})"
|
2022-09-09 11:52:57 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
2023-08-29 18:45:12 +08:00
|
|
|
|
def _from_field(
|
2024-01-26 11:12:57 +08:00
|
|
|
|
cls,
|
|
|
|
|
sub_dependent: Dependent,
|
|
|
|
|
use_cache: bool,
|
|
|
|
|
validate: Union[bool, PydanticFieldInfo],
|
2023-08-29 18:45:12 +08:00
|
|
|
|
) -> Self:
|
|
|
|
|
kwargs = {}
|
2024-01-26 11:12:57 +08:00
|
|
|
|
if isinstance(validate, PydanticFieldInfo):
|
|
|
|
|
kwargs.update(extract_field_info(validate))
|
2023-08-29 18:45:12 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
kwargs["validate"] = bool(validate)
|
|
|
|
|
kwargs["dependent"] = sub_dependent
|
|
|
|
|
kwargs["use_cache"] = use_cache
|
|
|
|
|
|
|
|
|
|
return cls(**kwargs)
|
2023-08-29 18:45:12 +08:00
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
def _check_param(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]
|
2023-08-29 18:45:12 +08:00
|
|
|
|
) -> Optional[Self]:
|
2023-03-22 20:54:46 +08:00
|
|
|
|
type_annotation, depends_inner = param.annotation, None
|
2023-08-29 18:45:12 +08:00
|
|
|
|
# extract type annotation and dependency from Annotated
|
2023-03-22 20:54:46 +08:00
|
|
|
|
if get_origin(param.annotation) is Annotated:
|
|
|
|
|
type_annotation, *extra_args = get_args(param.annotation)
|
|
|
|
|
depends_inner = next(
|
2023-09-14 00:14:45 +08:00
|
|
|
|
(x for x in reversed(extra_args) if isinstance(x, DependsInner)), None
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
# param default value takes higher priority
|
2023-03-22 20:54:46 +08:00
|
|
|
|
depends_inner = (
|
|
|
|
|
param.default if isinstance(param.default, DependsInner) else depends_inner
|
|
|
|
|
)
|
2023-08-29 18:45:12 +08:00
|
|
|
|
# not a dependent
|
2023-03-22 20:54:46 +08:00
|
|
|
|
if depends_inner is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
dependency: T_Handler
|
2023-08-29 18:45:12 +08:00
|
|
|
|
# sub dependency is not specified, use type annotation
|
2023-03-22 20:54:46 +08:00
|
|
|
|
if depends_inner.dependency is None:
|
|
|
|
|
assert (
|
|
|
|
|
type_annotation is not inspect.Signature.empty
|
|
|
|
|
), "Dependency cannot be empty"
|
|
|
|
|
dependency = type_annotation
|
|
|
|
|
else:
|
|
|
|
|
dependency = depends_inner.dependency
|
2023-08-29 18:45:12 +08:00
|
|
|
|
# parse sub dependency
|
2023-03-22 20:54:46 +08:00
|
|
|
|
sub_dependent = Dependent[Any].parse(
|
|
|
|
|
call=dependency,
|
|
|
|
|
allow_types=allow_types,
|
|
|
|
|
)
|
2023-08-29 18:45:12 +08:00
|
|
|
|
|
|
|
|
|
return cls._from_field(
|
|
|
|
|
sub_dependent, depends_inner.use_cache, depends_inner.validate
|
|
|
|
|
)
|
2023-03-22 20:54:46 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
def _check_parameterless(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cls, value: Any, allow_types: tuple[type[Param], ...]
|
2022-02-06 14:52:50 +08:00
|
|
|
|
) -> Optional["Param"]:
|
|
|
|
|
if isinstance(value, DependsInner):
|
|
|
|
|
assert value.dependency, "Dependency cannot be empty"
|
|
|
|
|
dependent = Dependent[Any].parse(
|
2022-09-07 09:59:05 +08:00
|
|
|
|
call=value.dependency, allow_types=allow_types
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
2023-08-29 18:45:12 +08:00
|
|
|
|
return cls._from_field(dependent, value.use_cache, value.validate)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
async def _solve(
|
|
|
|
|
self,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> Any:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
use_cache: bool = self.use_cache
|
2022-02-06 14:52:50 +08:00
|
|
|
|
dependency_cache = {} if dependency_cache is None else dependency_cache
|
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
sub_dependent: Dependent = self.dependent
|
2022-09-07 09:59:05 +08:00
|
|
|
|
call = cast(Callable[..., Any], sub_dependent.call)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
# solve sub dependency with current cache
|
|
|
|
|
sub_values = await sub_dependent.solve(
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
**kwargs,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# run dependency function
|
|
|
|
|
task: asyncio.Task[Any]
|
|
|
|
|
if use_cache and call in dependency_cache:
|
2022-09-07 09:59:05 +08:00
|
|
|
|
return await dependency_cache[call]
|
2022-02-06 14:52:50 +08:00
|
|
|
|
elif is_gen_callable(call) or is_async_gen_callable(call):
|
|
|
|
|
assert isinstance(
|
|
|
|
|
stack, AsyncExitStack
|
|
|
|
|
), "Generator dependency should be called in context"
|
|
|
|
|
if is_gen_callable(call):
|
|
|
|
|
cm = run_sync_ctx_manager(contextmanager(call)(**sub_values))
|
|
|
|
|
else:
|
|
|
|
|
cm = asynccontextmanager(call)(**sub_values)
|
|
|
|
|
task = asyncio.create_task(stack.enter_async_context(cm))
|
|
|
|
|
dependency_cache[call] = task
|
2022-09-07 09:59:05 +08:00
|
|
|
|
return await task
|
2022-02-06 14:52:50 +08:00
|
|
|
|
elif is_coroutine_callable(call):
|
|
|
|
|
task = asyncio.create_task(call(**sub_values))
|
|
|
|
|
dependency_cache[call] = task
|
2022-09-07 09:59:05 +08:00
|
|
|
|
return await task
|
2022-02-06 14:52:50 +08:00
|
|
|
|
else:
|
|
|
|
|
task = asyncio.create_task(run_sync(call)(**sub_values))
|
|
|
|
|
dependency_cache[call] = task
|
2022-09-07 09:59:05 +08:00
|
|
|
|
return await task
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-09-07 09:59:05 +08:00
|
|
|
|
async def _check(self, **kwargs: Any) -> None:
|
|
|
|
|
# run sub dependent pre-checkers
|
2024-01-26 11:12:57 +08:00
|
|
|
|
await self.dependent.check(**kwargs)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BotParam(Param):
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""{ref}`nonebot.adapters.Bot` 注入参数。
|
|
|
|
|
|
|
|
|
|
本注入解析所有类型为且仅为 {ref}`nonebot.adapters.Bot` 及其子类或 `None` 的参数。
|
|
|
|
|
|
|
|
|
|
为保证兼容性,本注入还会解析名为 `bot` 且没有类型注解的参数。
|
|
|
|
|
"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
def __init__(
|
|
|
|
|
self, *args, checker: Optional[ModelField] = None, **kwargs: Any
|
|
|
|
|
) -> None:
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.checker = checker
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return (
|
|
|
|
|
"BotParam("
|
2024-01-26 11:12:57 +08:00
|
|
|
|
+ (repr(self.checker.annotation) if self.checker is not None else "")
|
2022-09-09 11:52:57 +08:00
|
|
|
|
+ ")"
|
|
|
|
|
)
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
def _check_param(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]
|
2023-08-29 18:45:12 +08:00
|
|
|
|
) -> Optional[Self]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.adapters import Bot
|
|
|
|
|
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# param type is Bot(s) or subclass(es) of Bot or None
|
|
|
|
|
if generic_check_issubclass(param.annotation, Bot):
|
|
|
|
|
checker: Optional[ModelField] = None
|
|
|
|
|
if param.annotation is not Bot:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
checker = ModelField.construct(
|
|
|
|
|
name=param.name, annotation=param.annotation, field_info=FieldInfo()
|
2023-05-21 16:01:55 +08:00
|
|
|
|
)
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls(checker=checker)
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# legacy: param is named "bot" and has no type annotation
|
|
|
|
|
elif param.annotation == param.empty and param.name == "bot":
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls()
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2024-04-16 00:33:48 +08:00
|
|
|
|
async def _solve( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
|
self, bot: "Bot", **kwargs: Any
|
|
|
|
|
) -> Any:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return bot
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2024-04-16 00:33:48 +08:00
|
|
|
|
async def _check( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
|
self, bot: "Bot", **kwargs: Any
|
|
|
|
|
) -> None:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
if self.checker is not None:
|
|
|
|
|
check_field_type(self.checker, bot)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EventParam(Param):
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""{ref}`nonebot.adapters.Event` 注入参数
|
|
|
|
|
|
|
|
|
|
本注入解析所有类型为且仅为 {ref}`nonebot.adapters.Event` 及其子类或 `None` 的参数。
|
|
|
|
|
|
|
|
|
|
为保证兼容性,本注入还会解析名为 `event` 且没有类型注解的参数。
|
|
|
|
|
"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
def __init__(
|
|
|
|
|
self, *args, checker: Optional[ModelField] = None, **kwargs: Any
|
|
|
|
|
) -> None:
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.checker = checker
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return (
|
|
|
|
|
"EventParam("
|
2024-01-26 11:12:57 +08:00
|
|
|
|
+ (repr(self.checker.annotation) if self.checker is not None else "")
|
2022-09-09 11:52:57 +08:00
|
|
|
|
+ ")"
|
|
|
|
|
)
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
def _check_param(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]
|
2023-08-29 18:45:12 +08:00
|
|
|
|
) -> Optional[Self]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.adapters import Event
|
|
|
|
|
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# param type is Event(s) or subclass(es) of Event or None
|
|
|
|
|
if generic_check_issubclass(param.annotation, Event):
|
|
|
|
|
checker: Optional[ModelField] = None
|
|
|
|
|
if param.annotation is not Event:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
checker = ModelField.construct(
|
|
|
|
|
name=param.name, annotation=param.annotation, field_info=FieldInfo()
|
2023-05-21 16:01:55 +08:00
|
|
|
|
)
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls(checker=checker)
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# legacy: param is named "event" and has no type annotation
|
|
|
|
|
elif param.annotation == param.empty and param.name == "event":
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls()
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2024-04-16 00:33:48 +08:00
|
|
|
|
async def _solve( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
|
self, event: "Event", **kwargs: Any
|
|
|
|
|
) -> Any:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return event
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2024-04-16 00:33:48 +08:00
|
|
|
|
async def _check( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
|
self, event: "Event", **kwargs: Any
|
|
|
|
|
) -> Any:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
if self.checker is not None:
|
|
|
|
|
check_field_type(self.checker, event)
|
2022-09-07 09:59:05 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
class StateParam(Param):
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""事件处理状态注入参数
|
|
|
|
|
|
|
|
|
|
本注入解析所有类型为 `T_State` 的参数。
|
|
|
|
|
|
|
|
|
|
为保证兼容性,本注入还会解析名为 `state` 且没有类型注解的参数。
|
|
|
|
|
"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return "StateParam()"
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
def _check_param(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]
|
2023-08-29 18:45:12 +08:00
|
|
|
|
) -> Optional[Self]:
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# param type is T_State
|
2024-05-09 15:08:49 +08:00
|
|
|
|
if origin_is_annotated(
|
|
|
|
|
get_origin(param.annotation)
|
|
|
|
|
) and _STATE_FLAG in get_args(param.annotation):
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls()
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# legacy: param is named "state" and has no type annotation
|
|
|
|
|
elif param.annotation == param.empty and param.name == "state":
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls()
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2024-04-16 00:33:48 +08:00
|
|
|
|
async def _solve( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
|
self, state: T_State, **kwargs: Any
|
|
|
|
|
) -> Any:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MatcherParam(Param):
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""事件响应器实例注入参数
|
|
|
|
|
|
|
|
|
|
本注入解析所有类型为且仅为 {ref}`nonebot.matcher.Matcher` 及其子类或 `None` 的参数。
|
|
|
|
|
|
|
|
|
|
为保证兼容性,本注入还会解析名为 `matcher` 且没有类型注解的参数。
|
|
|
|
|
"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
def __init__(
|
|
|
|
|
self, *args, checker: Optional[ModelField] = None, **kwargs: Any
|
|
|
|
|
) -> None:
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.checker = checker
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return (
|
|
|
|
|
"MatcherParam("
|
|
|
|
|
+ (repr(self.checker.annotation) if self.checker is not None else "")
|
|
|
|
|
+ ")"
|
|
|
|
|
)
|
2022-09-09 11:52:57 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
def _check_param(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]
|
2023-08-29 18:45:12 +08:00
|
|
|
|
) -> Optional[Self]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.matcher import Matcher
|
|
|
|
|
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# param type is Matcher(s) or subclass(es) of Matcher or None
|
|
|
|
|
if generic_check_issubclass(param.annotation, Matcher):
|
2023-06-11 15:33:33 +08:00
|
|
|
|
checker: Optional[ModelField] = None
|
|
|
|
|
if param.annotation is not Matcher:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
checker = ModelField.construct(
|
|
|
|
|
name=param.name, annotation=param.annotation, field_info=FieldInfo()
|
2023-06-11 15:33:33 +08:00
|
|
|
|
)
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls(checker=checker)
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# legacy: param is named "matcher" and has no type annotation
|
|
|
|
|
elif param.annotation == param.empty and param.name == "matcher":
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls()
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2024-04-16 00:33:48 +08:00
|
|
|
|
async def _solve( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
|
self, matcher: "Matcher", **kwargs: Any
|
|
|
|
|
) -> Any:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return matcher
|
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2024-04-16 00:33:48 +08:00
|
|
|
|
async def _check( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
|
self, matcher: "Matcher", **kwargs: Any
|
|
|
|
|
) -> Any:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
if self.checker is not None:
|
|
|
|
|
check_field_type(self.checker, matcher)
|
2023-06-11 15:33:33 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
class ArgInner:
|
|
|
|
|
def __init__(
|
|
|
|
|
self, key: Optional[str], type: Literal["message", "str", "plaintext"]
|
|
|
|
|
) -> None:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
self.key: Optional[str] = key
|
|
|
|
|
self.type: Literal["message", "str", "plaintext"] = type
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"ArgInner(key={self.key!r}, type={self.type!r})"
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
def Arg(key: Optional[str] = None) -> Any:
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""Arg 参数消息"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return ArgInner(key, "message")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ArgStr(key: Optional[str] = None) -> str:
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""Arg 参数消息文本"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return ArgInner(key, "str") # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ArgPlainText(key: Optional[str] = None) -> str:
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""Arg 参数消息纯文本"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return ArgInner(key, "plaintext") # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ArgParam(Param):
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""Arg 注入参数
|
|
|
|
|
|
|
|
|
|
本注入解析事件响应器操作 `got` 所获取的参数。
|
|
|
|
|
|
|
|
|
|
可以通过 `Arg`、`ArgStr`、`ArgPlainText` 等函数参数 `key` 指定获取的参数,
|
|
|
|
|
留空则会根据参数名称获取。
|
|
|
|
|
"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
*args,
|
|
|
|
|
key: str,
|
|
|
|
|
type: Literal["message", "str", "plaintext"],
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> None:
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.key = key
|
|
|
|
|
self.type = type
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return f"ArgParam(key={self.key!r}, type={self.type!r})"
|
2022-09-09 11:52:57 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
def _check_param(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]
|
2023-08-29 18:45:12 +08:00
|
|
|
|
) -> Optional[Self]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
if isinstance(param.default, ArgInner):
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls(key=param.default.key or param.name, type=param.default.type)
|
2023-06-24 19:18:24 +08:00
|
|
|
|
elif get_origin(param.annotation) is Annotated:
|
2023-09-14 00:14:45 +08:00
|
|
|
|
for arg in get_args(param.annotation)[:0:-1]:
|
2023-06-24 19:18:24 +08:00
|
|
|
|
if isinstance(arg, ArgInner):
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls(key=arg.key or param.name, type=arg.type)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
async def _solve( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
|
self, matcher: "Matcher", **kwargs: Any
|
|
|
|
|
) -> Any:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
message = matcher.get_arg(self.key)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
if message is None:
|
|
|
|
|
return message
|
2024-01-26 11:12:57 +08:00
|
|
|
|
if self.type == "message":
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return message
|
2024-01-26 11:12:57 +08:00
|
|
|
|
elif self.type == "str":
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return str(message)
|
|
|
|
|
else:
|
|
|
|
|
return message.extract_plain_text()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExceptionParam(Param):
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""{ref}`nonebot.message.run_postprocessor` 的异常注入参数
|
|
|
|
|
|
|
|
|
|
本注入解析所有类型为 `Exception` 或 `None` 的参数。
|
|
|
|
|
|
|
|
|
|
为保证兼容性,本注入还会解析名为 `exception` 且没有类型注解的参数。
|
|
|
|
|
"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return "ExceptionParam()"
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
def _check_param(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]
|
2023-08-29 18:45:12 +08:00
|
|
|
|
) -> Optional[Self]:
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# param type is Exception(s) or subclass(es) of Exception or None
|
|
|
|
|
if generic_check_issubclass(param.annotation, Exception):
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls()
|
2023-05-21 16:01:55 +08:00
|
|
|
|
# legacy: param is named "exception" and has no type annotation
|
|
|
|
|
elif param.annotation == param.empty and param.name == "exception":
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls()
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
async def _solve(self, exception: Optional[Exception] = None, **kwargs: Any) -> Any:
|
|
|
|
|
return exception
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DefaultParam(Param):
|
2023-05-21 16:01:55 +08:00
|
|
|
|
"""默认值注入参数
|
|
|
|
|
|
|
|
|
|
本注入解析所有剩余未能解析且具有默认值的参数。
|
|
|
|
|
|
|
|
|
|
本注入参数应该具有最低优先级,因此应该在所有其他注入参数之后使用。
|
|
|
|
|
"""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"DefaultParam(default={self.default!r})"
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
def _check_param(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cls, param: inspect.Parameter, allow_types: tuple[type[Param], ...]
|
2023-08-29 18:45:12 +08:00
|
|
|
|
) -> Optional[Self]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
if param.default != param.empty:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return cls(default=param.default)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
|
@override
|
2022-02-06 14:52:50 +08:00
|
|
|
|
async def _solve(self, **kwargs: Any) -> Any:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return PydanticUndefined
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__autodoc__ = {
|
|
|
|
|
"DependsInner": False,
|
|
|
|
|
"StateInner": False,
|
|
|
|
|
"ArgInner": False,
|
|
|
|
|
}
|