Browse Source

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 <[email protected]>
pull/15191/head
Ben Mosher 3 months ago
parent
commit
f8fa20a218
  1. 4
      fastapi/sse.py
  2. 9
      tests/test_sse.py

4
fastapi/sse.py

@ -264,9 +264,7 @@ def get_sse_data_type(annotation: Any) -> Any | None:
if annotation is ServerSentEvent:
return None
meta = getattr(annotation, "__pydantic_generic_metadata__", None)
if not meta:
return None
args = meta.get("args", ())
args = meta.get("args", ()) if meta else ()
if not args or isinstance(args[0], TypeVar):
return None
return args[0]

9
tests/test_sse.py

@ -340,6 +340,15 @@ def test_get_sse_data_type_non_sse():
assert get_sse_data_type(None) is None
def test_get_sse_data_type_subclass_no_type_param():
"""get_sse_data_type returns None for a plain ServerSentEvent subclass."""
class MyEvent(ServerSentEvent):
pass
assert get_sse_data_type(MyEvent) is None
def test_generic_sse_construction_validates_data():
"""ServerSentEvent[Item] requires data to be an Item."""
item = Item(name="Foo", description=None)

Loading…
Cancel
Save