nonebot2/nonebot/params.py

77 lines
2.2 KiB
Python
Raw Normal View History

2021-11-15 21:44:24 +08:00
import inspect
from typing import Any, Dict, Optional
from pydantic.fields import Undefined
2021-11-15 21:44:24 +08:00
from nonebot.typing import T_State
from nonebot.dependencies import Param
2021-11-15 21:44:24 +08:00
from nonebot.adapters import Bot, Event
from nonebot.utils import generic_check_issubclass
2021-11-15 21:44:24 +08:00
class BotParam(Param):
@classmethod
def _check(cls, name: str, param: inspect.Parameter) -> bool:
return generic_check_issubclass(param.annotation, Bot) or (
param.annotation == param.empty and name == "bot"
)
2021-11-15 21:44:24 +08:00
def _solve(self, bot: Bot, **kwargs: Any) -> Any:
return bot
class EventParam(Param):
@classmethod
def _check(cls, name: str, param: inspect.Parameter) -> bool:
return generic_check_issubclass(param.annotation, Event) or (
param.annotation == param.empty and name == "event"
)
2021-11-15 21:44:24 +08:00
def _solve(self, event: Event, **kwargs: Any) -> Any:
return event
class StateParam(Param):
@classmethod
def _check(cls, name: str, param: inspect.Parameter) -> bool:
return generic_check_issubclass(param.annotation, Dict) or (
param.annotation == param.empty and name == "state"
)
2021-11-15 21:44:24 +08:00
def _solve(self, state: T_State, **kwargs: Any) -> Any:
return state
class MatcherParam(Param):
@classmethod
def _check(cls, name: str, param: inspect.Parameter) -> bool:
return generic_check_issubclass(param.annotation, Matcher) or (
param.annotation == param.empty and name == "matcher"
)
2021-11-15 21:44:24 +08:00
def _solve(self, matcher: Optional["Matcher"] = None, **kwargs: Any) -> Any:
return matcher
class ExceptionParam(Param):
@classmethod
def _check(cls, name: str, param: inspect.Parameter) -> bool:
return generic_check_issubclass(param.annotation, Exception) or (
param.annotation == param.empty and name == "exception"
)
2021-11-15 21:44:24 +08:00
def _solve(self, exception: Optional[Exception] = None, **kwargs: Any) -> Any:
2021-11-15 21:44:24 +08:00
return exception
class DefaultParam(Param):
@classmethod
def _check(cls, name: str, param: inspect.Parameter) -> bool:
return param.default != param.empty
def _solve(self, **kwargs: Any) -> Any:
return Undefined
from nonebot.matcher import Matcher