1
1
import datetime
2
2
from io import BytesIO
3
3
from json import JSONEncoder
4
- from typing import Any , Dict , Iterable , Iterator , List , Optional , Tuple , Type , Union , overload
4
+ from typing import Any , Dict , Generic , Iterable , Iterator , List , Optional , Tuple , Type , TypeVar , Union , overload
5
5
6
6
from django .core .handlers .wsgi import WSGIRequest
7
7
from django .http .cookie import SimpleCookie
@@ -10,6 +10,31 @@ from django.test.client import Client
10
10
from django .urls import ResolverMatch
11
11
from django .utils .datastructures import CaseInsensitiveMapping
12
12
13
+ _T = TypeVar ("_T" )
14
+ _U = TypeVar ("_U" )
15
+
16
+ class _PropertyDescriptor (Generic [_T , _U ]):
17
+ """
18
+ This helper property descriptor allows defining asynmetric getter/setters
19
+ which mypy currently doesn't support with either:
20
+
21
+ class HttpResponse:
22
+ @property
23
+ def content(...): ...
24
+ @property.setter
25
+ def content(...): ...
26
+
27
+ or:
28
+
29
+ class HttpResponse:
30
+ def _get_content(...): ...
31
+ def _set_content(...): ...
32
+ content = property(_get_content, _set_content)
33
+ """
34
+
35
+ def __get__ (self , instance : Any , owner : Optional [Any ]) -> _U : ...
36
+ def __set__ (self , instance : Any , value : _T ) -> None : ...
37
+
13
38
class BadHeaderError (ValueError ): ...
14
39
15
40
class ResponseHeaders (CaseInsensitiveMapping ):
@@ -21,7 +46,7 @@ class ResponseHeaders(CaseInsensitiveMapping):
21
46
def pop (self , key : str , default : Optional [str ] = ...) -> str : ...
22
47
def setdefault (self , key : str , value : str ) -> None : ...
23
48
24
- class HttpResponseBase ( Iterable [ Any ]) :
49
+ class HttpResponseBase :
25
50
status_code : int = ...
26
51
streaming : bool = ...
27
52
cookies : SimpleCookie = ...
@@ -72,10 +97,9 @@ class HttpResponseBase(Iterable[Any]):
72
97
def seekable (self ) -> bool : ...
73
98
def writable (self ) -> bool : ...
74
99
def writelines (self , lines : Iterable [object ]): ...
75
- def __iter__ (self ) -> Iterator [Any ]: ...
76
100
77
- class HttpResponse (HttpResponseBase ):
78
- content : Any
101
+ class HttpResponse (HttpResponseBase , Iterable [ bytes ] ):
102
+ content = _PropertyDescriptor [ object , bytes ]()
79
103
csrf_cookie_set : bool
80
104
redirect_chain : List [Tuple [str , int ]]
81
105
sameorigin : bool
@@ -85,6 +109,7 @@ class HttpResponse(HttpResponseBase):
85
109
def __init__ (self , content : object = ..., * args : Any , ** kwargs : Any ) -> None : ...
86
110
def serialize (self ) -> bytes : ...
87
111
__bytes__ = serialize
112
+ def __iter__ (self ) -> Iterator [bytes ]: ...
88
113
@property
89
114
def url (self ) -> str : ...
90
115
# Attributes assigned by monkey-patching in test client ClientHandler.__call__()
@@ -96,13 +121,12 @@ class HttpResponse(HttpResponseBase):
96
121
context : Context
97
122
resolver_match : ResolverMatch
98
123
def json (self ) -> Any : ...
99
- def __iter__ (self ): ...
100
124
def getvalue (self ) -> bytes : ...
101
125
102
- class StreamingHttpResponse (HttpResponseBase ):
103
- content : Any
104
- streaming_content : Iterator [ bytes ]
105
- def __init__ (self , streaming_content : Iterable [ bytes ] = ..., * args : Any , ** kwargs : Any ) -> None : ...
126
+ class StreamingHttpResponse (HttpResponseBase , Iterable [ bytes ] ):
127
+ streaming_content = _PropertyDescriptor [ Iterable [ object ], Iterator [ bytes ]]()
128
+ def __init__ ( self , streaming_content : Iterable [ object ] = ..., * args : Any , ** kwargs : Any ) -> None : ...
129
+ def __iter__ (self ) -> Iterator [ bytes ] : ...
106
130
def getvalue (self ) -> bytes : ...
107
131
108
132
class FileResponse (StreamingHttpResponse ):
0 commit comments