From fab31b4d87d9d0e2a0d7a8eb9fce3bebf665d6e7 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Wed, 15 Jan 2025 12:00:51 +0200 Subject: [PATCH] Moved ClusterParser exceptions to BaseParser class (#3475) * Moved ClusterParser exceptions to BaseParser class * Codestyle fixes * Removed ubused imports * Sorted imports --- docs/clustering.rst | 4 ++-- redis/_parsers/base.py | 12 ++++++++++++ redis/asyncio/cluster.py | 27 ++------------------------- redis/cluster.py | 19 +------------------ 4 files changed, 17 insertions(+), 45 deletions(-) diff --git a/docs/clustering.rst b/docs/clustering.rst index f8320e4e59..cf257d8ad5 100644 --- a/docs/clustering.rst +++ b/docs/clustering.rst @@ -17,8 +17,8 @@ Nodes <#specifying-target-nodes>`__ \| `Multi-key Commands <#multi-key-commands>`__ \| `Known PubSub Limitations <#known-pubsub-limitations>`__ -Creating clusters ------------------ +Connecting to cluster +--------------------- Connecting redis-py to a Redis Cluster instance(s) requires at a minimum a single node for cluster discovery. There are multiple ways in which a diff --git a/redis/_parsers/base.py b/redis/_parsers/base.py index 0137539d66..91a4f74199 100644 --- a/redis/_parsers/base.py +++ b/redis/_parsers/base.py @@ -9,18 +9,24 @@ from async_timeout import timeout as async_timeout from ..exceptions import ( + AskError, AuthenticationError, AuthenticationWrongNumberOfArgsError, BusyLoadingError, + ClusterCrossSlotError, + ClusterDownError, ConnectionError, ExecAbortError, + MasterDownError, ModuleError, + MovedError, NoPermissionError, NoScriptError, OutOfMemoryError, ReadOnlyError, RedisError, ResponseError, + TryAgainError, ) from ..typing import EncodableT from .encoders import Encoder @@ -72,6 +78,12 @@ class BaseParser(ABC): "READONLY": ReadOnlyError, "NOAUTH": AuthenticationError, "NOPERM": NoPermissionError, + "ASK": AskError, + "TRYAGAIN": TryAgainError, + "MOVED": MovedError, + "CLUSTERDOWN": ClusterDownError, + "CROSSSLOT": ClusterCrossSlotError, + "MASTERDOWN": MasterDownError, } @classmethod diff --git a/redis/asyncio/cluster.py b/redis/asyncio/cluster.py index 408fa19363..87a2c16afa 100644 --- a/redis/asyncio/cluster.py +++ b/redis/asyncio/cluster.py @@ -26,7 +26,7 @@ _RedisCallbacksRESP3, ) from redis.asyncio.client import ResponseCallbackT -from redis.asyncio.connection import Connection, DefaultParser, SSLConnection, parse_url +from redis.asyncio.connection import Connection, SSLConnection, parse_url from redis.asyncio.lock import Lock from redis.asyncio.retry import Retry from redis.auth.token import TokenInterface @@ -50,12 +50,10 @@ from redis.exceptions import ( AskError, BusyLoadingError, - ClusterCrossSlotError, ClusterDownError, ClusterError, ConnectionError, DataError, - MasterDownError, MaxConnectionsError, MovedError, RedisClusterException, @@ -66,33 +64,13 @@ TryAgainError, ) from redis.typing import AnyKeyT, EncodableT, KeyT -from redis.utils import ( - deprecated_function, - dict_merge, - get_lib_version, - safe_str, - str_if_bytes, -) +from redis.utils import deprecated_function, get_lib_version, safe_str, str_if_bytes TargetNodesT = TypeVar( "TargetNodesT", str, "ClusterNode", List["ClusterNode"], Dict[Any, "ClusterNode"] ) -class ClusterParser(DefaultParser): - EXCEPTION_CLASSES = dict_merge( - DefaultParser.EXCEPTION_CLASSES, - { - "ASK": AskError, - "CLUSTERDOWN": ClusterDownError, - "CROSSSLOT": ClusterCrossSlotError, - "MASTERDOWN": MasterDownError, - "MOVED": MovedError, - "TRYAGAIN": TryAgainError, - }, - ) - - class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommands): """ Create a new RedisCluster client. @@ -297,7 +275,6 @@ def __init__( kwargs: Dict[str, Any] = { "max_connections": max_connections, "connection_class": Connection, - "parser_class": ClusterParser, # Client related kwargs "credential_provider": credential_provider, "username": username, diff --git a/redis/cluster.py b/redis/cluster.py index 38bd5dde1a..8718493759 100644 --- a/redis/cluster.py +++ b/redis/cluster.py @@ -13,7 +13,7 @@ from redis.client import CaseInsensitiveDict, PubSub, Redis from redis.commands import READ_COMMANDS, RedisClusterCommands from redis.commands.helpers import list_or_args -from redis.connection import ConnectionPool, DefaultParser, parse_url +from redis.connection import ConnectionPool, parse_url from redis.crc import REDIS_CLUSTER_HASH_SLOTS, key_slot from redis.event import ( AfterPooledConnectionsInstantiationEvent, @@ -24,12 +24,10 @@ from redis.exceptions import ( AskError, AuthenticationError, - ClusterCrossSlotError, ClusterDownError, ClusterError, ConnectionError, DataError, - MasterDownError, MovedError, RedisClusterException, RedisError, @@ -193,20 +191,6 @@ def cleanup_kwargs(**kwargs): return connection_kwargs -class ClusterParser(DefaultParser): - EXCEPTION_CLASSES = dict_merge( - DefaultParser.EXCEPTION_CLASSES, - { - "ASK": AskError, - "TRYAGAIN": TryAgainError, - "MOVED": MovedError, - "CLUSTERDOWN": ClusterDownError, - "CROSSSLOT": ClusterCrossSlotError, - "MASTERDOWN": MasterDownError, - }, - ) - - class AbstractRedisCluster: RedisClusterRequestTTL = 16 @@ -692,7 +676,6 @@ def on_connect(self, connection): Initialize the connection, authenticate and select a database and send READONLY if it is set during object initialization. """ - connection.set_parser(ClusterParser) connection.on_connect() if self.read_from_replicas: