Browse Source

🎨 Auto format

pull/14232/head
pre-commit-ci-lite[bot] 4 months ago
committed by GitHub
parent
commit
18440f4b5a
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 24
      fastapi/routing.py

24
fastapi/routing.py

@ -463,9 +463,7 @@ def get_request_handler(
errors = solved_result.errors errors = solved_result.errors
assert dependant.call # For types assert dependant.call # For types
if errors: if errors:
raise RequestValidationError( raise RequestValidationError(errors, body=body, endpoint_ctx=endpoint_ctx)
errors, body=body, endpoint_ctx=endpoint_ctx
)
# Shared serializer for stream items (JSONL and SSE). # Shared serializer for stream items (JSONL and SSE).
# Validates against stream_item_field when set, then # Validates against stream_item_field when set, then
@ -520,9 +518,7 @@ def get_request_handler(
comment=item.comment, comment=item.comment,
) )
return format_sse_event( return format_sse_event(data_str=_serialize_data(item).decode("utf-8"))
data_str=_serialize_data(item).decode("utf-8")
)
if dependant.is_async_gen_callable: if dependant.is_async_gen_callable:
sse_aiter: AsyncIterator[Any] = gen.__aiter__() sse_aiter: AsyncIterator[Any] = gen.__aiter__()
@ -530,9 +526,7 @@ def get_request_handler(
sse_aiter = iterate_in_threadpool(gen) sse_aiter = iterate_in_threadpool(gen)
@asynccontextmanager @asynccontextmanager
async def _sse_producer_cm() -> AsyncIterator[ async def _sse_producer_cm() -> AsyncIterator[ObjectReceiveStream[bytes]]:
ObjectReceiveStream[bytes]
]:
# Use a memory stream to decouple generator iteration # Use a memory stream to decouple generator iteration
# from the keepalive timer. A producer task pulls items # from the keepalive timer. A producer task pulls items
# from the generator independently, so # from the generator independently, so
@ -547,18 +541,18 @@ def get_request_handler(
# streaming response completes — not by async generator # streaming response completes — not by async generator
# finalization via GeneratorExit. # finalization via GeneratorExit.
# Ref: https://peps.python.org/pep-0789/ # Ref: https://peps.python.org/pep-0789/
send_stream, receive_stream = anyio.create_memory_object_stream[ send_stream, receive_stream = anyio.create_memory_object_stream[bytes](
bytes max_buffer_size=1
](max_buffer_size=1) )
async def _producer() -> None: async def _producer() -> None:
async with send_stream: async with send_stream:
async for raw_item in sse_aiter: async for raw_item in sse_aiter:
await send_stream.send(_serialize_sse_item(raw_item)) await send_stream.send(_serialize_sse_item(raw_item))
send_keepalive, receive_keepalive = ( send_keepalive, receive_keepalive = anyio.create_memory_object_stream[
anyio.create_memory_object_stream[bytes](max_buffer_size=1) bytes
) ](max_buffer_size=1)
async def _keepalive_inserter() -> None: async def _keepalive_inserter() -> None:
"""Read from the producer and forward to the output, """Read from the producer and forward to the output,

Loading…
Cancel
Save