Skip to content

Commit

Permalink
Python: move the commands return value to bytes (valkey-io#1617)
Browse files Browse the repository at this point in the history
* In the case of Simple String, Bulk String, or Verbatim String commands, Bytes will be returned instead of strings.

---------

Co-authored-by: GilboaAWS <[email protected]>
Co-authored-by: Ubuntu <[email protected]>
Co-authored-by: Adar Ovadia <[email protected]>
Co-authored-by: Shoham Elias <[email protected]>
Co-authored-by: Shoham Elias <[email protected]>
  • Loading branch information
6 people authored Jun 30, 2024
1 parent fb9fe86 commit 398065b
Show file tree
Hide file tree
Showing 10 changed files with 1,058 additions and 706 deletions.
8 changes: 5 additions & 3 deletions examples/python/client_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ async def test_standalone_client(host: str = "localhost", port: int = 6379):
await send_set_and_get(client)
# Send PING to the primary node
pong = await client.custom_command(["PING"])
print(f"PONG response is = {pong}")
assert isinstance(pong, bytes)
print(f"PONG response is = {pong.decode()}")


async def test_cluster_client(host: str = "localhost", port: int = 6379):
Expand All @@ -71,10 +72,11 @@ async def test_cluster_client(host: str = "localhost", port: int = 6379):
await send_set_and_get(client)
# Send PING to all primaries (according to Redis's PING request_policy)
pong = await client.custom_command(["PING"])
print(f"PONG response is = {pong}")
assert isinstance(pong, bytes)
print(f"PONG response is = {pong.decode()}")
# Send INFO REPLICATION with routing option to all nodes
info_repl_resps = await client.custom_command(["INFO", "REPLICATION"], AllNodes())
print(f"INFO REPLICATION responses to all nodes are = {info_repl_resps}")
print(f"INFO REPLICATION responses from all nodes are = {info_repl_resps!r}")


async def main():
Expand Down
6 changes: 3 additions & 3 deletions python/python/glide/async_commands/cluster_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import Dict, List, Mapping, Optional, cast
from typing import Dict, List, Mapping, Optional, Union, cast

from glide.async_commands.command_args import Limit, OrderBy
from glide.async_commands.core import (
Expand Down Expand Up @@ -46,7 +46,7 @@ async def info(
self,
sections: Optional[List[InfoSection]] = None,
route: Optional[Route] = None,
) -> TClusterResponse[str]:
) -> TClusterResponse[bytes]:
"""
Get information and statistics about the Redis server.
See https://redis.io/commands/info/ for details.
Expand All @@ -65,7 +65,7 @@ async def info(
args = [section.value for section in sections] if sections else []

return cast(
TClusterResponse[str],
TClusterResponse[bytes],
await self._execute_command(RequestType.Info, args, route),
)

Expand Down
6 changes: 3 additions & 3 deletions python/python/glide/async_commands/standalone_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def custom_command(self, command_args: List[str]) -> TResult:
async def info(
self,
sections: Optional[List[InfoSection]] = None,
) -> str:
) -> bytes:
"""
Get information and statistics about the Redis server.
See https://redis.io/commands/info/ for details.
Expand All @@ -49,10 +49,10 @@ async def info(
Returns:
str: Returns a string containing the information for the sections requested.
bytes: Returns bytes containing the information for the sections requested.
"""
args = [section.value for section in sections] if sections else []
return cast(str, await self._execute_command(RequestType.Info, args))
return cast(bytes, await self._execute_command(RequestType.Info, args))

async def exec(
self,
Expand Down
5 changes: 4 additions & 1 deletion python/python/glide/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
float,
Set[T],
List[T],
bytes,
Dict[bytes, "TResult"],
Mapping[bytes, "TResult"],
]
TRequest = Union[RedisRequest, ConnectionRequest]
# When routing to a single node, response will be T
# Otherwise, response will be : {Address : response , ... } with type of Dict[str, T].
TClusterResponse = Union[T, Dict[str, T]]
TClusterResponse = Union[T, Dict[bytes, T]]
TSingleNodeRoute = Union[RandomNode, SlotKeyRoute, SlotIdRoute, ByAddressRoute]
# When specifying legacy path (path doesn't start with `$`), response will be T
# Otherwise, (when specifying JSONPath), response will be List[Optional[T]].
Expand Down
4 changes: 4 additions & 0 deletions python/python/glide/glide_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ async def _execute_command(
request = RedisRequest()
request.callback_idx = self._get_callback_index()
request.single_command.request_type = request_type
request.single_command.args_array.args[:] = [
bytes(elem, encoding="utf8") if isinstance(elem, str) else elem
for elem in args
]
(encoded_args, args_size) = self._encode_and_sum_size(args)
if args_size < MAX_REQUEST_ARGS_LEN:
request.single_command.args_array.args[:] = encoded_args
Expand Down
Loading

0 comments on commit 398065b

Please sign in to comment.