mirror of
https://github.com/davidhalter/django-stubs.git
synced 2026-04-29 02:04:39 +08:00
fix typing on HttpResponse and StreamingHttpResponse (#712)
While the documentation for `HttpResponse` and `StreamingHttpResponse` *says* `content` and `streaming_content` should be bytestrings [1] or an iterable of bytestrings respectively [2], this is not what the API supports [3] [4] and there are tests which make sure the API supports more than bytestrings [5] [6] [etc]. Before assigning `content` or `streaming_content` the code paths will call `self.make_bytes` to coerce the value to bytes. [1]: https://github.com/django/django/blob/ecf87ad513fd8af6e4a6093ed918723a7d88d5ca/django/http/response.py#L324-L327 [2]: https://github.com/django/django/blob/0a28b42b1510b8093a90718bafd7627ed67fa13b/django/http/response.py#L395-L399 [3]: https://github.com/django/django/blob/ecf87ad513fd8af6e4a6093ed918723a7d88d5ca/django/http/response.py#L342-L362 [4]: https://github.com/django/django/blob/0a28b42b1510b8093a90718bafd7627ed67fa13b/django/http/response.py#L415-L427 [5]: https://github.com/django/django/blob/0a28b42b1510b8093a90718bafd7627ed67fa13b/tests/cache/tests.py#L2250 [6]: https://github.com/django/django/blob/0a28b42b1510b8093a90718bafd7627ed67fa13b/tests/i18n/urls.py#L8
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
- case: http_response
|
||||
main: |
|
||||
from django.http.request import HttpRequest
|
||||
from django.http.response import HttpResponse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
def empty_response(request: HttpRequest) -> HttpResponse:
|
||||
return HttpResponse()
|
||||
|
||||
def str_response(request: HttpRequest) -> HttpResponse:
|
||||
return HttpResponse('It works!')
|
||||
|
||||
def bytes_response(request: HttpRequest) -> HttpResponse:
|
||||
return HttpResponse(b'It works!')
|
||||
|
||||
def object_response(request: HttpRequest) -> HttpResponse:
|
||||
return HttpResponse(_('It works!'))
|
||||
|
||||
- case: http_response_content
|
||||
main: |
|
||||
from django.http.request import HttpRequest
|
||||
from django.http.response import HttpResponse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
def empty_response(request: HttpRequest) -> HttpResponse:
|
||||
response = HttpResponse()
|
||||
reveal_type(response.content) # N: Revealed type is "builtins.bytes*"
|
||||
return response
|
||||
|
||||
def str_response(request: HttpRequest) -> HttpResponse:
|
||||
response = HttpResponse()
|
||||
response.content = 'It works!'
|
||||
reveal_type(response.content) # N: Revealed type is "builtins.bytes*"
|
||||
return response
|
||||
|
||||
def bytes_response(request: HttpRequest) -> HttpResponse:
|
||||
response = HttpResponse()
|
||||
response.content = b'It works!'
|
||||
reveal_type(response.content) # N: Revealed type is "builtins.bytes*"
|
||||
return response
|
||||
|
||||
def object_response(request: HttpRequest) -> HttpResponse:
|
||||
response = HttpResponse()
|
||||
response.content = _('It works!')
|
||||
reveal_type(response.content) # N: Revealed type is "builtins.bytes*"
|
||||
return response
|
||||
|
||||
- case: streaming_http_response
|
||||
main: |
|
||||
from django.http.request import HttpRequest
|
||||
from django.http.response import StreamingHttpResponse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
def empty_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
return StreamingHttpResponse()
|
||||
|
||||
def str_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
return StreamingHttpResponse(['It works!'])
|
||||
|
||||
def bytes_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
return StreamingHttpResponse([b'It works!'])
|
||||
|
||||
def object_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
return StreamingHttpResponse([_('It works!')])
|
||||
|
||||
def mixed_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
return StreamingHttpResponse([_('Yes'), '/', _('No')])
|
||||
|
||||
- case: streaming_http_response_streaming_content
|
||||
main: |
|
||||
from django.http.request import HttpRequest
|
||||
from django.http.response import StreamingHttpResponse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
def empty_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
response = StreamingHttpResponse()
|
||||
reveal_type(response.streaming_content) # N: Revealed type is "typing.Iterator*[builtins.bytes]"
|
||||
return response
|
||||
|
||||
def str_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
response = StreamingHttpResponse()
|
||||
response.streaming_content = ['It works!']
|
||||
reveal_type(response.streaming_content) # N: Revealed type is "typing.Iterator*[builtins.bytes]"
|
||||
return response
|
||||
|
||||
def bytes_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
response = StreamingHttpResponse()
|
||||
response.streaming_content = [b'It works!']
|
||||
reveal_type(response.streaming_content) # N: Revealed type is "typing.Iterator*[builtins.bytes]"
|
||||
return response
|
||||
|
||||
def object_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
response = StreamingHttpResponse()
|
||||
response.streaming_content = [_('It works!')]
|
||||
reveal_type(response.streaming_content) # N: Revealed type is "typing.Iterator*[builtins.bytes]"
|
||||
return response
|
||||
|
||||
def mixed_response(request: HttpRequest) -> StreamingHttpResponse:
|
||||
response = StreamingHttpResponse()
|
||||
response.streaming_content = [_('Yes'), '/', _('No')]
|
||||
reveal_type(response.streaming_content) # N: Revealed type is "typing.Iterator*[builtins.bytes]"
|
||||
return response
|
||||
Reference in New Issue
Block a user