Repository navigation
fix(rtc): avoid mutable default and accept None in LocalParticipant.publish_data - #905
NabiBukhsh-AI wants to merge 2 commits into
Conversation
|
Hi @cloudwebrtc @lukasIO, Gentle check-in on this PR. It avoids mutable default arguments in All CI checks and Devin review are green. Ready for review whenever you have a moment! |
cloudwebrtc
left a comment
There was a problem hiding this comment.
Thanks! Dropping the mutable default and accepting None is a good change, and the new test looks sound. One thing to address before merging (inline): widening to Sequence[str] lets a bare string through the type checker.
Also a nit, outside the diff: send_text, stream_text, send_file and stream_bytes in the same class still take Optional[List[str]] for destination_identities, so after this PR a tuple type-checks for publish_data but not for the others. It would be nice to use the same annotation (and the same bare-str guard) across all of them so the public API stays consistent.
| *, | ||
| reliable: bool = True, | ||
| destination_identities: List[str] = [], | ||
| destination_identities: Optional[Sequence[str]] = None, |
There was a problem hiding this comment.
A plain str is itself a Sequence[str], so with this annotation
await lp.publish_data(b"hi", destination_identities="alice")now passes mypy/pyright, where List[str] used to flag it. At runtime extend() then iterates it character by character and the packet is addressed to ['a', 'l', 'i', 'c', 'e'], so it silently reaches no one. (The runtime behavior was the same before; what regresses is the type checker catching it.)
Could you guard against it, e.g.
if isinstance(destination_identities, str):
raise TypeError("destination_identities must be a sequence of identities, not a str")or keep the annotation as List[str]?
…List[str]] Widening the annotation to Sequence[str] let a bare str through the type checker, since a str is itself a Sequence[str]; at runtime it was then sent as one identity per character. Use Optional[List[str]] = None, the same annotation send_text, stream_text, send_file and stream_bytes use, so the type checker flags it again and the API stays consistent. Add a changeset for the release notes.
|
Thanks for the review, @cloudwebrtc, and good catch on the bare In 36aebe9 I've gone back to The test now covers |
Summary
LocalParticipant.publish_datadefined its destination parameter with a mutable default:This caused two issues:
ruff B006):destination_identities: List[str] = []is shared across calls.TypeErrorcrash onNone: Passingdestination_identities=None(common when forwarding an optional argument from caller code) causedreq.publish_data.destination_identities.extend(None)to raise:LocalParticipantthat takes destination identities (publish_dtmf,send_chat_message,stream_bytes,stream_text) defaults toNoneand conditionally extends the request withif destination_identities:.Changes
destination_identitiesdefault toNonewith typeOptional[Sequence[str]] = None, accepting tuples, lists, orNone.req.publish_data.destination_identitiesonly whendestination_identitiesis provided.Testing
Added unit tests in
tests/rtc/test_participant_publish_data.py:test_publish_data_parameter_default_is_none: verifiespublish_datasignature default isNone.test_publish_data_accepts_none_and_sequences: verifiespublish_datasucceeds when called with:destination_identities=None-> broadcastsdestination_identities=[]-> broadcasts["user1", "user2"])("user3", "user4"))uv run pytest tests/rtc/test_participant_publish_data.py -v -W error(2/2 passed)uv run ruff check livekit-rtc/livekit/rtc/participant.py tests/rtc/test_participant_publish_data.py(passed)uv run ruff format --check livekit-rtc/livekit/rtc/participant.py tests/rtc/test_participant_publish_data.py(passed)uv run mypy livekit-rtc/livekit/rtc/participant.py tests/rtc/test_participant_publish_data.py(0 issues)