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

return type for django.views.static is less specific than expected.

Open kezabelle opened this issue 3 years ago • 1 comments

What's wrong

The current definition for django.views.static.serve specifies the return type as HttpResponseBase. Prior to that. it was specified as FileResponse.

How is that should be

As far as I'm aware, the return type for django.views.static.serve is actually: Union[HttpResponse, HttpResponseNotModified, FileResponse]

Specifically:

  • HttpResponse is via directory_index
  • HttpResponseNotModified is for If-Modified-Since
  • FileResponse is the file sending itself.

System information

  • django version: Any, I think. The last version to offer another return type was 1.10.6 before the redirect functionality was removed.
  • mypy version: 0.960
  • django-stubs version: master

kezabelle avatar May 27 '22 10:05 kezabelle

Is there any practical difference for you between HttpResponseBase and HttpResponse | FileResponse? It is not a single place where HttpResponse | FileResponse are widened to HttpResponseBase, this makes subclassing easier plus is more readable in general. Could you show a use case where it changes type checking process (adds or removes errors or affects implementation)?

HttpResponseNotModified is a subclass of HttpResponse AFAIR, so doesn't need to be included in union.

sterliakov avatar Jun 14 '22 17:06 sterliakov