Search code examples
pythondecoratornonetype

how to fix nonetype error leaving in decorators


error problem "created a dictionary name user2 but an error occurred in running the program. this program is of authenticating the message that has been delivered to the user."

The code is as below.

user2 = {
    'name':'parth',
    'valid':True
    }

def authentication(func):
    def wrap(*args, **kwargs):
        if args[0]['valid']:
            return func(*args, **kwargs)
        return wrap

@authentication
def message_sent(user):
    print('The message has been delivered')

message_sent(user2)

Output is

Traceback (most recent call last):
  File "someFileName", line 16, in <module>
    message_sent(user2)
TypeError: 'NoneType' object is not callable

Solution

  • You have not returned anything in authentication function which is the main reason for error. You have just returned wrap inside wrap function. So outdent is what you need

    Modify authentication function like this:

    def authentication(func):
      def wrap(*args, **kwargs):
        if args[0]['valid']:
    
          return func(*args, **kwargs)
      return wrap