Allow Views to return the more generic HttpResponseBase instead of HttpResponse, to allow returning StreamingHttpResponse (#607)

* Add type hints for the postgres CursorDebugWrapper, expand the BaseCursorDebugWrapper.

* Fix how Optinal gets applied.

* Fix optional handling further.

* Adjust postgres debugcursorwrapper to look more like the implementation.

* Apply review feedback.

* Use the more generic HttpResponseBase when appriopriate.

* Fix failing test and add new test.

* Remove the other test again, it was the wrong location. Add new tests in the correct location.

Co-authored-by: LanDinh <coding+sourcetree@khaleesi.ninja>
This commit is contained in:
LanDinh
2021-05-01 23:33:05 +02:00
committed by GitHub
parent 2a1c86744c
commit 9251520f66
3 changed files with 30 additions and 11 deletions

View File

@@ -11,10 +11,10 @@
from django.views.generic.base import View
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.http.response import HttpResponse
from django.http.response import HttpResponseBase
from django.http.request import HttpRequest
class TestView(View):
@method_decorator(login_required)
def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponseBase:
return super().dispatch(request, *args, **kwargs)
reveal_type(dispatch) # N: Revealed type is 'def (self: main.TestView, request: django.http.request.HttpRequest, *args: Any, **kwargs: Any) -> django.http.response.HttpResponse'
reveal_type(dispatch) # N: Revealed type is 'def (self: main.TestView, request: django.http.request.HttpRequest, *args: Any, **kwargs: Any) -> django.http.response.HttpResponseBase'

View File

@@ -10,3 +10,21 @@
"""Ensure that form can have type AuthenticationForm."""
form.get_user()
return HttpResponseRedirect(self.get_success_url())
- case: dispatch_http_response
main: |
from django.http import HttpResponse
from django.views.generic.base import View
class MyView(View):
def dispatch(self, request, *args, **kwargs) -> HttpResponse:
response: HttpResponse
return response
- case: dispatch_streaming_http_response
main: |
from django.http import StreamingHttpResponse
from django.views.generic.base import View
class MyView(View):
def dispatch(self, request, *args, **kwargs) -> StreamingHttpResponse:
response: StreamingHttpResponse
return response