Skip to content

Adding support for WITHSCORE in zrank #2698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4654,14 +4654,20 @@ 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``.
If `withscore` is True, the score of the element is also returned.

For more information see https://redis.io/commands/zrank
"""
return self.execute_command("ZRANK", name, value)
pieces = [name, value]
if withscore:
pieces.append("WITHSCORE")
return self.execute_command("ZRANK", *pieces)

def zrem(self, name: KeyT, *values: FieldT) -> ResponseT:
"""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_asyncio/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,13 @@ 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("b", {"a1": 1, "a2": 2, "a3": 3, "a4": 4, "a5": 5})
assert await r.zrank("b", "a1", True) == [0, 1.0]
assert await r.zrank("b", "a2", True) == [1, 2.0]
assert await r.zrank("b", "a6", 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
Expand Down
7 changes: 7 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2581,6 +2581,13 @@ 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_zrankwithscore(self, r):
r.zadd("b", {"a1": 1, "a2": 2, "a3": 3, "a4": 4, "a5": 5})
assert r.zrank("b", "a1", True) == [0, 1.0]
assert r.zrank("b", "a2", True) == [1, 2.0]
assert r.zrank("b", "a6", True) is None

def test_zrem(self, r):
r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})
assert r.zrem("a", "a2") == 1
Expand Down