Linter and typing

This commit is contained in:
L. Kärkkäinen 2020-03-25 15:05:51 +02:00
parent 5832764aab
commit 6bedec97ae
4 changed files with 22 additions and 24 deletions

View File

@ -63,9 +63,14 @@ class Sanic:
frame_records = stack()[1] frame_records = stack()[1]
name = getmodulename(frame_records[1]) name = getmodulename(frame_records[1])
# Check for unsupported function on custom request objects # Check for unsupported function on custom request objects
if request_class and any(hasattr(request_class, m) for m in ( if (
"body_init", "body_push", "body_finish" request_class
)) and request_class.receive_body is Request.receive_body: and any(
hasattr(request_class, m)
for m in ("body_init", "body_push", "body_finish")
)
and request_class.receive_body is Request.receive_body
):
raise NotImplementedError( raise NotImplementedError(
"Request methods body_init, body_push and body_finish " "Request methods body_init, body_push and body_finish "
f"are no longer supported. {request_class!r} should " f"are no longer supported. {request_class!r} should "

View File

@ -5,11 +5,11 @@ from multidict import CIMultiDict # type: ignore
try: try:
from trio import Cancelled from trio import Cancelled # type: ignore
CancelledErrors = CancelledError, Cancelled CancelledErrors = [CancelledError, Cancelled]
except ImportError: except ImportError:
CancelledErrors = (CancelledError,) CancelledErrors = [CancelledError]
class Header(CIMultiDict): class Header(CIMultiDict):

View File

@ -179,7 +179,7 @@ class Http:
self.stage = Stage.HANDLER self.stage = Stage.HANDLER
self.request = request self.request = request
async def http1_response_header(self, data, end_stream) -> bytes: async def http1_response_header(self, data, end_stream):
res = self.response res = self.response
# Compatibility with simple response body # Compatibility with simple response body
if not data and getattr(res, "body", None): if not data and getattr(res, "body", None):
@ -244,22 +244,22 @@ class Http:
self.response_func = None self.response_func = None
self.stage = Stage.IDLE self.stage = Stage.IDLE
async def http1_response_chunked(self, data, end_stream) -> bytes: async def http1_response_chunked(self, data, end_stream):
"""Format a part of response body in chunked encoding.""" """Format a part of response body in chunked encoding."""
# Chunked encoding # Chunked encoding
size = len(data) size = len(data)
if end_stream: if end_stream:
await self._send( await self._send(
b"%x\r\n%b\r\n0\r\n\r\n" % (size, data) b"%x\r\n%b\r\n0\r\n\r\n" % (size, data)
if size else if size
b"0\r\n\r\n" else b"0\r\n\r\n"
) )
self.response_func = None self.response_func = None
self.stage = Stage.IDLE self.stage = Stage.IDLE
elif size: elif size:
await self._send(b"%x\r\n%b\r\n" % (size, data)) await self._send(b"%x\r\n%b\r\n" % (size, data))
async def http1_response_normal(self, data: bytes, end_stream: bool) -> bytes: async def http1_response_normal(self, data: bytes, end_stream: bool):
"""Format / keep track of non-chunked response.""" """Format / keep track of non-chunked response."""
bytes_left = self.response_bytes_left - len(data) bytes_left = self.response_bytes_left - len(data)
if bytes_left <= 0: if bytes_left <= 0:

View File

@ -132,23 +132,16 @@ class Request:
) )
return response return response
async def receive_body(self):
self.body = b"".join([data async for data in self.stream])
async def receive_body(self): async def receive_body(self):
"""Receive request.body, if not already received. """Receive request.body, if not already received.
Streaming handlers may call this to receive the full body. Streaming handlers may call this to receive the full body. Sanic calls
this function before running any handlers of non-streaming routes.
This is added as a compatibility shim in Sanic 20.3 because future Custom request classes can override this for custom handling of both
versions of Sanic will make all requests streaming and will use this streaming and non-streaming routes.
function instead of the non-async body_init/push/finish functions.
Please make an issue if your code depends on the old functionality and
cannot be upgraded to the new API.
""" """
if not self.stream: if not self.body:
return
self.body = b"".join([data async for data in self.stream]) self.body = b"".join([data async for data in self.stream])
@property @property