Skip to content

Commit 9cda16f

Browse files
authored
Improve redis.sentinel types (#8331)
1 parent e3d4bdc commit 9cda16f

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

stubs/redis/redis/sentinel.pyi

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1+
from collections.abc import Iterable, Iterator
12
from typing import Any, TypeVar, overload
2-
from typing_extensions import Literal
3+
from typing_extensions import Literal, TypeAlias
34

45
from redis.client import Redis
56
from redis.commands.sentinel import SentinelCommands
67
from redis.connection import Connection, ConnectionPool, SSLConnection
78
from redis.exceptions import ConnectionError
89

910
_RedisT = TypeVar("_RedisT", bound=Redis[Any])
11+
_AddressAndPort: TypeAlias = tuple[str, int]
12+
_SentinelState: TypeAlias = dict[str, Any] # TODO: this can be a TypedDict
1013

1114
class MasterNotFoundError(ConnectionError): ...
1215
class SlaveNotFoundError(ConnectionError): ...
1316

1417
class SentinelManagedConnection(Connection):
15-
connection_pool: Any
18+
connection_pool: SentinelConnectionPool
1619
def __init__(self, **kwargs) -> None: ...
17-
def connect_to(self, address) -> None: ...
20+
def connect_to(self, address: _AddressAndPort) -> None: ...
1821
def connect(self) -> None: ...
19-
def read_response(self, disable_decoding: bool = ...): ...
22+
# The result can be either `str | bytes` or `list[str | bytes]`
23+
def read_response(self, disable_decoding: bool = ...) -> Any: ...
2024

2125
class SentinelManagedSSLConnection(SentinelManagedConnection, SSLConnection): ...
2226

@@ -25,25 +29,29 @@ class SentinelConnectionPool(ConnectionPool):
2529
check_connection: bool
2630
connection_kwargs: Any
2731
service_name: str
28-
sentinel_manager: Any
29-
def __init__(self, service_name, sentinel_manager, **kwargs) -> None: ...
32+
sentinel_manager: Sentinel
33+
def __init__(self, service_name: str, sentinel_manager: Sentinel, **kwargs) -> None: ...
3034
def reset(self) -> None: ...
31-
def owns_connection(self, connection) -> bool: ...
32-
def get_master_address(self): ...
33-
def rotate_slaves(self): ...
35+
def owns_connection(self, connection: Connection) -> bool: ...
36+
def get_master_address(self) -> _AddressAndPort: ...
37+
def rotate_slaves(self) -> Iterator[_AddressAndPort]: ...
3438

3539
class Sentinel(SentinelCommands):
3640
sentinel_kwargs: Any
37-
sentinels: Any
41+
sentinels: list[Redis[Any]]
3842
min_other_sentinels: int
3943
connection_kwargs: Any
4044
def __init__(
41-
self, sentinels, min_other_sentinels: int = ..., sentinel_kwargs: Any | None = ..., **connection_kwargs
45+
self,
46+
sentinels: Iterable[_AddressAndPort],
47+
min_other_sentinels: int = ...,
48+
sentinel_kwargs: Any | None = ...,
49+
**connection_kwargs,
4250
) -> None: ...
43-
def check_master_state(self, state, service_name) -> bool: ...
44-
def discover_master(self, service_name): ...
45-
def filter_slaves(self, slaves): ...
46-
def discover_slaves(self, service_name): ...
51+
def check_master_state(self, state: _SentinelState, service_name: str) -> bool: ...
52+
def discover_master(self, service_name: str) -> _AddressAndPort: ...
53+
def filter_slaves(self, slaves: Iterable[_SentinelState]) -> list[_AddressAndPort]: ...
54+
def discover_slaves(self, service_name: str) -> list[_AddressAndPort]: ...
4755
@overload
4856
def master_for(self, service_name: str, *, connection_pool_class=..., **kwargs) -> Redis[Any]: ...
4957
@overload

0 commit comments

Comments
 (0)