Repository: redis/redis-py
File: redis/commands/core.py (Python)

Code before:
        return response

    def spublish(self, shard_channel: ChannelT, message: EncodableT) -> ResponseT:
        """
        Posts a message to the given shard channel.
        Returns the number of clients that received the message

        For more information, see https://redis.io/commands/spublish
        """
        response = self.execute_command("SPUBLISH", shard_channel, message)
        self._event_dispatcher.dispatch(
            OnPubSubMessageEvent(
                direction=PubSubDirection.PUBLISH,
                channel=shard_channel,
                sharded=True,
            )
        )
        return response

    def pubsub_channels(self, pattern: PatternT = "*", **kwargs) -> ResponseT:
        """
        Return a list of channels that have at least one subscriber

        For more information, see https://redis.io/commands/pubsub-channels
        """
        return self.execute_command("PUBSUB CHANNELS", pattern, **kwargs)

    def pubsub_shardchannels(self, pattern: PatternT = "*", **kwargs) -> ResponseT:
        """
        Return a list of shard_channels that have at least one subscriber

        For more information, see https://redis.io/commands/pubsub-shardchannels
        """
        return self.execute_command("PUBSUB SHARDCHANNELS", pattern, **kwargs)

    def pubsub_numpat(self, **kwargs) -> ResponseT:
        """
        Returns the number of subscriptions to patterns

        For more information, see https://redis.io/commands/pubsub-numpat
        """
        return self.execute_command("PUBSUB NUMPAT", **kwargs)

    def pubsub_numsub(self, *args: ChannelT, **kwargs) -> ResponseT:
        """
        Return a list of (channel, number of subscribers) tuples
        for each channel given in ``*args``

        For more information, see https://redis.io/commands/pubsub-numsub
        """
        return self.execute_command("PUBSUB NUMSUB", *args, **kwargs)

Review comment (suggestion):
The shard_channel parameter has type ChannelT which is Union[bytes, str, memoryview], but OnPubSubMessageEvent expects a str type for the channel field. The channel should be converted to string using str_if_bytes() before passing it to the event, to ensure type consistency and avoid potential issues in the metric recording layer.

Code after:
        """
        response = self.execute_command("PUBLISH", channel, message, **kwargs)
        self._event_dispatcher.dispatch(
            OnPubSubMessageEvent(
                direction=PubSubDirection.PUBLISH,
                channel=str_if_bytes(channel),
            )
        )
        return response

    def spublish(self, shard_channel: ChannelT, message: EncodableT) -> ResponseT:
        """
        Posts a message to the given shard channel.
        Returns the number of clients that received the message

        For more information, see https://redis.io/commands/spublish
        """
        response = self.execute_command("SPUBLISH", shard_channel, message)
        self._event_dispatcher.dispatch(
            OnPubSubMessageEvent(
                direction=PubSubDirection.PUBLISH,
                channel=str_if_bytes(shard_channel),
                sharded=True,
            )
        )
        return response

    def pubsub_channels(self, pattern: PatternT = "*", **kwargs) -> ResponseT:
        """
        Return a list of channels that have at least one subscriber

        For more information, see https://redis.io/commands/pubsub-channels
        """
        return self.execute_command("PUBSUB CHANNELS", pattern, **kwargs)

    def pubsub_shardchannels(self, pattern: PatternT = "*", **kwargs) -> ResponseT:
        """
        Return a list of shard_channels that have at least one subscriber

        For more information, see https://redis.io/commands/pubsub-shardchannels
        """
        return self.execute_command("PUBSUB SHARDCHANNELS", pattern, **kwargs)

    def pubsub_numpat(self, **kwargs) -> ResponseT:
        """
        Returns the number of subscriptions to patterns

        For more information, see https://redis.io/commands/pubsub-numpat
        """
        return self.execute_command("PUBSUB NUMPAT", **kwargs)