Skip to content

Commit

Permalink
Python - change type() to isinstance()
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon authored Oct 15, 2023
1 parent 94a246b commit 0ee6f21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions python/python/pybushka/async_commands/cluster_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def is_single_response(response: T, single_res: T) -> bool:
>>> is_single_response([["value"]], LIST_STR)
False
"""
if type(single_res) == type(response) == list:
if isinstance(single_res, list) and isinstance(response, list):
return is_single_response(response[0], single_res[0])
elif type(response) == type(single_res):
elif isinstance(response, type(single_res)):
return True
return False

Expand All @@ -59,7 +59,7 @@ def convert_multi_node_res_to_dict(
dict_res: Dict[str, T] = {}
while len(response) > 0:
cur_res = response.pop()
if cur_res is not None and type(cur_res[0]) == str:
if cur_res is not None and isinstance(cur_res[0], str):
dict_res[cur_res[0]] = cast(T, cur_res[1])

return dict_res
Expand Down
28 changes: 14 additions & 14 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def exec_command(i):
async def test_can_connect_with_auth_requirepass(
self, redis_client: TRedisClient, request
):
is_cluster = type(redis_client) == RedisClusterClient
is_cluster = isinstance(redis_client, RedisClusterClient)
password = "TEST_AUTH"
credentials = AuthenticationOptions(password)
try:
Expand All @@ -149,7 +149,7 @@ async def test_can_connect_with_auth_requirepass(
async def test_can_connect_with_auth_acl(
self, redis_client: Union[RedisClient, RedisClusterClient], request
):
is_cluster = type(redis_client) == RedisClusterClient
is_cluster = isinstance(redis_client, RedisClusterClient)
username = "testuser"
password = "TEST_AUTH"
try:
Expand Down Expand Up @@ -245,7 +245,7 @@ async def test_custom_command_multi_arg(self, redis_client: TRedisClient):
client_list = await redis_client.custom_command(
["CLIENT", "LIST", "TYPE", "NORMAL"]
)
assert type(client_list) == str or type(client_list) == list
assert isinstance(client_list, (str, list))
res: str = get_first_result(client_list)
assert res is not None
assert "id" in res
Expand All @@ -259,7 +259,7 @@ async def test_custom_command_lower_and_upper_case(
client_list = await redis_client.custom_command(
["CLIENT", "LIST", "TYPE", "NORMAL"]
)
assert type(client_list) == str or type(client_list) == list
assert isinstance(client_list, (str, list))
res: str = get_first_result(client_list)
assert res is not None
assert "id" in res
Expand All @@ -282,16 +282,16 @@ async def test_info_server_replication(self, redis_client: TRedisClient):
assert "# Replication" in info
assert "# Errorstats" not in info
cluster_mode = parse_info_response(info)["redis_mode"]
expected_cluster_mode = type(redis_client) == RedisClusterClient
expected_cluster_mode = isinstance(redis_client, RedisClusterClient)
assert cluster_mode == "cluster" if expected_cluster_mode else "standalone"

@pytest.mark.parametrize("cluster_mode", [True, False])
async def test_info_default(self, redis_client: TRedisClient):
cluster_mode = type(redis_client) == RedisClusterClient
cluster_mode = isinstance(redis_client, RedisClusterClient)
info_result = await redis_client.info()
if cluster_mode:
cluster_nodes = await redis_client.custom_command(["CLUSTER", "NODES"])
assert type(cluster_nodes) == str or type(cluster_nodes) == list
assert isinstance(cluster_nodes, (str, list))
cluster_nodes = get_first_result(cluster_nodes)
expected_num_of_results = cluster_nodes.count("master")
assert len(info_result) == expected_num_of_results
Expand Down Expand Up @@ -432,7 +432,7 @@ async def test_config_get_set(self, redis_client: TRedisClient):
assert await redis_client.config_get(["timeout"]) == ["timeout", "1000"]
# revert changes to previous timeout
assert isinstance(previous_timeout, list)
assert type(previous_timeout[-1]) == str
assert isinstance(previous_timeout[-1], str)
assert await redis_client.config_set({"timeout": previous_timeout[-1]}) == OK

@pytest.mark.parametrize("cluster_mode", [True, False])
Expand Down Expand Up @@ -545,21 +545,21 @@ async def cluster_route_custom_command_multi_nodes(
route: Route,
):
cluster_nodes = await redis_client.custom_command(["CLUSTER", "NODES"])
assert type(cluster_nodes) == str or type(cluster_nodes) == list
assert isinstance(cluster_nodes, (str, list))
cluster_nodes = get_first_result(cluster_nodes)
num_of_nodes = len(cluster_nodes.splitlines())
expected_num_of_results = (
num_of_nodes
if type(route) == AllNodes
if isinstance(route, AllNodes)
else num_of_nodes - cluster_nodes.count("slave")
)
expected_primary_count = cluster_nodes.count("master")
expected_replica_count = (
cluster_nodes.count("slave") if type(route) == AllNodes else 0
cluster_nodes.count("slave") if isinstance(route, AllNodes) else 0
)

all_results = await redis_client.custom_command(["INFO", "REPLICATION"], route)
assert type(all_results) == list
assert isinstance(all_results, list)
assert len(all_results) == expected_num_of_results
primary_count = 0
replica_count = 0
Expand Down Expand Up @@ -615,7 +615,7 @@ async def cluster_route_custom_command_slot_route(
replica_res = await redis_client.custom_command(
["CLUSTER", "NODES"], route_class(SlotType.REPLICA, route_second_arg)
)
assert type(replica_res) == str
assert isinstance(replica_res, str)
assert "myself,slave" in replica_res
for node_line in replica_res:
if "myself" in node_line:
Expand All @@ -637,5 +637,5 @@ async def test_cluster_route_custom_command_slot_id_route(
@pytest.mark.parametrize("cluster_mode", [True])
async def test_info_random_route(self, redis_client: RedisClusterClient):
info = await redis_client.info([InfoSection.SERVER], RandomNode())
assert type(info) == str
assert isinstance(info, str)
assert "# Server" in info

0 comments on commit 0ee6f21

Please sign in to comment.