diff --git a/redis/commands/core.py b/redis/commands/core.py index f2d7bf2eb4..af5384e9ef 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -4654,13 +4654,22 @@ def zrevrangebyscore( options = {"withscores": withscores, "score_cast_func": score_cast_func} return self.execute_command(*pieces, **options) - def zrank(self, name: KeyT, value: EncodableT) -> ResponseT: + def zrank( + self, + name: KeyT, + value: EncodableT, + withscore: bool = False, + ) -> ResponseT: """ Returns a 0-based value indicating the rank of ``value`` in sorted set - ``name`` + ``name``. + The optional WITHSCORE argument supplements the command's + reply with the score of the element returned. For more information see https://redis.io/commands/zrank """ + if withscore: + return self.execute_command("ZRANK", name, value, "WITHSCORE") return self.execute_command("ZRANK", name, value) def zrem(self, name: KeyT, *values: FieldT) -> ResponseT: diff --git a/tests/test_asyncio/test_commands.py b/tests/test_asyncio/test_commands.py index 955b9d42bc..5219291942 100644 --- a/tests/test_asyncio/test_commands.py +++ b/tests/test_asyncio/test_commands.py @@ -1645,6 +1645,15 @@ async def test_zrank(self, r: redis.Redis): assert await r.zrank("a", "a2") == 1 assert await r.zrank("a", "a6") is None + @skip_if_server_version_lt("7.2.0") + async def test_zrank_withscore(self, r: redis.Redis): + await r.zadd("a", {"a1": 1, "a2": 2, "a3": 3, "a4": 4, "a5": 5}) + assert await r.zrank("a", "a1") == 0 + assert await r.rank("a", "a2") == 1 + assert await r.zrank("a", "a6") is None + assert await r.zrank("a", "a3", withscore=True) == [2, "3"] + assert await r.zrank("a", "a6", withscore=True) is None + async def test_zrem(self, r: redis.Redis): await r.zadd("a", {"a1": 1, "a2": 2, "a3": 3}) assert await r.zrem("a", "a2") == 1 diff --git a/tests/test_commands.py b/tests/test_commands.py index c71e347fab..4e2ac5b041 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -2581,6 +2581,15 @@ def test_zrank(self, r): assert r.zrank("a", "a2") == 1 assert r.zrank("a", "a6") is None + @skip_if_server_version_lt("7.2.0") + def test_zrank_withscore(self, r: redis.Redis): + r.zadd("a", {"a1": 1, "a2": 2, "a3": 3, "a4": 4, "a5": 5}) + assert r.zrank("a", "a1") == 0 + assert r.rank("a", "a2") == 1 + assert r.zrank("a", "a6") is None + assert r.zrank("a", "a3", withscore=True) == [2, "3"] + assert r.zrank("a", "a6", withscore=True) is None + def test_zrem(self, r): r.zadd("a", {"a1": 1, "a2": 2, "a3": 3}) assert r.zrem("a", "a2") == 1