Decorator used for debugging - has problems

Hi , and thanks for awesome Sanic ! am slowly understanding it better… :pray:t2::blush:

Problem - I use this decorator sometimes to help with debugging, to show me the args with which a function was called. I mean - with the function itself still running, obviously. Only problem is, in Sanic, the decorator still works and prints the args, but the underlying function doesn’t run :man_shrugging:t2:

Is there some way I can modify my decorator so it works as intended in Sanic ?

Decorator:

def print_args(func):
    async def wrapper(*args, **kwargs):
        print(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
        return await func(*args, **kwargs)
    return wrapper

Example function:

    async def insert_data_AR(self, 
        ip_address: str, 
        user_agent: str,
        AR_status_code: int,
        AR_request_id: str,
        AR_user_id: str,
        AR_email_id: str,
        AR_email: str,
        AR_email_verified: bool,
        AR_created_at: str,
        AR_status: str,
        AR_reset_sessions: bool,
        AR_raw_json: str
        ) -> None:
        ...

Your decorator seems correct, although you might wish to use @functools.wraps in it. If you apply it to a handler or another thing that also has other decorators, the order of decorators matters. You should probably put this as close to the function as possible (after other decorators). Other than that, there should be nothing special with Sanic.

If you cannot get it working, could you make a test case, a minimal Sanic app that shows the problem?

Yes, you will absolutely want to put it as close to the function as possible. The route definition decorator should be the top most.

Checkout the page on the docs for more information

1 Like