'How to detect missing arguments in decorator?

I would like to define a decorator with a parameter that raises an error if the parameter is missing.

Here's a naive attempt on a simplified example:

def decorator_with_arg(a=None):

    if a is None :
        raise ValueError("Missing argument in decorator")

    def decorator(func):

        def wrapped_func(x):
            return func(x+ a)

        return wrapped_func

    return decorator

But when I use this decorator without a parameter it's not raising any errors:

@decorator_with_arg
def simple_func(x):
    return 2*x

simple_func(1)

How can I raise an exception?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source