Skip to content

Demo potential fix for stubtest metaclass issues with Redis #10115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions stubs/redis/redis/asyncio/cluster.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ from redis.asyncio.connection import BaseParser, Connection, Encoder
from redis.asyncio.parser import CommandsParser
from redis.client import AbstractRedis
from redis.cluster import AbstractRedisCluster, LoadBalancer

# TODO: add AsyncRedisClusterCommands stubs
# from redis.commands import AsyncRedisClusterCommands
from redis.commands import AsyncRedisClusterCommands
from redis.commands.core import _StrType
from redis.credentials import CredentialProvider
from redis.retry import Retry
Expand All @@ -20,7 +18,7 @@ from redis.typing import AnyKeyT, EncodableT, KeyT
# It uses `DefaultParser` in real life, but it is a dynamic base class.
class ClusterParser(BaseParser): ...

class RedisCluster(AbstractRedis, AbstractRedisCluster, Generic[_StrType]): # TODO: AsyncRedisClusterCommands
class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommands, Generic[_StrType]): # type:ignore[misc]
retry: Retry | None
connection_kwargs: dict[str, Any]
nodes_manager: NodesManager
Expand Down Expand Up @@ -152,7 +150,7 @@ class NodesManager:
async def close(self, attr: str = "nodes_cache") -> None: ...
def remap_host_port(self, host: str, port: int) -> tuple[str, int]: ...

class ClusterPipeline(AbstractRedis, AbstractRedisCluster, Generic[_StrType]): # TODO: AsyncRedisClusterCommands
class ClusterPipeline(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommands, Generic[_StrType]): # type:ignore[misc]
def __init__(self, client: RedisCluster[_StrType]) -> None: ...
async def initialize(self) -> Self: ...
async def __aenter__(self) -> Self: ...
Expand All @@ -168,7 +166,7 @@ class ClusterPipeline(AbstractRedis, AbstractRedisCluster, Generic[_StrType]):
def __len__(self) -> int: ...
def execute_command(self, *args: KeyT | EncodableT, **kwargs: Any) -> Self: ...
async def execute(self, raise_on_error: bool = True, allow_redirections: bool = True) -> list[Any]: ...
def mset_nonatomic(self, mapping: Mapping[AnyKeyT, EncodableT]) -> Self: ...
def mset_nonatomic(self, mapping: Mapping[AnyKeyT, EncodableT]) -> Self: ... # type:ignore[override]

class PipelineCommand:
args: Any
Expand Down
2 changes: 1 addition & 1 deletion stubs/redis/redis/asyncio/connection.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def to_bool(value) -> bool | None: ...

URL_QUERY_ARGUMENT_PARSERS: Mapping[str, Callable[..., object]]

class ConnectKwargs(TypedDict):
class ConnectKwargs(TypedDict, total=False):
username: str
password: str
connection_class: type[Connection]
Expand Down
19 changes: 13 additions & 6 deletions stubs/redis/redis/commands/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
from .cluster import RedisClusterCommands as RedisClusterCommands
from .cluster import (
READ_COMMANDS as READ_COMMANDS,
AsyncRedisClusterCommands as AsyncRedisClusterCommands,
RedisClusterCommands as RedisClusterCommands,
)
from .core import AsyncCoreCommands as AsyncCoreCommands, CoreCommands as CoreCommands
from .helpers import list_or_args as list_or_args
from .parser import CommandsParser as CommandsParser
from .redismodules import RedisModuleCommands as RedisModuleCommands
from .redismodules import RedisModuleCommands as RedisModuleCommands # , AsyncRedisModuleCommands as AsyncRedisModuleCommands
from .sentinel import AsyncSentinelCommands as AsyncSentinelCommands, SentinelCommands as SentinelCommands

__all__ = [
"RedisClusterCommands",
"CommandsParser",
"AsyncCoreCommands",
"AsyncRedisClusterCommands",
# "AsyncRedisModuleCommands", # incomplete
"AsyncSentinelCommands",
"CommandsParser",
"CoreCommands",
"list_or_args",
"READ_COMMANDS",
"RedisClusterCommands",
"RedisModuleCommands",
"AsyncSentinelCommands",
"SentinelCommands",
"list_or_args",
]
48 changes: 45 additions & 3 deletions stubs/redis/redis/commands/cluster.pyi
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
from _typeshed import Incomplete
from typing import Generic, NoReturn
from collections.abc import AsyncIterator, Mapping
from typing import Any, Generic, NoReturn

from .core import ACLCommands, DataAccessCommands, ManagementCommands, PubSubCommands, _StrType
from ..asyncio.connection import ConnectionPool as AsyncConnectionPool, Encoder as AsyncEncoder
from ..connection import ConnectionPool, Encoder
from ..typing import AnyKeyT, ClusterCommandsProtocol, EncodableT, KeysT, KeyT, PatternT
from .core import (
ACLCommands,
AsyncACLCommands,
AsyncDataAccessCommands,
AsyncFunctionCommands,
AsyncManagementCommands,
AsyncScriptCommands,
DataAccessCommands,
ManagementCommands,
PubSubCommands,
_StrType,
)

READ_COMMANDS: frozenset[str]

class ClusterMultiKeyCommands:
class ClusterMultiKeyCommands(ClusterCommandsProtocol):
connection_pool: AsyncConnectionPool | ConnectionPool
encoder: AsyncEncoder | Encoder
def mget_nonatomic(self, keys, *args): ...
def mset_nonatomic(self, mapping): ...
def exists(self, *keys): ...
def delete(self, *keys): ...
def touch(self, *keys): ...
def unlink(self, *keys): ...

class AsyncClusterMultiKeyCommands(ClusterMultiKeyCommands):
async def mget_nonatomic(self, keys: KeysT, *args: KeyT) -> list[Any | None]: ...
async def mset_nonatomic(self, mapping: Mapping[AnyKeyT, EncodableT]) -> list[bool]: ...

class ClusterManagementCommands(ManagementCommands):
def slaveof(self, *args, **kwargs) -> None: ...
def replicaof(self, *args, **kwargs) -> None: ...
def swapdb(self, *args, **kwargs) -> None: ...

class AsyncClusterManagementCommands(ClusterManagementCommands, AsyncManagementCommands): # type:ignore[misc]
async def cluster_delslots(self, *slots: EncodableT) -> list[bool]: ...

class ClusterDataAccessCommands(DataAccessCommands[_StrType], Generic[_StrType]):
def stralgo(
self,
Expand All @@ -30,6 +56,13 @@ class ClusterDataAccessCommands(DataAccessCommands[_StrType], Generic[_StrType])
**kwargs,
): ...

class AsyncClusterDataAccessCommands( # type:ignore[misc]
ClusterDataAccessCommands[Incomplete], AsyncDataAccessCommands[Incomplete]
):
async def scan_iter( # type:ignore[override]
self, match: PatternT | None = None, count: int | None = None, _type: str | None = None, **kwargs
) -> AsyncIterator[Incomplete]: ...

class RedisClusterCommands(
ClusterMultiKeyCommands,
ClusterManagementCommands,
Expand Down Expand Up @@ -63,3 +96,12 @@ class RedisClusterCommands(
read_from_replicas: bool
def readonly(self, target_nodes: Incomplete | None = None): ...
def readwrite(self, target_nodes: Incomplete | None = None): ...

class AsyncRedisClusterCommands( # type:ignore[misc]
AsyncClusterMultiKeyCommands,
AsyncClusterManagementCommands,
AsyncACLCommands[Incomplete],
AsyncClusterDataAccessCommands,
AsyncScriptCommands[Incomplete],
AsyncFunctionCommands,
): ...
Loading