sysid / sse-starlette

BSD 3-Clause "New" or "Revised" License
544 stars 37 forks source link

Should the content parameter on EventSourceResponse take a stream of Any? #101

Closed datalogics-kam closed 3 months ago

datalogics-kam commented 3 months ago

I'm making my streams strongly typed in my program, for instance:

    send_chan, recv_chan = anyio.create_memory_object_stream[StreamingEvent](10)

(StreamingEvent is a union of Pydantic base models.)

But mypy complains about the content argument to EventSourceResponse.

The code still works, because ensure_bytes at

https://github.com/sysid/sse-starlette/blob/4b1ec63da5a7da0a0243b699f4d83f04f255a41c/sse_starlette/sse.py#L143

...will convert anything to a str, and I simply arranged for my event objects to return JSON serialization for str.

Furthermore, the data property of ServerSentEvent can be Optional[Any].

It seems to me that since any object will be turned into a string, having Content also include Any would be convenient. Then you could send a stream of any object that's got a good string conversion, and it would work.

https://github.com/sysid/sse-starlette/blob/4b1ec63da5a7da0a0243b699f4d83f04f255a41c/sse_starlette/sse.py#L146

sysid commented 3 months ago

@datalogics-kam thanks for raising this issue.

I can generally follow your reasoning but need some time to think more about it. Just a bit busy right now, so I need to ask you for some patience.

sysid commented 3 months ago

fixed with 4844ce8