Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing for web_exceptions #3284

Merged
merged 5 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/1749.feature
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Add type hints to Application and Response
Add type hints to Exceptions
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ Thanos Lefteris
Thijs Vermeir
Thomas Forbes
Thomas Grainger
Tomasz Trebski
Tolga Tezel
Trinh Hoang Nhu
Vadim Suharnikov
Expand Down
49 changes: 39 additions & 10 deletions aiohttp/web_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Any, Dict, List, Optional # noqa

from .typedefs import LooseHeaders, StrOrURL
from .web_response import Response


Expand Down Expand Up @@ -76,16 +79,20 @@ class HTTPException(Response, Exception):

__http_exception__ = True

def __init__(self, *, headers=None, reason=None,
body=None, text=None, content_type=None):
def __init__(self, *,
headers: Optional[LooseHeaders]=None,
reason: Optional[str]=None,
body: Any=None,
text: Optional[str]=None,
content_type: Optional[str]=None) -> None:
Response.__init__(self, status=self.status_code,
headers=headers, reason=reason,
body=body, text=text, content_type=content_type)
Exception.__init__(self, self.reason)
if self.body is None and not self.empty_body:
self.text = "{}: {}".format(self.status, self.reason)

def __bool__(self):
def __bool__(self) -> bool:
return True


Expand Down Expand Up @@ -138,8 +145,14 @@ class HTTPPartialContent(HTTPSuccessful):

class _HTTPMove(HTTPRedirection):

def __init__(self, location, *, headers=None, reason=None,
body=None, text=None, content_type=None):
def __init__(self,
location: StrOrURL,
*,
headers: Optional[LooseHeaders]=None,
reason: Optional[str]=None,
body: Any=None,
text: Optional[str]=None,
content_type: Optional[str]=None) -> None:
if not location:
raise ValueError("HTTP redirects need a location to redirect to.")
super().__init__(headers=headers, reason=reason,
Expand Down Expand Up @@ -217,8 +230,15 @@ class HTTPNotFound(HTTPClientError):
class HTTPMethodNotAllowed(HTTPClientError):
status_code = 405

def __init__(self, method, allowed_methods, *, headers=None, reason=None,
body=None, text=None, content_type=None):
def __init__(self,
method: str,
allowed_methods: List[str],
*,
headers: Optional[LooseHeaders]=None,
reason: Optional[str]=None,
body: Any=None,
text: Optional[str]=None,
content_type: Optional[str]=None) -> None:
allow = ','.join(sorted(allowed_methods))
super().__init__(headers=headers, reason=reason,
body=body, text=text, content_type=content_type)
Expand Down Expand Up @@ -258,7 +278,10 @@ class HTTPPreconditionFailed(HTTPClientError):
class HTTPRequestEntityTooLarge(HTTPClientError):
status_code = 413

def __init__(self, max_size, actual_size, **kwargs):
def __init__(self,
max_size: float,
actual_size: float,
**kwargs: Any) -> None:
kwargs.setdefault(
'text',
'Maximum request body size {} exceeded, '
Expand Down Expand Up @@ -314,8 +337,14 @@ class HTTPRequestHeaderFieldsTooLarge(HTTPClientError):
class HTTPUnavailableForLegalReasons(HTTPClientError):
status_code = 451

def __init__(self, link, *, headers=None, reason=None,
body=None, text=None, content_type=None):
def __init__(self,
link: str,
*,
headers: Optional[LooseHeaders]=None,
reason: Optional[str]=None,
body: Any=None,
text: Optional[str]=None,
content_type: Optional[str]=None) -> None:
super().__init__(headers=headers, reason=reason,
body=body, text=text, content_type=content_type)
self.headers['Link'] = '<%s>; rel="blocked-by"' % link
Expand Down