Skip to content

fix(rtc): avoid mutable default and accept None in LocalParticipant.publish_data - #905

Open
NabiBukhsh-AI wants to merge 1 commit into
livekit:mainfrom
NabiBukhsh-AI:fix/rtc-participant-publish-data-destinations
Open

NabiBukhsh-AI wants to merge 1 commit into
livekit:mainfrom
NabiBukhsh-AI:fix/rtc-participant-publish-data-destinations

Conversation

@NabiBukhsh-AI

Copy link
Copy Markdown
Contributor

Summary

LocalParticipant.publish_data defined its destination parameter with a mutable default:

async def publish_data(
    self,
    payload: Union[bytes, str],
    *,
    reliable: bool = True,
    destination_identities: List[str] = [],
    topic: str = "",
) -> None:
    ...
    req.publish_data.destination_identities.extend(destination_identities)

This caused two issues:

  1. Mutable default argument (ruff B006): destination_identities: List[str] = [] is shared across calls.
  2. TypeError crash on None: Passing destination_identities=None (common when forwarding an optional argument from caller code) caused req.publish_data.destination_identities.extend(None) to raise:
    TypeError: Value must be iterable
    
  3. API Inconsistency: Every other method on LocalParticipant that takes destination identities (publish_dtmf, send_chat_message, stream_bytes, stream_text) defaults to None and conditionally extends the request with if destination_identities:.

Changes

  • Changed destination_identities default to None with type Optional[Sequence[str]] = None, accepting tuples, lists, or None.
  • Conditionally extends req.publish_data.destination_identities only when destination_identities is provided.
  • Updated docstring documentation.

Testing

Added unit tests in tests/rtc/test_participant_publish_data.py:

  • test_publish_data_parameter_default_is_none: verifies publish_data signature default is None.

  • test_publish_data_accepts_none_and_sequences: verifies publish_data succeeds when called with:

    • Default (omitted) -> broadcasts (empty list in request)
    • Explicit destination_identities=None -> broadcasts
    • Explicit destination_identities=[] -> broadcasts
    • List of identities (["user1", "user2"])
    • Tuple of identities (("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)

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant