-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Perform type check on passed request argument #5618
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Nice. 👍
We only recently attempted to upgrade past 3.7.3 and encountered a problem with this. We have where we delegate to an existing ViewSet from a parent object. I think this assertion is the wrong direction. I think a potentially better way of doing this is first checking if the request is an instance of DRF's Request object and then plucking if isinstance(request, Request):
request = request._request
assert isinstance(request, django.http.HttpRequest), (msg) This would fix the original issue and avoid any problems along those lines instead of forcing us and anyone else doing something apparently unconventional to access |
@sigmavirus24 I'd happily look at that in a PR, so we can see it in action, if you want to submit one. |
Instead of flat-out erroring (which can cause unhandled exceptions) let's perhaps be nicer to users who are considerately and carefully reusing ViewSets in other ViewSets. This means accessing the private attribute for them and then (and only then) raising an AssertionError if it isn't a Django HttpResponse instance. See also encode#5618
Sort of annoying that this change appeared in a patch version (somewhere between 3.7.3 and 3.7.7)... |
Hi all - apologies for the inconvenience this PR caused. In hindsight, this change should have been added to the 3.8 milestone instead of a patch release. That said, I do think this is ultimately the correct behavior on the part of the framework, as the request is not designed to be reprocessed, and views expect to receive a Django If using this view delegation pattern, making your code compatible requires passing the underlying Django # from this
return other_view(request)
# to this
return other_view(request._request) |
Hey @rpkilby - Yeah, you're absolutely right it's the correct behavior, and I apologize for the curtness and tone of my previous comment. For what it's worth: in my particular case this change made me realize I was calling |
@rpkilby right, we're doing view delegation and we've made that change, but IMO we should absolutely not be accessing private attributes that you could pull out from under us. |
I think it’s fair to say that _request isn’t going anywhere. We could certainly consider a PR adding it to the Request docs, in a “you wouldn’t normally need this but...” type block. |
It would be better to move it to something like |
I'd be +1 for |
@carltongibson @rpkilby this breaks get_paginated_response as it still uses the restframework.response.Response object or have I been using pagination wrong? |
@liampauling Can you put an example of that in a PR? The tests are still passing The issue here is not |
The error in the test was: ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ``` The (controversial) incompatible change was in 3.7.4: encode/django-rest-framework#5618 I'll look into whether there's another way to address it. <details> <summary>Full error report</summary> ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. Stacktrace self = <lms.djangoapps.discussion_api.tests.test_views.CommentViewSetListTest testMethod=test_profile_image_request_for_null_endorsed_by> def test_profile_image_request_for_null_endorsed_by(self): """ Tests if 'endorsed' is True but 'endorsed_by' is null, the api does not crash. This is the case for some old/stale data in prod/stage environments. """ self.register_get_user_response(self.user) thread = self.make_minimal_cs_thread({ "thread_type": "question", "endorsed_responses": [make_minimal_cs_comment({ "id": "endorsed_comment", "user_id": self.user.id, "username": self.user.username, "endorsed": True, })], "non_endorsed_resp_total": 0, }) self.register_get_thread_response(thread) self.create_profile_image(self.user, get_profile_image_storage()) response = self.client.get(self.url, { "thread_id": thread["id"], "endorsed": True, > "requested_fields": "profile_image", }) lms/djangoapps/discussion_api/tests/test_views.py:1446: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:291: in get response = super(APIClient, self).get(path, data=data, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:208: in get return self.generic('GET', path, **r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:237: in generic method, path, data, content_type, secure, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:416: in generic return self.request(**r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:288: in request return super(APIClient, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:240: in request request = super(APIRequestFactory, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:501: in request six.reraise(*exc_info) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/exception.py:41: in inner response = get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:249: in _legacy_get_response response = self._get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:187: in _get_response response = self.process_exception_by_middleware(e, request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:185: in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py:185: in inner return func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:494: in dispatch response = self.handle_exception(exc) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:491: in dispatch response = handler(request, *args, **kwargs) lms/djangoapps/discussion_api/views.py:505: in list form.cleaned_data["requested_fields"], lms/djangoapps/discussion_api/api.py:659: in get_comment_list results = _serialize_discussion_entities(request, context, responses, requested_fields, DiscussionEntity.comment) lms/djangoapps/discussion_api/api.py:468: in _serialize_discussion_entities request, results, usernames, discussion_entity_type, include_profile_image lms/djangoapps/discussion_api/api.py:413: in _add_additional_response_fields username_profile_dict = _get_user_profile_dict(request, usernames=','.join(usernames)) lms/djangoapps/discussion_api/api.py:350: in _get_user_profile_dict user_profile_details = AccountViewSet.as_view({'get': 'list'})(request).data ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:477: in dispatch request = self.initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:118: in initialize_request request = super(ViewSetMixin, self).initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:381: in initialize_request parser_context=parser_context _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <rest_framework.request.Request object at 0x7f597c773890> request = <rest_framework.request.Request object at 0x7f597fa20f90> parsers = [<openedx.core.lib.api.parsers.MergePatchParser object at 0x7f59810c32d0>] authenticators = [<edx_rest_framework_extensions.auth.jwt.authentication.JwtAuthentication object at 0x7f597c713690>, <openedx.core.lib...rest_framework_extensions.auth.session.authentication.SessionAuthenticationAllowInactiveUser object at 0x7f597c773390>] negotiator = <rest_framework.negotiation.DefaultContentNegotiation object at 0x7f597fa20410> parser_context = {'args': (), 'kwargs': {}, 'view': <openedx.core.djangoapps.user_api.accounts.views.AccountViewSet object at 0x7f597c7846d0>} def __init__(self, request, parsers=None, authenticators=None, negotiator=None, parser_context=None): assert isinstance(request, HttpRequest), ( 'The `request` argument must be an instance of ' '`django.http.HttpRequest`, not `{}.{}`.' > .format(request.__class__.__module__, request.__class__.__name__) ) E AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/request.py:159: AssertionError ``` </details>
The error in the test was: ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ``` The (controversial) incompatible change was in 3.7.4: encode/django-rest-framework#5618 I'll look into whether there's another way to address it. <details> <summary>Full error report</summary> ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. Stacktrace self = <lms.djangoapps.discussion_api.tests.test_views.CommentViewSetListTest testMethod=test_profile_image_request_for_null_endorsed_by> def test_profile_image_request_for_null_endorsed_by(self): """ Tests if 'endorsed' is True but 'endorsed_by' is null, the api does not crash. This is the case for some old/stale data in prod/stage environments. """ self.register_get_user_response(self.user) thread = self.make_minimal_cs_thread({ "thread_type": "question", "endorsed_responses": [make_minimal_cs_comment({ "id": "endorsed_comment", "user_id": self.user.id, "username": self.user.username, "endorsed": True, })], "non_endorsed_resp_total": 0, }) self.register_get_thread_response(thread) self.create_profile_image(self.user, get_profile_image_storage()) response = self.client.get(self.url, { "thread_id": thread["id"], "endorsed": True, > "requested_fields": "profile_image", }) lms/djangoapps/discussion_api/tests/test_views.py:1446: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:291: in get response = super(APIClient, self).get(path, data=data, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:208: in get return self.generic('GET', path, **r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:237: in generic method, path, data, content_type, secure, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:416: in generic return self.request(**r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:288: in request return super(APIClient, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:240: in request request = super(APIRequestFactory, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:501: in request six.reraise(*exc_info) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/exception.py:41: in inner response = get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:249: in _legacy_get_response response = self._get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:187: in _get_response response = self.process_exception_by_middleware(e, request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:185: in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py:185: in inner return func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:494: in dispatch response = self.handle_exception(exc) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:491: in dispatch response = handler(request, *args, **kwargs) lms/djangoapps/discussion_api/views.py:505: in list form.cleaned_data["requested_fields"], lms/djangoapps/discussion_api/api.py:659: in get_comment_list results = _serialize_discussion_entities(request, context, responses, requested_fields, DiscussionEntity.comment) lms/djangoapps/discussion_api/api.py:468: in _serialize_discussion_entities request, results, usernames, discussion_entity_type, include_profile_image lms/djangoapps/discussion_api/api.py:413: in _add_additional_response_fields username_profile_dict = _get_user_profile_dict(request, usernames=','.join(usernames)) lms/djangoapps/discussion_api/api.py:350: in _get_user_profile_dict user_profile_details = AccountViewSet.as_view({'get': 'list'})(request).data ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:477: in dispatch request = self.initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:118: in initialize_request request = super(ViewSetMixin, self).initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:381: in initialize_request parser_context=parser_context _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <rest_framework.request.Request object at 0x7f597c773890> request = <rest_framework.request.Request object at 0x7f597fa20f90> parsers = [<openedx.core.lib.api.parsers.MergePatchParser object at 0x7f59810c32d0>] authenticators = [<edx_rest_framework_extensions.auth.jwt.authentication.JwtAuthentication object at 0x7f597c713690>, <openedx.core.lib...rest_framework_extensions.auth.session.authentication.SessionAuthenticationAllowInactiveUser object at 0x7f597c773390>] negotiator = <rest_framework.negotiation.DefaultContentNegotiation object at 0x7f597fa20410> parser_context = {'args': (), 'kwargs': {}, 'view': <openedx.core.djangoapps.user_api.accounts.views.AccountViewSet object at 0x7f597c7846d0>} def __init__(self, request, parsers=None, authenticators=None, negotiator=None, parser_context=None): assert isinstance(request, HttpRequest), ( 'The `request` argument must be an instance of ' '`django.http.HttpRequest`, not `{}.{}`.' > .format(request.__class__.__module__, request.__class__.__name__) ) E AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/request.py:159: AssertionError ``` </details>
The error in the test was: ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ``` The (controversial) incompatible change was in 3.7.4: encode/django-rest-framework#5618 I'll look into whether there's another way to address it. <details> <summary>Full error report</summary> ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. Stacktrace self = <lms.djangoapps.discussion_api.tests.test_views.CommentViewSetListTest testMethod=test_profile_image_request_for_null_endorsed_by> def test_profile_image_request_for_null_endorsed_by(self): """ Tests if 'endorsed' is True but 'endorsed_by' is null, the api does not crash. This is the case for some old/stale data in prod/stage environments. """ self.register_get_user_response(self.user) thread = self.make_minimal_cs_thread({ "thread_type": "question", "endorsed_responses": [make_minimal_cs_comment({ "id": "endorsed_comment", "user_id": self.user.id, "username": self.user.username, "endorsed": True, })], "non_endorsed_resp_total": 0, }) self.register_get_thread_response(thread) self.create_profile_image(self.user, get_profile_image_storage()) response = self.client.get(self.url, { "thread_id": thread["id"], "endorsed": True, > "requested_fields": "profile_image", }) lms/djangoapps/discussion_api/tests/test_views.py:1446: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:291: in get response = super(APIClient, self).get(path, data=data, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:208: in get return self.generic('GET', path, **r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:237: in generic method, path, data, content_type, secure, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:416: in generic return self.request(**r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:288: in request return super(APIClient, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:240: in request request = super(APIRequestFactory, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:501: in request six.reraise(*exc_info) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/exception.py:41: in inner response = get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:249: in _legacy_get_response response = self._get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:187: in _get_response response = self.process_exception_by_middleware(e, request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:185: in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py:185: in inner return func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:494: in dispatch response = self.handle_exception(exc) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:491: in dispatch response = handler(request, *args, **kwargs) lms/djangoapps/discussion_api/views.py:505: in list form.cleaned_data["requested_fields"], lms/djangoapps/discussion_api/api.py:659: in get_comment_list results = _serialize_discussion_entities(request, context, responses, requested_fields, DiscussionEntity.comment) lms/djangoapps/discussion_api/api.py:468: in _serialize_discussion_entities request, results, usernames, discussion_entity_type, include_profile_image lms/djangoapps/discussion_api/api.py:413: in _add_additional_response_fields username_profile_dict = _get_user_profile_dict(request, usernames=','.join(usernames)) lms/djangoapps/discussion_api/api.py:350: in _get_user_profile_dict user_profile_details = AccountViewSet.as_view({'get': 'list'})(request).data ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:477: in dispatch request = self.initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:118: in initialize_request request = super(ViewSetMixin, self).initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:381: in initialize_request parser_context=parser_context _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <rest_framework.request.Request object at 0x7f597c773890> request = <rest_framework.request.Request object at 0x7f597fa20f90> parsers = [<openedx.core.lib.api.parsers.MergePatchParser object at 0x7f59810c32d0>] authenticators = [<edx_rest_framework_extensions.auth.jwt.authentication.JwtAuthentication object at 0x7f597c713690>, <openedx.core.lib...rest_framework_extensions.auth.session.authentication.SessionAuthenticationAllowInactiveUser object at 0x7f597c773390>] negotiator = <rest_framework.negotiation.DefaultContentNegotiation object at 0x7f597fa20410> parser_context = {'args': (), 'kwargs': {}, 'view': <openedx.core.djangoapps.user_api.accounts.views.AccountViewSet object at 0x7f597c7846d0>} def __init__(self, request, parsers=None, authenticators=None, negotiator=None, parser_context=None): assert isinstance(request, HttpRequest), ( 'The `request` argument must be an instance of ' '`django.http.HttpRequest`, not `{}.{}`.' > .format(request.__class__.__module__, request.__class__.__name__) ) E AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/request.py:159: AssertionError ``` </details>
The error in the test was: ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ``` The (controversial) incompatible change was in 3.7.4: encode/django-rest-framework#5618 I'll look into whether there's another way to address it. <details> <summary>Full error report</summary> ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. Stacktrace self = <lms.djangoapps.discussion_api.tests.test_views.CommentViewSetListTest testMethod=test_profile_image_request_for_null_endorsed_by> def test_profile_image_request_for_null_endorsed_by(self): """ Tests if 'endorsed' is True but 'endorsed_by' is null, the api does not crash. This is the case for some old/stale data in prod/stage environments. """ self.register_get_user_response(self.user) thread = self.make_minimal_cs_thread({ "thread_type": "question", "endorsed_responses": [make_minimal_cs_comment({ "id": "endorsed_comment", "user_id": self.user.id, "username": self.user.username, "endorsed": True, })], "non_endorsed_resp_total": 0, }) self.register_get_thread_response(thread) self.create_profile_image(self.user, get_profile_image_storage()) response = self.client.get(self.url, { "thread_id": thread["id"], "endorsed": True, > "requested_fields": "profile_image", }) lms/djangoapps/discussion_api/tests/test_views.py:1446: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:291: in get response = super(APIClient, self).get(path, data=data, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:208: in get return self.generic('GET', path, **r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:237: in generic method, path, data, content_type, secure, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:416: in generic return self.request(**r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:288: in request return super(APIClient, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:240: in request request = super(APIRequestFactory, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:501: in request six.reraise(*exc_info) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/exception.py:41: in inner response = get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:249: in _legacy_get_response response = self._get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:187: in _get_response response = self.process_exception_by_middleware(e, request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:185: in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py:185: in inner return func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:494: in dispatch response = self.handle_exception(exc) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:491: in dispatch response = handler(request, *args, **kwargs) lms/djangoapps/discussion_api/views.py:505: in list form.cleaned_data["requested_fields"], lms/djangoapps/discussion_api/api.py:659: in get_comment_list results = _serialize_discussion_entities(request, context, responses, requested_fields, DiscussionEntity.comment) lms/djangoapps/discussion_api/api.py:468: in _serialize_discussion_entities request, results, usernames, discussion_entity_type, include_profile_image lms/djangoapps/discussion_api/api.py:413: in _add_additional_response_fields username_profile_dict = _get_user_profile_dict(request, usernames=','.join(usernames)) lms/djangoapps/discussion_api/api.py:350: in _get_user_profile_dict user_profile_details = AccountViewSet.as_view({'get': 'list'})(request).data ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:477: in dispatch request = self.initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:118: in initialize_request request = super(ViewSetMixin, self).initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:381: in initialize_request parser_context=parser_context _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <rest_framework.request.Request object at 0x7f597c773890> request = <rest_framework.request.Request object at 0x7f597fa20f90> parsers = [<openedx.core.lib.api.parsers.MergePatchParser object at 0x7f59810c32d0>] authenticators = [<edx_rest_framework_extensions.auth.jwt.authentication.JwtAuthentication object at 0x7f597c713690>, <openedx.core.lib...rest_framework_extensions.auth.session.authentication.SessionAuthenticationAllowInactiveUser object at 0x7f597c773390>] negotiator = <rest_framework.negotiation.DefaultContentNegotiation object at 0x7f597fa20410> parser_context = {'args': (), 'kwargs': {}, 'view': <openedx.core.djangoapps.user_api.accounts.views.AccountViewSet object at 0x7f597c7846d0>} def __init__(self, request, parsers=None, authenticators=None, negotiator=None, parser_context=None): assert isinstance(request, HttpRequest), ( 'The `request` argument must be an instance of ' '`django.http.HttpRequest`, not `{}.{}`.' > .format(request.__class__.__module__, request.__class__.__name__) ) E AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/request.py:159: AssertionError ``` </details>
The error in the test was: ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ``` The (controversial) incompatible change was in 3.7.4: encode/django-rest-framework#5618 I'll look into whether there's another way to address it. <details> <summary>Full error report</summary> ``` AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. Stacktrace self = <lms.djangoapps.discussion_api.tests.test_views.CommentViewSetListTest testMethod=test_profile_image_request_for_null_endorsed_by> def test_profile_image_request_for_null_endorsed_by(self): """ Tests if 'endorsed' is True but 'endorsed_by' is null, the api does not crash. This is the case for some old/stale data in prod/stage environments. """ self.register_get_user_response(self.user) thread = self.make_minimal_cs_thread({ "thread_type": "question", "endorsed_responses": [make_minimal_cs_comment({ "id": "endorsed_comment", "user_id": self.user.id, "username": self.user.username, "endorsed": True, })], "non_endorsed_resp_total": 0, }) self.register_get_thread_response(thread) self.create_profile_image(self.user, get_profile_image_storage()) response = self.client.get(self.url, { "thread_id": thread["id"], "endorsed": True, > "requested_fields": "profile_image", }) lms/djangoapps/discussion_api/tests/test_views.py:1446: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:291: in get response = super(APIClient, self).get(path, data=data, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:208: in get return self.generic('GET', path, **r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:237: in generic method, path, data, content_type, secure, **extra) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:416: in generic return self.request(**r) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:288: in request return super(APIClient, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/test.py:240: in request request = super(APIRequestFactory, self).request(**kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/test/client.py:501: in request six.reraise(*exc_info) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/exception.py:41: in inner response = get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:249: in _legacy_get_response response = self._get_response(request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:187: in _get_response response = self.process_exception_by_middleware(e, request) ../venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py:185: in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py:185: in inner return func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:494: in dispatch response = self.handle_exception(exc) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:491: in dispatch response = handler(request, *args, **kwargs) lms/djangoapps/discussion_api/views.py:505: in list form.cleaned_data["requested_fields"], lms/djangoapps/discussion_api/api.py:659: in get_comment_list results = _serialize_discussion_entities(request, context, responses, requested_fields, DiscussionEntity.comment) lms/djangoapps/discussion_api/api.py:468: in _serialize_discussion_entities request, results, usernames, discussion_entity_type, include_profile_image lms/djangoapps/discussion_api/api.py:413: in _add_additional_response_fields username_profile_dict = _get_user_profile_dict(request, usernames=','.join(usernames)) lms/djangoapps/discussion_api/api.py:350: in _get_user_profile_dict user_profile_details = AccountViewSet.as_view({'get': 'list'})(request).data ../venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py:58: in wrapped_view return view_func(*args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:95: in view return self.dispatch(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:477: in dispatch request = self.initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/viewsets.py:118: in initialize_request request = super(ViewSetMixin, self).initialize_request(request, *args, **kwargs) ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py:381: in initialize_request parser_context=parser_context _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <rest_framework.request.Request object at 0x7f597c773890> request = <rest_framework.request.Request object at 0x7f597fa20f90> parsers = [<openedx.core.lib.api.parsers.MergePatchParser object at 0x7f59810c32d0>] authenticators = [<edx_rest_framework_extensions.auth.jwt.authentication.JwtAuthentication object at 0x7f597c713690>, <openedx.core.lib...rest_framework_extensions.auth.session.authentication.SessionAuthenticationAllowInactiveUser object at 0x7f597c773390>] negotiator = <rest_framework.negotiation.DefaultContentNegotiation object at 0x7f597fa20410> parser_context = {'args': (), 'kwargs': {}, 'view': <openedx.core.djangoapps.user_api.accounts.views.AccountViewSet object at 0x7f597c7846d0>} def __init__(self, request, parsers=None, authenticators=None, negotiator=None, parser_context=None): assert isinstance(request, HttpRequest), ( 'The `request` argument must be an instance of ' '`django.http.HttpRequest`, not `{}.{}`.' > .format(request.__class__.__module__, request.__class__.__name__) ) E AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`. ../venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/request.py:159: AssertionError ``` </details>
* Add test for wrapped request instance * Add 'request' argument type check to Request init * Fix metadata tests' request object
Fixes #2771 by implementing a simple check on the
request
argument's type.