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

Code before:

        This is useful for conditional operations like IFDEQ/IFDNE where you need to
        compute the digest client-side before sending a command.

        Warning:
        **Experimental** - This API may change or be removed without notice.

        Arguments:
          - value: Union[bytes, str] - the value to compute the digest of.

        Returns:
          - (str | bytes) the XXH3 digest of the value as a hex string (16 hex characters)

        For more information, see https://redis.io/commands/digest
        """
        if not HAS_XXHASH:
            raise NotImplementedError(
                "XXHASH library not installed. Install with: "
                "'pip install xxhash' or 'pip install redis[xxhash]' to use this feature."
            )

        local_digest = xxhash.xxh3_64(value).hexdigest()

        # To align with digest, we want to return bytes if decode_responses is False.
        # The following should work because Python's mixin approach.
        if not self.get_encoder().decode_responses:
            local_digest = local_digest.encode()

        return local_digest

    @experimental_method()
    def digest(self, name: KeyT) -> Union[str, bytes, None]:
        """
        Return the digest of the value stored at the specified key.

        Warning:
        **Experimental** since 7.1.
        This API may change or be removed without notice.
        The API may change based on feedback.

        Arguments:
          - name: KeyT - the key to get the digest of

        Returns:
          - None if the key does not exist
          - (bulk string) the XXH3 digest of the value as a hex string
        Raises:
          - ResponseError if key exists but is not a string


        Requires Redis 8.4 or greater.

Review comment (suggestion):
The method accepts Union[bytes, str] but there's no explicit handling of string encoding before passing to xxhash.xxh3_64(). When a string is passed, xxhash will use its default encoding (typically UTF-8), which should match Redis's behavior, but this assumption should be documented. Consider adding a note in the docstring about encoding expectations, or explicitly encode strings to bytes using the same encoding as the Redis connection to ensure hash consistency.

Code after:

        This is useful for conditional operations like IFDEQ/IFDNE where you need to
        compute the digest client-side before sending a command.

        Warning:
        **Experimental** - This API may change or be removed without notice.

        Arguments:
          - value: Union[bytes, str] - the value to compute the digest of.
            If a string is provided, it will be encoded using UTF-8 before hashing,
            which matches Redis's default encoding behavior.

        Returns:
          - (str | bytes) the XXH3 digest of the value as a hex string (16 hex characters).
            Returns bytes if decode_responses is False, otherwise returns str.

        For more information, see https://redis.io/commands/digest
        """
        if not HAS_XXHASH:
            raise NotImplementedError(
                "XXHASH support requires the optional 'xxhash' library. "
                "Install it with 'pip install xxhash' or use this package's extra with "
                "'pip install redis[xxhash]' to enable this feature."
            )

        local_digest = xxhash.xxh3_64(value).hexdigest()

        # To align with digest, we want to return bytes if decode_responses is False.
        # The following works because of Python's mixin-based client class hierarchy.
        if not self.get_encoder().decode_responses:
            local_digest = local_digest.encode()

        return local_digest

    @experimental_method()
    def digest(self, name: KeyT) -> Union[str, bytes, None]:
        """
        Return the digest of the value stored at the specified key.

        Warning:
        **Experimental** since 7.1.
        This API may change or be removed without notice.
        The API may change based on feedback.

        Arguments:
          - name: KeyT - the key to get the digest of

        Returns:
          - None if the key does not exist
          - (bulk string) the XXH3 digest of the value as a hex string
        Raises: