Skip to content

Commit f8fa20a

Browse files
benmosherclaude
andcommitted
✅ Remove unreachable guard in get_sse_data_type; add subclass coverage test
Pydantic BaseModel subclasses always have __pydantic_generic_metadata__, so the `if not meta` early-return was dead code. Collapse it into a ternary and add a test for a plain (non-parameterized) ServerSentEvent subclass to reach the `not args` branch, bringing fastapi/sse.py to 100% coverage. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 141d3d2 commit f8fa20a

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

fastapi/sse.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,7 @@ def get_sse_data_type(annotation: Any) -> Any | None:
264264
if annotation is ServerSentEvent:
265265
return None
266266
meta = getattr(annotation, "__pydantic_generic_metadata__", None)
267-
if not meta:
268-
return None
269-
args = meta.get("args", ())
267+
args = meta.get("args", ()) if meta else ()
270268
if not args or isinstance(args[0], TypeVar):
271269
return None
272270
return args[0]

tests/test_sse.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,15 @@ def test_get_sse_data_type_non_sse():
340340
assert get_sse_data_type(None) is None
341341

342342

343+
def test_get_sse_data_type_subclass_no_type_param():
344+
"""get_sse_data_type returns None for a plain ServerSentEvent subclass."""
345+
346+
class MyEvent(ServerSentEvent):
347+
pass
348+
349+
assert get_sse_data_type(MyEvent) is None
350+
351+
343352
def test_generic_sse_construction_validates_data():
344353
"""ServerSentEvent[Item] requires data to be an Item."""
345354
item = Item(name="Foo", description=None)

0 commit comments

Comments
 (0)