Yariv Levy
Yariv Levy

Reputation: 162

Python decorator using wraps but name still changes

I'm having the following error handler (bypasser actually) decorator -

import functools

def exception_safe(*args):
        ErrList = tuple(args)
        def decorator(f):
                @functools.wraps(f)
                def wrapper(*args, **kwargs):
                        if ErrList:
                                try:
                                        result = f(*args, **kwargs)
                                        print(f'No error!')
                                        return result 
                                except ErrList as err:
                                        print(f'got error!')
                        else:
                                try:
                                        result = f(*args, **kwargs)
                                        print(f'No error!')
                                        return result 
                                except Exception as err:
                                        print(f'got error!')
                return wrapper
        return decorator

Even though I still get the next assertion error from my pytests, telling me that the function name is 'decorator':

pytest stack

What could be a reason for that? I literally tried everything..

Upvotes: 1

Views: 61

Answers (1)

Alex Hall
Alex Hall

Reputation: 36033

exception_safe needs to be given arguments, even if there aren't any:

@exception_safe()
def function():

Upvotes: 1

Related Questions