Here is an other solution that work also if the optional argument is a callable:
def test_equal(func=None, optional_value=None):
if func is not None and optional_value is not None:
# prevent user to set func parameter manually
raise ValueError("Don't set 'func' parameter manually")
if optional_value is None:
optional_value = 10 # The default value (if needed)
def inner(function):
def func_wrapper(*args, **kwargs):
# do something
return function(*args, **kwargs) == optional_value
return func_wrapper
if not func:
return inner
return inner(func)
This way both syntax will work:
@test_equal
def does_return_10():
return 10
@test_equal(optional_value=20)
def does_return_20():
return 20
# does_return_10() return True
# does_return_20() return True