From 5086076590c9423b06a70581787d0001f80f4d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Sun, 1 Mar 2020 13:21:02 +0200 Subject: [PATCH] Linter --- sanic/app.py | 4 +--- sanic/asgi.py | 16 ++++++++++------ sanic/http.py | 14 +++++++++----- sanic/request.py | 14 ++++++++------ sanic/response.py | 5 ++--- sanic/server.py | 10 +++++++++- sanic/testing.py | 4 +++- 7 files changed, 42 insertions(+), 25 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index c471855b..95c871ff 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -968,7 +968,6 @@ class Sanic: ) return response - async def handle_request(self, request): """Take a request from the HTTP Server and return a response object to be sent back The HTTP Server only expects a response object, so @@ -1062,7 +1061,6 @@ class Sanic: response = await self.handle_exception(request, e, name) await request.respond(response).send(end_stream=True) - # -------------------------------------------------------------------- # # Testing # -------------------------------------------------------------------- # @@ -1092,7 +1090,7 @@ class Sanic: stop_event: Any = None, register_sys_signals: bool = True, access_log: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> None: """Run the HTTP Server and listen until keyboard interrupt or term signal. On termination, drain connections before closing. diff --git a/sanic/asgi.py b/sanic/asgi.py index a49ae633..879ee1b9 100644 --- a/sanic/asgi.py +++ b/sanic/asgi.py @@ -345,13 +345,17 @@ class ASGIApp: await self.transport.send(self.response_start) self.response_start = None if self.response_body: - data = self.response_body + data if data else self.response_body + data = ( + self.response_body + data if data else self.response_body + ) self.response_body = None - await self.transport.send({ - "type": "http.response.body", - "body": data.encode() if hasattr(data, "encode") else data, - "more_body": not end_stream, - }) + await self.transport.send( + { + "type": "http.response.body", + "body": data.encode() if hasattr(data, "encode") else data, + "more_body": not end_stream, + } + ) async def __call__(self) -> None: """ diff --git a/sanic/http.py b/sanic/http.py index c899831a..034e32f7 100644 --- a/sanic/http.py +++ b/sanic/http.py @@ -28,6 +28,7 @@ class Stage(Enum): HTTP_CONTINUE = b"HTTP/1.1 100 Continue\r\n\r\n" + class Http: __slots__ = [ "_send", @@ -47,6 +48,7 @@ class Http: "response_func", "response_bytes_left", ] + def __init__(self, protocol): self._send = protocol.send self._receive_more = protocol.receive_more @@ -111,7 +113,7 @@ class Http: self.stage = Stage.REQUEST else: raise PayloadTooLarge("Payload Too Large") - # Parse header content + # Parse header content try: reqline, *raw_headers = buf[:pos].decode().split("\r\n") method, self.url, protocol = reqline.split(" ") @@ -193,9 +195,9 @@ class Http: data, size, end_stream = b"", 0, True headers.pop("content-length", None) headers.pop("transfer-encoding", None) - #raise ServerError( + # raise ServerError( # f"A {status} response may only have headers, no body." - #) + # ) elif self.head_only and "content-length" in headers: self.response_func = None elif end_stream: @@ -344,13 +346,15 @@ class Http: del buf[:size] self.request_bytes_left -= size self.protocol._total_request_size += size - if self.protocol._total_request_size > self.protocol.request_max_size: + if ( + self.protocol._total_request_size + > self.protocol.request_max_size + ): self.keep_alive = False raise PayloadTooLarge("Payload Too Large") return data return None - # Response methods def respond(self, response): diff --git a/sanic/request.py b/sanic/request.py index 7b1b4939..c77da108 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -17,8 +17,8 @@ from sanic.headers import ( parse_host, parse_xforwarded, ) -from sanic.response import HTTPResponse from sanic.log import error_logger, logger +from sanic.response import HTTPResponse try: @@ -132,12 +132,14 @@ class Request: Custom context is now stored in `request.custom_context.yourkey`""" setattr(self.ctx, key, value) - def respond(self, status=200, headers=None, content_type=DEFAULT_HTTP_CONTENT_TYPE): + def respond( + self, status=200, headers=None, content_type=DEFAULT_HTTP_CONTENT_TYPE + ): return self.stream.respond( - status if isinstance(status, HTTPResponse) else HTTPResponse( - status=status, - headers=headers, - content_type=content_type, + status + if isinstance(status, HTTPResponse) + else HTTPResponse( + status=status, headers=headers, content_type=content_type, ) ) diff --git a/sanic/response.py b/sanic/response.py index fc6c64b1..53797699 100644 --- a/sanic/response.py +++ b/sanic/response.py @@ -71,13 +71,12 @@ class StreamingHTTPResponse(BaseHTTPResponse): async def stream(self, request): self.send = request.respond( - self.status, - self.headers, - self.content_type, + self.status, self.headers, self.content_type, ).send await self.streaming_fn(self) await self.send(end_stream=True) + class HTTPResponse(BaseHTTPResponse): __slots__ = ("body", "status", "content_type", "headers", "_cookies") diff --git a/sanic/server.py b/sanic/server.py index 646e5c09..6f07231e 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -183,7 +183,14 @@ class HttpProtocol(asyncio.Protocol): ): self._http.exception = ServiceUnavailable("Response Timeout") else: - interval = min(self.keep_alive_timeout, self.request_timeout, self.response_timeout) / 2 + interval = ( + min( + self.keep_alive_timeout, + self.request_timeout, + self.response_timeout, + ) + / 2 + ) self.loop.call_later(max(0.1, interval), self.check_timeouts) return self._task.cancel() @@ -265,6 +272,7 @@ class HttpProtocol(asyncio.Protocol): except: logger.exception("protocol.data_received") + def trigger_events(events, loop): """Trigger event callbacks (functions or async) diff --git a/sanic/testing.py b/sanic/testing.py index cb110cd3..45c50d74 100644 --- a/sanic/testing.py +++ b/sanic/testing.py @@ -41,7 +41,9 @@ class SanicTestClient: url, verify=False, *args, **kwargs ) except httpx.exceptions.ConnectionClosed: - logger.error(f"{method.upper()} {url} broken HTTP, response is None!") + logger.error( + f"{method.upper()} {url} broken HTTP, response is None!" + ) return None except NameError: raise Exception(response.status_code)