Skip to content

Commit

Permalink
PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-congo committed May 13, 2024
1 parent e14573e commit ce71d3c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
14 changes: 10 additions & 4 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,9 @@ async def zdiff(self, keys: List[str]) -> List[str]:
empty list.
Examples:
>>> await client.zadd("sorted_set1", {"element1":1.0, "element2": 2.0, "element3": 3.0})
>>> await client.zadd("sorted_set2", {"element2": 2.0})
>>> await client.zadd("sorted_set3", {"element3": 3.0})
>>> await client.zdiff("sorted_set1", "sorted_set2", "sorted_set3")
["element1"] # Indicates that "element1" is in "sorted_set1" but not "sorted_set2" or "sorted_set3".
"""
Expand All @@ -2738,9 +2741,9 @@ async def zdiff(self, keys: List[str]) -> List[str]:
await self._execute_command(RequestType.ZDiff, [str(len(keys))] + keys),
)

async def zdiff_withscores(self, keys: List[str]) -> Dict[str, float]:
async def zdiff_withscores(self, keys: List[str]) -> Mapping[str, float]:
"""
Returns the difference between the first sorted set and all the successive sorted sets.
Returns the difference between the first sorted set and all the successive sorted sets, with the associated scores.
When in Cluster mode, all keys must map to the same hash slot.
See https://valkey.io/commands/zdiff for more details.
Expand All @@ -2749,17 +2752,20 @@ async def zdiff_withscores(self, keys: List[str]) -> Dict[str, float]:
keys (List[str]): The keys of the sorted sets.
Returns:
Dict[str, float]: A dictionary of elements and their scores representing the difference between the sorted
Mapping[str, float]: A dictionary of elements and their scores representing the difference between the sorted
sets.
If the first `key` does not exist, it is treated as an empty sorted set, and the command returns an
empty list.
Examples:
>>> await client.zadd("sorted_set1", {"element1":1.0, "element2": 2.0, "element3": 3.0})
>>> await client.zadd("sorted_set2", {"element2": 2.0})
>>> await client.zadd("sorted_set3", {"element3": 3.0})
>>> await client.zdiff_withscores("sorted_set1", "sorted_set2", "sorted_set3")
{"element1": 1.0} # Indicates that "element1" is in "sorted_set1" but not "sorted_set2" or "sorted_set3".
"""
return cast(
Dict[str, float],
Mapping[str, float],
await self._execute_command(
RequestType.ZDiff, [str(len(keys))] + keys + ["WITHSCORES"]
),
Expand Down
4 changes: 2 additions & 2 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1964,15 +1964,15 @@ def zdiff(self: TTransaction, keys: List[str]) -> TTransaction:

def zdiff_withscores(self: TTransaction, keys: List[str]) -> TTransaction:
"""
Returns the difference between the first sorted set and all the successive sorted sets.
Returns the difference between the first sorted set and all the successive sorted sets, with the associated scores.
See https://valkey.io/commands/zdiff for more details.
Args:
keys (List[str]): The keys of the sorted sets.
Command response:
Dict[str, float]: A dictionary of elements and their scores representing the difference between the sorted sets.
Mapping[str, float]: A dictionary of elements and their scores representing the difference between the sorted sets.
If the first `key` does not exist, it is treated as an empty sorted set, and the command returns an
empty list.
"""
Expand Down
23 changes: 20 additions & 3 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2353,12 +2353,29 @@ async def test_zdiff(self, redis_client: TRedisClient):
assert await redis_client.zdiff([key1, key3]) == []
assert await redis_client.zdiff([non_existing_key, key3]) == []

assert await redis_client.zdiff_withscores([key1, key2]) == {
zdiff_map = await redis_client.zdiff_withscores([key1, key2])
expected_map = {
"one": 1.0,
"three": 3.0,
}
assert await redis_client.zdiff_withscores([key1, key3]) == {}
assert await redis_client.zdiff_withscores([non_existing_key, key3]) == {}
assert compare_maps(zdiff_map, expected_map) is True
assert (
compare_maps(await redis_client.zdiff_withscores([key1, key3]), {}) is True
)
assert (
compare_maps(
await redis_client.zdiff_withscores([non_existing_key, key3]), {}
)
is True
)

# invalid argument - key list must not be empty
with pytest.raises(RequestError):
await redis_client.zdiff([])

# invalid argument - key list must not be empty
with pytest.raises(RequestError):
await redis_client.zdiff_withscores([])

# key exists, but it is not a sorted set
assert await redis_client.set(string_key, "foo") == OK
Expand Down

0 comments on commit ce71d3c

Please sign in to comment.