⚗️ finish handler class

This commit is contained in:
yanyongyu 2021-03-18 21:30:52 +08:00
parent 6bd12b7f2c
commit e7dc4e1eec
2 changed files with 13 additions and 15 deletions

View File

@ -75,19 +75,19 @@ class Handler(metaclass=HandlerMeta):
@property @property
def event_type(self) -> Optional[Type["Event"]]: def event_type(self) -> Optional[Type["Event"]]:
if "event" not in self.signature: if "event" not in self.signature.parameters:
return None return None
return self.signature.parameters["event"].annotation return self.signature.parameters["event"].annotation
@property @property
def state_type(self) -> Optional[T_State]: def state_type(self) -> Optional[T_State]:
if "state" not in self.signature: if "state" not in self.signature.parameters:
return None return None
return self.signature.parameters["state"].annotation return self.signature.parameters["state"].annotation
@property @property
def matcher_type(self) -> Optional[Type["Matcher"]]: def matcher_type(self) -> Optional[Type["Matcher"]]:
if "matcher" not in self.signature: if "matcher" not in self.signature.parameters:
return None return None
return self.signature.parameters["matcher"].annotation return self.signature.parameters["matcher"].annotation
@ -97,12 +97,11 @@ class Handler(metaclass=HandlerMeta):
self._check_params(signature) self._check_params(signature)
self._check_bot_param(signature) self._check_bot_param(signature)
self._check_bot_param(wrapped_signature) self._check_bot_param(wrapped_signature)
signature.parameters["bot"].annotation = wrapped_signature.parameters[ signature.parameters["bot"].replace(
"bot"].annotation annotation=wrapped_signature.parameters["bot"].annotation)
if "event" in wrapped_signature.parameters and "event" in signature.parameters: if "event" in wrapped_signature.parameters and "event" in signature.parameters:
signature.parameters[ signature.parameters["event"].replace(
"event"].annotation = wrapped_signature.parameters[ annotation=wrapped_signature.parameters["event"].annotation)
"event"].annotation
return signature return signature
def update_signature( def update_signature(
@ -124,7 +123,7 @@ class Handler(metaclass=HandlerMeta):
def _get_typed_signature(self, def _get_typed_signature(self,
follow_wrapped: bool = True) -> inspect.Signature: follow_wrapped: bool = True) -> inspect.Signature:
signature = inspect.signature(self.func, follow_wrapped=follow_wrapped) signature = inspect.signature(self.func, follow_wrapped=follow_wrapped)
globalns = getattr(self, "__globals__", {}) globalns = getattr(self.func, "__globals__", {})
typed_params = [ typed_params = [
inspect.Parameter( inspect.Parameter(
name=param.name, name=param.name,
@ -149,7 +148,7 @@ class Handler(metaclass=HandlerMeta):
return param.annotation return param.annotation
def _check_params(self, signature: inspect.Signature): def _check_params(self, signature: inspect.Signature):
if not set(signature.parameters.keys()) < { if not set(signature.parameters.keys()) <= {
"bot", "event", "state", "matcher" "bot", "event", "state", "matcher"
}: }:
raise ValueError( raise ValueError(

View File

@ -319,9 +319,6 @@ class Matcher(metaclass=MatcherMeta):
""" """
def _decorator(func: T_Handler) -> T_Handler: def _decorator(func: T_Handler) -> T_Handler:
print(
sys._getframe(1).f_code.co_filename,
sys._getframe(1).f_code.co_name)
cls.append_handler(func) cls.append_handler(func)
return func return func
@ -351,7 +348,9 @@ class Matcher(metaclass=MatcherMeta):
def _decorator(func: T_Handler) -> T_Handler: def _decorator(func: T_Handler) -> T_Handler:
if not cls.handlers or cls.handlers[-1] is not func: if not cls.handlers or cls.handlers[-1] is not func:
func_handler = cls.append_handler(func) func_handler = cls.append_handler(func)
receive_handler.update_signature(bot=func_handler.bot_type, if receive_handler:
receive_handler.update_signature(
bot=func_handler.bot_type,
event=func_handler.event_type) event=func_handler.event_type)
return func return func