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

error: "Response" has no attribute "json"

Open rr- opened this issue 3 years ago • 0 comments

Bug report

What's wrong

Given the snippet such as this

from rest_framework.response import Response
from rest_framework.test import APIClient


def test_response(api_client: APIClient) -> None:
    response: Response = api_client.get('https://example.com/example.json')
    assert response.json() == {}

and running mypy . on it, I receive error: "Response" has no attribute "json" Running the test works fine.

The reason is that rest_framework-stubs/response.pyi declares Response to inherit from django.template.response.SimpleTemplateResponse and this class in turn does not declare the json field, it's the TemplateResponse that does.

This was already reported in django-stubs here: https://github.com/typeddjango/django-stubs/issues/962 I'm not that person.

How is that should be

Since the test works mypy shouldn't complain that the method Response.json() does not exist.

System information

  • OS: Linux
  • python version: 3.8.11
  • django version: 3.0.14
  • mypy version: 0.950
  • django-stubs version: 1.11.0

rr- avatar Jun 09 '22 11:06 rr-