Browse Source

🎨 Auto format

pull/15428/head
pre-commit-ci-lite[bot] 3 months ago
committed by GitHub
parent
commit
aeabe6c030
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      fastapi/sse.py
  2. 8
      tests/test_sse.py

9
fastapi/sse.py

@ -11,7 +11,7 @@ from pydantic import AfterValidator, BaseModel, Field, model_validator
from starlette.concurrency import iterate_in_threadpool
from starlette.requests import ClientDisconnect
from starlette.responses import StreamingResponse
from starlette.types import Message, Receive, Scope, Send
from starlette.types import Receive, Scope, Send
try:
from starlette._utils import collapse_excgroups
@ -23,6 +23,7 @@ except ImportError:
def collapse_excgroups():
yield
# Canonical SSE event schema matching the OpenAPI 3.2 spec
# (Section 4.14.4 "Special Considerations for Server-Sent Events")
_SSE_EVENT_SCHEMA: dict[str, Any] = {
@ -91,9 +92,7 @@ class EventSourceResponse(StreamingResponse):
# ServerSentEvent objects, emit an initial retry field, and add
# keepalive pings. Also set SSE-appropriate headers.
if content is not None:
content = _wrap_content(
content, retry=retry, ping_interval=ping_interval
)
content = _wrap_content(content, retry=retry, ping_interval=ping_interval)
if headers is None:
headers = {}
headers.setdefault("Cache-Control", "no-cache")
@ -392,7 +391,7 @@ class ServerSentEvent(BaseModel):
] = None
@model_validator(mode="after")
def _check_data_exclusive(self) -> "ServerSentEvent":
def _check_data_exclusive(self) -> ServerSentEvent:
if self.data is not None and self.raw_data is not None:
raise ValueError(
"Cannot set both 'data' and 'raw_data' on the same "

8
tests/test_sse.py

@ -463,8 +463,8 @@ def test_direct_use_keepalive_ping():
assert response.status_code == 200
text = response.text
assert ": ping\n" in text
assert "data: \"before-ping\"\n" in text
assert "data: \"after-ping\"\n" in text
assert 'data: "before-ping"\n' in text
assert 'data: "after-ping"\n' in text
def test_direct_use_no_ping_when_disabled():
@ -472,8 +472,8 @@ def test_direct_use_no_ping_when_disabled():
response = direct_client.get("/direct-no-ping")
assert response.status_code == 200
assert ": ping\n" not in response.text
assert "data: \"msg1\"\n" in response.text
assert "data: \"msg2\"\n" in response.text
assert 'data: "msg1"\n' in response.text
assert 'data: "msg2"\n' in response.text
def test_direct_use_sync_iterable():

Loading…
Cancel
Save