RFE: incorrectly defined view permission_classes throw obscure TypeError - add better error-message

90 views
Skip to first unread message

Chris BigBad

unread,
Sep 6, 2017, 11:11:46 AM9/6/17
to Django REST framework
Hi,

I thought I'd just put in a pull-request but there are quite a few hoops to jump through. Which is great for quality, but bad for a noob like me.

Said noob as spent quite some time staring at this error-message here before I figured out what was wrong:
Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/sendEmail/

Django Version: 1.11.1
Python Version: 3.4.6
Installed Applications:
...
Installed Middleware:
...



Traceback:

File "/home/me/code/venv/lib/python3.4/site-packages/django/core/handlers/exception.py" in inner
 
41.             response = get_response(request)

File "/home/me/code/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
 
187.                 response = self.process_exception_by_middleware(e, request)

File "/home/me/code/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
 
185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/me/code/venv/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
 
58.         return view_func(*args, **kwargs)

File "/home/me/code/venv/lib/python3.4/site-packages/django/views/generic/base.py" in view
 
68.             return self.dispatch(request, *args, **kwargs)

File "/home/me/code/venv/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
 
496.             response = self.handle_exception(exc)

File "/home/me/code/venv/lib/python3.4/site-packages/rest_framework/views.py" in handle_exception
 
456.             self.raise_uncaught_exception(exc)

File "/home/me/code/venv/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
 
484.             self.initial(request, *args, **kwargs)

File "/home/me/code/venv/lib/python3.4/site-packages/rest_framework/views.py" in initial
 
402.         self.check_permissions(request)

File "/home/me/code/venv/lib/python3.4/site-packages/rest_framework/views.py" in check_permissions
 
334.         for permission in self.get_permissions():

File "/home/me/code/venv/lib/python3.4/site-packages/rest_framework/views.py" in get_permissions
 
275.             return [permission() for permission in self.permission_classes]

Exception Type: TypeError at /sendEmail/
Exception Value: 'type' object is not iterable

I finally noticed that
    permission_classes = (IsAuthenticated) # Just a stupid class
does not equal
    permission_classes = (IsAuthenticated,) # a set with a class in it


As a solution, I offer this code-snippet, which would go here. Anyone is free to grab and stick through all these hoops into the DRF.
    def get_permissions(self):
       
"""
        Instantiates and returns the list of permissions that this view requires.
        """

       
try:
           
return [permission() for permission in self.permission_classes]
       
except TypeError as e:
           
raise TypeError("Make sure your view's 'permission_classes' are iterable. "
                           
+"If you use '()' to generate a set with a single element "
                           
+"make sure that there is a comma behind the one (element,).") from e


I fear it will not work in Python2, which is a requirement, right? But surely more experienced coders can make something similar, that will.
Cheers!
chris :)
Reply all
Reply to author
Forward
0 new messages