66import aiohttp
77import yarl
88import io
9- from typing import Union , ByteString , Callable , Any
9+ from typing import Union , ByteString , Callable , TypedDict
10+
11+ class XHRResponse (TypedDict , total = True ):
12+ """
13+ See definitions in `XMLHttpRequest-internal.d.ts`
14+ """
15+ url : str
16+ status : int
17+ statusText : str
18+ contentLength : int
19+ getResponseHeader : Callable [[str ], Union [str , None ]]
20+ getAllResponseHeaders : Callable [[], str ]
1021
1122async def request (
1223 method : str ,
@@ -17,7 +28,7 @@ async def request(
1728 processRequestBodyChunkLength : Callable [[int ], None ],
1829 processRequestEndOfBody : Callable [[], None ],
1930 # callbacks for response progress
20- processResponse : Callable [[Any ], None ],
31+ processResponse : Callable [[XHRResponse ], None ],
2132 processBodyChunk : Callable [[bytearray ], None ],
2233 processEndOfBody : Callable [[], None ],
2334 /
@@ -52,15 +63,15 @@ def getAllResponseHeaders():
5263 return "\r \n " .join (headers )
5364
5465 # readyState HEADERS_RECEIVED
55- responseData = { # FIXME: PythonMonkey bug: the dict will be GCed if directly as an argument
66+ responseData : XHRResponse = { # FIXME: PythonMonkey bug: the dict will be GCed if directly as an argument
5667 'url' : str (res .real_url ),
5768 'status' : res .status ,
58- 'statusText' : res .reason ,
69+ 'statusText' : str ( res .reason or '' ) ,
5970
6071 'getResponseHeader' : getResponseHeader ,
6172 'getAllResponseHeaders' : getAllResponseHeaders ,
6273
63- 'contentLength' : res .content_length ,
74+ 'contentLength' : res .content_length or 0 ,
6475 }
6576 processResponse (responseData )
6677
0 commit comments