def __call__(self, fn, *args, **kwargs):
try:
fn._decorators.append(self)
except AttributeError:
fn._decorators = [self]
# Verify function signature
argspec = inspect.getfullargspec(fn)
n_args = len(argspec.args) - 1 if 'self' in argspec.args else len(argspec.args)
assert n_args >= 1, 'Functions decorated with @Subscribe must have a parameter for the message.'
n_required_args = n_args - (len(argspec.defaults) if argspec.defaults else 0)
assert n_required_args <= 1, 'Functions decorated with @Subscribe can only have one required parameter.'
# Add typing information if not already defined
first_arg = argspec.args[1] if 'self' in argspec.args else argspec.args[0]
if first_arg not in argspec.annotations.keys():
fn.__annotations__[first_arg] = self.subs[-1]
return fn
评论列表
文章目录