Skip to content

Commit fab31b4

Browse files
authored
Moved ClusterParser exceptions to BaseParser class (#3475)
* Moved ClusterParser exceptions to BaseParser class * Codestyle fixes * Removed ubused imports * Sorted imports
1 parent 7a6b412 commit fab31b4

File tree

4 files changed

+17
-45
lines changed

4 files changed

+17
-45
lines changed

Diff for: docs/clustering.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ Nodes <#specifying-target-nodes>`__ \| `Multi-key
1717
Commands <#multi-key-commands>`__ \| `Known PubSub
1818
Limitations <#known-pubsub-limitations>`__
1919

20-
Creating clusters
21-
-----------------
20+
Connecting to cluster
21+
---------------------
2222

2323
Connecting redis-py to a Redis Cluster instance(s) requires at a minimum
2424
a single node for cluster discovery. There are multiple ways in which a

Diff for: redis/_parsers/base.py

+12
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@
99
from async_timeout import timeout as async_timeout
1010

1111
from ..exceptions import (
12+
AskError,
1213
AuthenticationError,
1314
AuthenticationWrongNumberOfArgsError,
1415
BusyLoadingError,
16+
ClusterCrossSlotError,
17+
ClusterDownError,
1518
ConnectionError,
1619
ExecAbortError,
20+
MasterDownError,
1721
ModuleError,
22+
MovedError,
1823
NoPermissionError,
1924
NoScriptError,
2025
OutOfMemoryError,
2126
ReadOnlyError,
2227
RedisError,
2328
ResponseError,
29+
TryAgainError,
2430
)
2531
from ..typing import EncodableT
2632
from .encoders import Encoder
@@ -72,6 +78,12 @@ class BaseParser(ABC):
7278
"READONLY": ReadOnlyError,
7379
"NOAUTH": AuthenticationError,
7480
"NOPERM": NoPermissionError,
81+
"ASK": AskError,
82+
"TRYAGAIN": TryAgainError,
83+
"MOVED": MovedError,
84+
"CLUSTERDOWN": ClusterDownError,
85+
"CROSSSLOT": ClusterCrossSlotError,
86+
"MASTERDOWN": MasterDownError,
7587
}
7688

7789
@classmethod

Diff for: redis/asyncio/cluster.py

+2-25
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
_RedisCallbacksRESP3,
2727
)
2828
from redis.asyncio.client import ResponseCallbackT
29-
from redis.asyncio.connection import Connection, DefaultParser, SSLConnection, parse_url
29+
from redis.asyncio.connection import Connection, SSLConnection, parse_url
3030
from redis.asyncio.lock import Lock
3131
from redis.asyncio.retry import Retry
3232
from redis.auth.token import TokenInterface
@@ -50,12 +50,10 @@
5050
from redis.exceptions import (
5151
AskError,
5252
BusyLoadingError,
53-
ClusterCrossSlotError,
5453
ClusterDownError,
5554
ClusterError,
5655
ConnectionError,
5756
DataError,
58-
MasterDownError,
5957
MaxConnectionsError,
6058
MovedError,
6159
RedisClusterException,
@@ -66,33 +64,13 @@
6664
TryAgainError,
6765
)
6866
from redis.typing import AnyKeyT, EncodableT, KeyT
69-
from redis.utils import (
70-
deprecated_function,
71-
dict_merge,
72-
get_lib_version,
73-
safe_str,
74-
str_if_bytes,
75-
)
67+
from redis.utils import deprecated_function, get_lib_version, safe_str, str_if_bytes
7668

7769
TargetNodesT = TypeVar(
7870
"TargetNodesT", str, "ClusterNode", List["ClusterNode"], Dict[Any, "ClusterNode"]
7971
)
8072

8173

82-
class ClusterParser(DefaultParser):
83-
EXCEPTION_CLASSES = dict_merge(
84-
DefaultParser.EXCEPTION_CLASSES,
85-
{
86-
"ASK": AskError,
87-
"CLUSTERDOWN": ClusterDownError,
88-
"CROSSSLOT": ClusterCrossSlotError,
89-
"MASTERDOWN": MasterDownError,
90-
"MOVED": MovedError,
91-
"TRYAGAIN": TryAgainError,
92-
},
93-
)
94-
95-
9674
class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommands):
9775
"""
9876
Create a new RedisCluster client.
@@ -297,7 +275,6 @@ def __init__(
297275
kwargs: Dict[str, Any] = {
298276
"max_connections": max_connections,
299277
"connection_class": Connection,
300-
"parser_class": ClusterParser,
301278
# Client related kwargs
302279
"credential_provider": credential_provider,
303280
"username": username,

Diff for: redis/cluster.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from redis.client import CaseInsensitiveDict, PubSub, Redis
1414
from redis.commands import READ_COMMANDS, RedisClusterCommands
1515
from redis.commands.helpers import list_or_args
16-
from redis.connection import ConnectionPool, DefaultParser, parse_url
16+
from redis.connection import ConnectionPool, parse_url
1717
from redis.crc import REDIS_CLUSTER_HASH_SLOTS, key_slot
1818
from redis.event import (
1919
AfterPooledConnectionsInstantiationEvent,
@@ -24,12 +24,10 @@
2424
from redis.exceptions import (
2525
AskError,
2626
AuthenticationError,
27-
ClusterCrossSlotError,
2827
ClusterDownError,
2928
ClusterError,
3029
ConnectionError,
3130
DataError,
32-
MasterDownError,
3331
MovedError,
3432
RedisClusterException,
3533
RedisError,
@@ -193,20 +191,6 @@ def cleanup_kwargs(**kwargs):
193191
return connection_kwargs
194192

195193

196-
class ClusterParser(DefaultParser):
197-
EXCEPTION_CLASSES = dict_merge(
198-
DefaultParser.EXCEPTION_CLASSES,
199-
{
200-
"ASK": AskError,
201-
"TRYAGAIN": TryAgainError,
202-
"MOVED": MovedError,
203-
"CLUSTERDOWN": ClusterDownError,
204-
"CROSSSLOT": ClusterCrossSlotError,
205-
"MASTERDOWN": MasterDownError,
206-
},
207-
)
208-
209-
210194
class AbstractRedisCluster:
211195
RedisClusterRequestTTL = 16
212196

@@ -692,7 +676,6 @@ def on_connect(self, connection):
692676
Initialize the connection, authenticate and select a database and send
693677
READONLY if it is set during object initialization.
694678
"""
695-
connection.set_parser(ClusterParser)
696679
connection.on_connect()
697680

698681
if self.read_from_replicas:

0 commit comments

Comments
 (0)