Skip to content

Commit 80ba357

Browse files
authored
Merge branch 'master' into vv-test-8.0
2 parents 98df6e8 + afabed6 commit 80ba357

File tree

9 files changed

+265
-45
lines changed

9 files changed

+265
-45
lines changed

docs/clustering.rst

Lines changed: 2 additions & 2 deletions
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

doctests/cmds_cnxmgmt.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# EXAMPLE: cmds_cnxmgmt
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(decode_responses=True)
6+
# HIDE_END
7+
8+
# STEP_START auth1
9+
# REMOVE_START
10+
r.config_set("requirepass", "temp_pass")
11+
# REMOVE_END
12+
res1 = r.auth(password="temp_pass")
13+
print(res1) # >>> True
14+
15+
res2 = r.auth(password="temp_pass", username="default")
16+
print(res2) # >>> True
17+
18+
# REMOVE_START
19+
assert res1 == True
20+
assert res2 == True
21+
r.config_set("requirepass", "")
22+
# REMOVE_END
23+
# STEP_END
24+
25+
# STEP_START auth2
26+
# REMOVE_START
27+
r.acl_setuser("test-user", enabled=True, passwords=["+strong_password"], commands=["+acl"])
28+
# REMOVE_END
29+
res = r.auth(username="test-user", password="strong_password")
30+
print(res) # >>> True
31+
32+
# REMOVE_START
33+
assert res == True
34+
r.acl_deluser("test-user")
35+
# REMOVE_END
36+
# STEP_END

doctests/cmds_hash.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,27 @@
6161
r.delete("myhash")
6262
# REMOVE_END
6363
# STEP_END
64+
65+
# STEP_START hgetall
66+
res10 = r.hset("myhash", mapping={"field1": "Hello", "field2": "World"})
67+
68+
res11 = r.hgetall("myhash")
69+
print(res11) # >>> { "field1": "Hello", "field2": "World" }
70+
71+
# REMOVE_START
72+
assert res11 == { "field1": "Hello", "field2": "World" }
73+
r.delete("myhash")
74+
# REMOVE_END
75+
# STEP_END
76+
77+
# STEP_START hvals
78+
res10 = r.hset("myhash", mapping={"field1": "Hello", "field2": "World"})
79+
80+
res11 = r.hvals("myhash")
81+
print(res11) # >>> [ "Hello", "World" ]
82+
83+
# REMOVE_START
84+
assert res11 == [ "Hello", "World" ]
85+
r.delete("myhash")
86+
# REMOVE_END
87+
# STEP_END

doctests/cmds_list.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# EXAMPLE: cmds_list
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(decode_responses=True)
6+
# HIDE_END
7+
8+
# STEP_START lpush
9+
res1 = r.lpush("mylist", "world")
10+
print(res1) # >>> 1
11+
12+
res2 = r.lpush("mylist", "hello")
13+
print(res2) # >>> 2
14+
15+
res3 = r.lrange("mylist", 0, -1)
16+
print(res3) # >>> [ "hello", "world" ]
17+
18+
# REMOVE_START
19+
assert res3 == [ "hello", "world" ]
20+
r.delete("mylist")
21+
# REMOVE_END
22+
# STEP_END
23+
24+
# STEP_START lrange
25+
res4 = r.rpush("mylist", "one");
26+
print(res4) # >>> 1
27+
28+
res5 = r.rpush("mylist", "two")
29+
print(res5) # >>> 2
30+
31+
res6 = r.rpush("mylist", "three")
32+
print(res6) # >>> 3
33+
34+
res7 = r.lrange('mylist', 0, 0)
35+
print(res7) # >>> [ 'one' ]
36+
37+
res8 = r.lrange('mylist', -3, 2)
38+
print(res8) # >>> [ 'one', 'two', 'three' ]
39+
40+
res9 = r.lrange('mylist', -100, 100)
41+
print(res9) # >>> [ 'one', 'two', 'three' ]
42+
43+
res10 = r.lrange('mylist', 5, 10)
44+
print(res10) # >>> []
45+
46+
# REMOVE_START
47+
assert res7 == [ 'one' ]
48+
assert res8 == [ 'one', 'two', 'three' ]
49+
assert res9 == [ 'one', 'two', 'three' ]
50+
assert res10 == []
51+
r.delete('mylist')
52+
# REMOVE_END
53+
# STEP_END
54+
55+
# STEP_START llen
56+
res11 = r.lpush("mylist", "World")
57+
print(res11) # >>> 1
58+
59+
res12 = r.lpush("mylist", "Hello")
60+
print(res12) # >>> 2
61+
62+
res13 = r.llen("mylist")
63+
print(res13) # >>> 2
64+
65+
# REMOVE_START
66+
assert res13 == 2
67+
r.delete("mylist")
68+
# REMOVE_END
69+
# STEP_END
70+
71+
# STEP_START rpush
72+
res14 = r.rpush("mylist", "hello")
73+
print(res14) # >>> 1
74+
75+
res15 = r.rpush("mylist", "world")
76+
print(res15) # >>> 2
77+
78+
res16 = r.lrange("mylist", 0, -1)
79+
print(res16) # >>> [ "hello", "world" ]
80+
81+
# REMOVE_START
82+
assert res16 == [ "hello", "world" ]
83+
r.delete("mylist")
84+
# REMOVE_END
85+
# STEP_END
86+
87+
# STEP_START lpop
88+
res17 = r.rpush("mylist", *["one", "two", "three", "four", "five"])
89+
print(res17) # >>> 5
90+
91+
res18 = r.lpop("mylist")
92+
print(res18) # >>> "one"
93+
94+
res19 = r.lpop("mylist", 2)
95+
print(res19) # >>> ['two', 'three']
96+
97+
res17 = r.lrange("mylist", 0, -1)
98+
print(res17) # >>> [ "four", "five" ]
99+
100+
# REMOVE_START
101+
assert res17 == [ "four", "five" ]
102+
r.delete("mylist")
103+
# REMOVE_END
104+
# STEP_END
105+
106+
# STEP_START rpop
107+
res18 = r.rpush("mylist", *["one", "two", "three", "four", "five"])
108+
print(res18) # >>> 5
109+
110+
res19 = r.rpop("mylist")
111+
print(res19) # >>> "five"
112+
113+
res20 = r.rpop("mylist", 2)
114+
print(res20) # >>> ['four', 'three']
115+
116+
res21 = r.lrange("mylist", 0, -1)
117+
print(res21) # >>> [ "one", "two" ]
118+
119+
# REMOVE_START
120+
assert res21 == [ "one", "two" ]
121+
r.delete("mylist")
122+
# REMOVE_END
123+
# STEP_END

doctests/cmds_servermgmt.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# EXAMPLE: cmds_servermgmt
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(decode_responses=True)
6+
# HIDE_END
7+
8+
# STEP_START flushall
9+
# REMOVE_START
10+
r.set("foo", "1")
11+
r.set("bar", "2")
12+
r.set("baz", "3")
13+
# REMOVE_END
14+
res1 = r.flushall(asynchronous=False)
15+
print(res1) # >>> True
16+
17+
res2 = r.keys()
18+
print(res2) # >>> []
19+
20+
# REMOVE_START
21+
assert res1 == True
22+
assert res2 == []
23+
# REMOVE_END
24+
# STEP_END
25+
26+
# STEP_START info
27+
res3 = r.info()
28+
print(res3)
29+
# >>> {'redis_version': '7.4.0', 'redis_git_sha1': 'c9d29f6a',...}
30+
# STEP_END

doctests/cmds_set.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# EXAMPLE: cmds_set
2+
# HIDE_START
3+
import redis
4+
5+
r = redis.Redis(decode_responses=True)
6+
# HIDE_END
7+
8+
# STEP_START sadd
9+
res1 = r.sadd("myset", "Hello", "World")
10+
print(res1) # >>> 2
11+
12+
res2 = r.sadd("myset", "World")
13+
print(res2) # >>> 0
14+
15+
res3 = r.smembers("myset")
16+
print(res3) # >>> {'Hello', 'World'}
17+
18+
# REMOVE_START
19+
assert res3 == {'Hello', 'World'}
20+
r.delete('myset')
21+
# REMOVE_END
22+
# STEP_END
23+
24+
# STEP_START smembers
25+
res4 = r.sadd("myset", "Hello", "World")
26+
print(res4) # >>> 2
27+
28+
res5 = r.smembers("myset")
29+
print(res5) # >>> {'Hello', 'World'}
30+
31+
# REMOVE_START
32+
assert res5 == {'Hello', 'World'}
33+
r.delete('myset')
34+
# REMOVE_END
35+
# STEP_END

redis/_parsers/base.py

Lines changed: 12 additions & 0 deletions
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

redis/asyncio/cluster.py

Lines changed: 2 additions & 25 deletions
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,

0 commit comments

Comments
 (0)