Skip to content

Commit 29ea862

Browse files
committed
Fixed passing not supported arguments
1 parent ea521c7 commit 29ea862

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

redis/asyncio/multidb/healthcheck.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import asyncio
2+
import inspect
23
import logging
34
from abc import ABC, abstractmethod
45
from enum import Enum
5-
from typing import List, Optional, Tuple, Union
6+
from typing import List, Optional, Tuple, Type, Union
67

78
from redis.asyncio import Redis as AsyncRedis
89
from redis.asyncio.cluster import RedisCluster as AsyncRedisCluster
@@ -17,6 +18,28 @@
1718
# Type alias for async Redis clients (standalone or cluster)
1819
AsyncRedisClientT = Union[AsyncRedis, AsyncRedisCluster]
1920

21+
22+
def _get_init_params(cls: Type) -> frozenset:
23+
"""Extract parameter names from a class's __init__ method."""
24+
sig = inspect.signature(cls.__init__)
25+
return frozenset(
26+
name
27+
for name, param in sig.parameters.items()
28+
if name != "self"
29+
and param.kind
30+
in (
31+
inspect.Parameter.POSITIONAL_OR_KEYWORD,
32+
inspect.Parameter.KEYWORD_ONLY,
33+
)
34+
)
35+
36+
37+
def _filter_kwargs(kwargs: dict, cls: Type) -> dict:
38+
"""Filter kwargs to only include parameters accepted by the class's __init__."""
39+
allowed = _get_init_params(cls)
40+
return {k: v for k, v in kwargs.items() if k in allowed}
41+
42+
2043
DEFAULT_HEALTH_CHECK_PROBES = 3
2144
DEFAULT_HEALTH_CHECK_INTERVAL = 5
2245
DEFAULT_HEALTH_CHECK_TIMEOUT = 3
@@ -152,19 +175,21 @@ async def get_client(self, database) -> AsyncRedisClientT:
152175
# Check for both sync and async standalone Redis clients
153176
if isinstance(database.client, (AsyncRedis, SyncRedis)):
154177
conn_kwargs = database.client.get_connection_kwargs()
155-
client = AsyncRedis(**conn_kwargs)
178+
filtered_kwargs = _filter_kwargs(conn_kwargs, AsyncRedis)
179+
client = AsyncRedis(**filtered_kwargs)
156180
elif isinstance(database.client, (AsyncRedisCluster, SyncRedisCluster)):
157181
# Cluster client - create a single cluster client that handles
158182
# topology changes internally
159-
conn_kwargs = database.client.connection_kwargs.copy()
183+
conn_kwargs = database.client.get_connection_kwargs().copy()
184+
filtered_kwargs = _filter_kwargs(conn_kwargs, AsyncRedisCluster)
160185
startup_nodes = database.client.startup_nodes
161186
# Use the first node as the startup node
162187
if startup_nodes:
163188
first_node = startup_nodes[0]
164189
client = AsyncRedisCluster(
165190
host=first_node.host,
166191
port=first_node.port,
167-
**conn_kwargs,
192+
**filtered_kwargs,
168193
)
169194
else:
170195
raise ValueError(

0 commit comments

Comments
 (0)