django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

"_MonkeyPatchedWSGIResponse" has no attribute "data"

Open ollejernstrom opened this issue 1 year ago • 3 comments

"_MonkeyPatchedWSGIResponse" has no attribute "data" test.py:106: error: "_MonkeyPatchedWSGIResponse" has no attribute "data" [attr-defined]sop/tests/test_task_copy.py:106: error: "_MonkeyPatchedWSGIResponse" has no attribute "data" [attr-defined]

Does anyone know why this is?

ollejernstrom avatar Dec 17 '23 19:12 ollejernstrom

Why do you think data attribute should exist? I could be missing something, but I don't see it in any of the Django BaseHttpResponse classes.

JsonResponse() accepts a data= constructor argument, but it's not assigned to a property named data.

intgr avatar Dec 18 '23 12:12 intgr

Also don't see it documented here: https://docs.djangoproject.com/en/5.0/topics/testing/tools/ (only has data= argument on request methods)

intgr avatar Dec 18 '23 12:12 intgr

Could this be related to Django Rest Framework?

Because I had the same issue when using django.test.TransactionTestCase instead of rest_framework.test.APITransactionTestCase. Somehow responses from self.client still had the data property, which are a feature of APIClient (s. https://www.django-rest-framework.org/api-guide/testing/#checking-the-response-data).

The fix would be to use the correct TestCase class, however I'm not sure why the property is there in the first place when using the vanilla Django TestCase.

JannKleen avatar Mar 15 '24 15:03 JannKleen