Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when Authentication fails: __init__() takes from 1 to 2 positional arguments but 3 were given #144

Open
naelmusleh opened this issue Aug 2, 2019 · 4 comments

Comments

@naelmusleh
Copy link

naelmusleh commented Aug 2, 2019

I have the following in my views.py

from rest_framework_simplejwt.views import TokenObtainPairView
from .serializers import CustomTokenObtainPairSerializer

class CustomTokenObtainPairView(TokenObtainPairView):
    serializer_class = CustomTokenObtainPairSerializer

And in my serializers.py

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from backend.users.serializers import UserSerializer

class CustomTokenObtainPairSerializer(TokenObtainPairSerializer):
    def validate(self, attrs):
        tokens = super(CustomTokenObtainPairSerializer, self).validate(attrs)

        data = {
            "tokens": tokens,
            "user": UserSerializer(self.user).data
        }
        return data

When Authentication is successful, data is returned with no issues. But when Authentication fails, instead of getting a JSON with the error message, I get the following Django Exception:

__init__() takes from 1 to 2 positional arguments but 3 were given

Traceback:

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python3.5/contextlib.py" in inner
  30.                 return func(*args, **kwds)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  466.             response = self.handle_exception(exc)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework_simplejwt/views.py" in post
  27.             serializer.is_valid(raise_exception=True)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/serializers.py" in is_valid
  213.                 self._validated_data = self.run_validation(self.initial_data)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework/serializers.py" in run_validation
  410.             value = self.validate(value)

File "/vagrant/Devel/sample_app/sample_app/backend/apps/api/auth/serializers.py" in validate
  15.         tokens = super(CustomTokenObtainPairSerializer, self).validate(attrs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework_simplejwt/serializers.py" in validate
  71.         data = super().validate(attrs)

File "/vagrant/Devel/sample_app/sample_app/env/lib/python3.5/site-packages/rest_framework_simplejwt/serializers.py" in validate
  55.                 'no_active_account',

Exception Type: TypeError at /api/auth/token/obtain/
Exception Value: __init__() takes from 1 to 2 positional arguments but 3 were given

@haristimuno-omnibnk
Copy link

Did you solve this issuer?

@hvitis
Copy link

hvitis commented Mar 16, 2020

Reviving issue, any ideas?

@Andrew-Chen-Wang
Copy link
Member

Andrew-Chen-Wang commented Mar 16, 2020

@naelmusleh @haristimuno-omnibnk @hvitis I’m not sure where the problem could lie to be honest IN GENERAL. But if you take a look at the original def validate, it already returns something... so having token = super() doesn’t really make (at first glance, it seemed like an infinite loop, but I guess not). You’re getting this because you’ve supplied validate with super().validate(attrs) which in turn makes the serializer’s validate method use self.validate(attrs, attrs) which is giving you this error.

So instead of using super(), rewrite the entire validate method.

Another theory which is not as good since the above is probably the correct identification of the problem: Based on traceback error, you’re missing the whole “not authenticated part” as in the default:

if self.user is None or not self.user.is_active:
            raise exceptions.AuthenticationFailed(
                self.error_messages['no_active_account'],
                'no_active_account',
            )

        return {}

If you take a look at what the original problem was, we have a default “no_account_account” error that should’ve been raised, but I suppose wasn’t based on the original poster’s serializer... which shouldn’t have touched the validation part to be honest.

@hvitis
Copy link

hvitis commented Aug 15, 2020

@Andrew-Chen-Wang @naelmusleh @haristimuno-omnibnk

🎉 🎊 Fixed it. 🎉 🎊 I was unnecessarily adding the extended, customized serializer to my settings.
Deleting it from settings.

Now my extending process looks like this:
serializer > view > url

No need to change anything in settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants