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]
name = getmodulename(frame_records[1])
# Check for unsupported function on custom request objects
if request_class and any(hasattr(request_class, m) for m in (
"body_init", "body_push", "body_finish"
)) and request_class.receive_body is Request.receive_body:
if (
request_class
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(
"Request methods body_init, body_push and body_finish "
f"are no longer supported. {request_class!r} should "

View File

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

View File

@ -179,7 +179,7 @@ class Http:
self.stage = Stage.HANDLER
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
# Compatibility with simple response body
if not data and getattr(res, "body", None):
@ -244,22 +244,22 @@ class Http:
self.response_func = None
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."""
# Chunked encoding
size = len(data)
if end_stream:
await self._send(
b"%x\r\n%b\r\n0\r\n\r\n" % (size, data)
if size else
b"0\r\n\r\n"
if size
else b"0\r\n\r\n"
)
self.response_func = None
self.stage = Stage.IDLE
elif size:
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."""
bytes_left = self.response_bytes_left - len(data)
if bytes_left <= 0:

View File

@ -132,24 +132,17 @@ class Request:
)
return response
async def receive_body(self):
self.body = b"".join([data async for data in self.stream])
async def receive_body(self):
"""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
versions of Sanic will make all requests streaming and will use this
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.
Custom request classes can override this for custom handling of both
streaming and non-streaming routes.
"""
if not self.stream:
return
self.body = b"".join([data async for data in self.stream])
if not self.body:
self.body = b"".join([data async for data in self.stream])
@property
def json(self):