Skip to content

WebhookHandler.add cannot cope with handler function with *args in its parameters #259

@loloicci

Description

@loloicci

Problem

When we add a function with *args to the WebhookHandler,
WebhookHandler calls the function with 0 args.
It causes some problems when we use ordinary wrapped functions with a decorator.
An example of this is as follows.

import functools

from linebot import WebhookHandler

def with_log(f):
    @functools.wraps(f)
    def _decorator(*args):
        log(f"args: {args}")
        return f(*args)
    return _decorator

handler = WebhookHandler(...)

@handler.add(...)
@with_log
def example_handler(event):
    pass

This program raises the following exception when this example_handler is called.

TypeError: example_handler() missing 1 required positional argument: 'event'

This is because of WebhookHandler.__get_args_count does not care about *args in the parameters.

Solution

Fix WebhookHandler.__get_args_count as follows.

If the added handler function has *args in its parameters, __get_args_count returns 2.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions