Skip to content

Commit 044996a

Browse files
kornicameisterasvetlov
authored andcommitted
Add typing for web_exceptions (#3284)
1 parent 6cb85a7 commit 044996a

3 files changed

Lines changed: 41 additions & 10 deletions

File tree

CHANGES/1749.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Add type hints to Application and Response
2+
Add type hints to Exceptions

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Thanos Lefteris
195195
Thijs Vermeir
196196
Thomas Forbes
197197
Thomas Grainger
198+
Tomasz Trebski
198199
Tolga Tezel
199200
Trinh Hoang Nhu
200201
Vadim Suharnikov

aiohttp/web_exceptions.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import Any, Dict, List, Optional # noqa
2+
3+
from .typedefs import LooseHeaders, StrOrURL
14
from .web_response import Response
25

36

@@ -76,16 +79,20 @@ class HTTPException(Response, Exception):
7679

7780
__http_exception__ = True
7881

79-
def __init__(self, *, headers=None, reason=None,
80-
body=None, text=None, content_type=None):
82+
def __init__(self, *,
83+
headers: Optional[LooseHeaders]=None,
84+
reason: Optional[str]=None,
85+
body: Any=None,
86+
text: Optional[str]=None,
87+
content_type: Optional[str]=None) -> None:
8188
Response.__init__(self, status=self.status_code,
8289
headers=headers, reason=reason,
8390
body=body, text=text, content_type=content_type)
8491
Exception.__init__(self, self.reason)
8592
if self.body is None and not self.empty_body:
8693
self.text = "{}: {}".format(self.status, self.reason)
8794

88-
def __bool__(self):
95+
def __bool__(self) -> bool:
8996
return True
9097

9198

@@ -138,8 +145,14 @@ class HTTPPartialContent(HTTPSuccessful):
138145

139146
class _HTTPMove(HTTPRedirection):
140147

141-
def __init__(self, location, *, headers=None, reason=None,
142-
body=None, text=None, content_type=None):
148+
def __init__(self,
149+
location: StrOrURL,
150+
*,
151+
headers: Optional[LooseHeaders]=None,
152+
reason: Optional[str]=None,
153+
body: Any=None,
154+
text: Optional[str]=None,
155+
content_type: Optional[str]=None) -> None:
143156
if not location:
144157
raise ValueError("HTTP redirects need a location to redirect to.")
145158
super().__init__(headers=headers, reason=reason,
@@ -217,8 +230,15 @@ class HTTPNotFound(HTTPClientError):
217230
class HTTPMethodNotAllowed(HTTPClientError):
218231
status_code = 405
219232

220-
def __init__(self, method, allowed_methods, *, headers=None, reason=None,
221-
body=None, text=None, content_type=None):
233+
def __init__(self,
234+
method: str,
235+
allowed_methods: List[str],
236+
*,
237+
headers: Optional[LooseHeaders]=None,
238+
reason: Optional[str]=None,
239+
body: Any=None,
240+
text: Optional[str]=None,
241+
content_type: Optional[str]=None) -> None:
222242
allow = ','.join(sorted(allowed_methods))
223243
super().__init__(headers=headers, reason=reason,
224244
body=body, text=text, content_type=content_type)
@@ -258,7 +278,10 @@ class HTTPPreconditionFailed(HTTPClientError):
258278
class HTTPRequestEntityTooLarge(HTTPClientError):
259279
status_code = 413
260280

261-
def __init__(self, max_size, actual_size, **kwargs):
281+
def __init__(self,
282+
max_size: float,
283+
actual_size: float,
284+
**kwargs: Any) -> None:
262285
kwargs.setdefault(
263286
'text',
264287
'Maximum request body size {} exceeded, '
@@ -314,8 +337,14 @@ class HTTPRequestHeaderFieldsTooLarge(HTTPClientError):
314337
class HTTPUnavailableForLegalReasons(HTTPClientError):
315338
status_code = 451
316339

317-
def __init__(self, link, *, headers=None, reason=None,
318-
body=None, text=None, content_type=None):
340+
def __init__(self,
341+
link: str,
342+
*,
343+
headers: Optional[LooseHeaders]=None,
344+
reason: Optional[str]=None,
345+
body: Any=None,
346+
text: Optional[str]=None,
347+
content_type: Optional[str]=None) -> None:
319348
super().__init__(headers=headers, reason=reason,
320349
body=body, text=text, content_type=content_type)
321350
self.headers['Link'] = '<%s>; rel="blocked-by"' % link

0 commit comments

Comments
 (0)