Repository: redis/redis-py
File: redis/client.py (Python)

Code before:
            message = {
                "type": message_type,
                "pattern": None,
                "channel": None,
                "data": response[1],
            }
        else:
            message = {
                "type": message_type,
                "pattern": None,
                "channel": response[1],
                "data": response[2],
            }

        if message_type in ["message", "pmessage"]:
            self._event_dispatcher.dispatch(
                OnPubSubMessageEvent(
                    direction=PubSubDirection.RECEIVE,
                    channel=message["channel"],
                )
            )
        elif message_type == "smessage":
            self._event_dispatcher.dispatch(
                OnPubSubMessageEvent(
                    direction=PubSubDirection.RECEIVE,
                    channel=message["channel"],
                    sharded=True,
                )
            )

        # if this is an unsubscribe message, remove it from memory
        if message_type in self.UNSUBSCRIBE_MESSAGE_TYPES:
            if message_type == "punsubscribe":
                pattern = response[1]
                if pattern in self.pending_unsubscribe_patterns:
                    self.pending_unsubscribe_patterns.remove(pattern)
                    self.patterns.pop(pattern, None)
            elif message_type == "sunsubscribe":
                s_channel = response[1]
                if s_channel in self.pending_unsubscribe_shard_channels:
                    self.pending_unsubscribe_shard_channels.remove(s_channel)
                    self.shard_channels.pop(s_channel, None)
            else:
                channel = response[1]
                if channel in self.pending_unsubscribe_channels:
                    self.pending_unsubscribe_channels.remove(channel)
                    self.channels.pop(channel, None)
            if not self.channels and not self.patterns and not self.shard_channels:
                # There are no subscriptions anymore, set subscribed_event flag
                # to false
                self.subscribed_event.clear()

Review comment (suggestion):
The channel parameter may be bytes (from response[1] or response[2]) but the OnPubSubMessageEvent expects a str type. The channel should be converted to string using str_if_bytes() before passing it to the event, to ensure type consistency with the event's type annotation and to avoid potential issues in the metric recording layer.
```suggestion
            channel = str_if_bytes(message["channel"])
            self._event_dispatcher.dispatch(
                OnPubSubMessageEvent(
                    direction=PubSubDirection.RECEIVE,
                    channel=channel,
                )
            )
        elif message_type == "smessage":
            channel = str_if_bytes(message["channel"])
            self._event_dispatcher.dispatch(
                OnPubSubMessageEvent(
                    direction=PubSubDirection.RECEIVE,
                    channel=channel,
```

Code after:
            message = {
                "type": message_type,
                "pattern": None,
                "channel": None,
                "data": response[1],
            }
        else:
            message = {
                "type": message_type,
                "pattern": None,
                "channel": response[1],
                "data": response[2],
            }

        if message_type in ["message", "pmessage"]:
            channel = str_if_bytes(message["channel"])
            self._event_dispatcher.dispatch(
                OnPubSubMessageEvent(
                    direction=PubSubDirection.RECEIVE,
                    channel=channel,
                )
            )
        elif message_type == "smessage":
            channel = str_if_bytes(message["channel"])
            self._event_dispatcher.dispatch(
                OnPubSubMessageEvent(
                    direction=PubSubDirection.RECEIVE,
                    channel=channel,
                    sharded=True,
                )
            )

        # if this is an unsubscribe message, remove it from memory
        if message_type in self.UNSUBSCRIBE_MESSAGE_TYPES:
            if message_type == "punsubscribe":
                pattern = response[1]
                if pattern in self.pending_unsubscribe_patterns:
                    self.pending_unsubscribe_patterns.remove(pattern)
                    self.patterns.pop(pattern, None)
            elif message_type == "sunsubscribe":
                s_channel = response[1]
                if s_channel in self.pending_unsubscribe_shard_channels:
                    self.pending_unsubscribe_shard_channels.remove(s_channel)
                    self.shard_channels.pop(s_channel, None)
            else:
                channel = response[1]
                if channel in self.pending_unsubscribe_channels:
                    self.pending_unsubscribe_channels.remove(channel)
                    self.channels.pop(channel, None)
            if not self.channels and not self.patterns and not self.shard_channels:
                # There are no subscriptions anymore, set subscribed_event flag