1
1
from datetime import timedelta
2
- from typing import Any , Callable , Dict , Iterable , Iterator , List , Mapping , Optional , Sequence , Set , Text , Tuple , Union , overload
2
+ from typing import (
3
+ Any ,
4
+ Callable ,
5
+ Dict ,
6
+ Generic ,
7
+ Iterable ,
8
+ Iterator ,
9
+ List ,
10
+ Mapping ,
11
+ Optional ,
12
+ Sequence ,
13
+ Set ,
14
+ Text ,
15
+ Tuple ,
16
+ TypeVar ,
17
+ Union ,
18
+ overload ,
19
+ )
3
20
from typing_extensions import Literal
4
21
5
22
from .connection import ConnectionPool
@@ -38,14 +55,174 @@ def parse_slowlog_get(response, **options): ...
38
55
_Value = Union [bytes , float , int , Text ]
39
56
_Key = Union [Text , bytes ]
40
57
41
- class Redis (object ):
58
+ # Lib returns str or bytes depending on Python version and value of decode_responses
59
+ _StrType = TypeVar ("_StrType" , bound = Union [Text , bytes ])
60
+
61
+ class Redis (Generic [_StrType ]):
42
62
RESPONSE_CALLBACKS : Any
63
+ @overload
64
+ @classmethod
65
+ def from_url (
66
+ cls ,
67
+ url : Text ,
68
+ host : Optional [Text ] = ...,
69
+ port : Optional [int ] = ...,
70
+ db : Optional [int ] = ...,
71
+ password : Optional [Text ] = ...,
72
+ socket_timeout : Optional [float ] = ...,
73
+ socket_connect_timeout : Optional [float ] = ...,
74
+ socket_keepalive : Optional [bool ] = ...,
75
+ socket_keepalive_options : Optional [Mapping [str , Union [int , str ]]] = ...,
76
+ connection_pool : Optional [ConnectionPool ] = ...,
77
+ unix_socket_path : Optional [Text ] = ...,
78
+ encoding : Text = ...,
79
+ encoding_errors : Text = ...,
80
+ charset : Optional [Text ] = ...,
81
+ errors : Optional [Text ] = ...,
82
+ decode_responses : Optional [bool ] = ...,
83
+ retry_on_timeout : bool = ...,
84
+ ssl : bool = ...,
85
+ ssl_keyfile : Optional [Text ] = ...,
86
+ ssl_certfile : Optional [Text ] = ...,
87
+ ssl_cert_reqs : Optional [Union [str , int ]] = ...,
88
+ ssl_ca_certs : Optional [Text ] = ...,
89
+ ssl_check_hostname : bool = ...,
90
+ max_connections : Optional [int ] = ...,
91
+ single_connection_client : bool = ...,
92
+ health_check_interval : float = ...,
93
+ client_name : Optional [Text ] = ...,
94
+ username : Optional [Text ] = ...,
95
+ ) -> Redis [bytes ]: ...
96
+ @overload
43
97
@classmethod
44
- def from_url (cls , url : Text , db : Optional [int ] = ..., ** kwargs ) -> Redis : ...
98
+ def from_url (
99
+ cls ,
100
+ url : Text ,
101
+ host : Optional [Text ] = ...,
102
+ port : Optional [int ] = ...,
103
+ db : Optional [int ] = ...,
104
+ password : Optional [Text ] = ...,
105
+ socket_timeout : Optional [float ] = ...,
106
+ socket_connect_timeout : Optional [float ] = ...,
107
+ socket_keepalive : Optional [bool ] = ...,
108
+ socket_keepalive_options : Optional [Mapping [str , Union [int , str ]]] = ...,
109
+ connection_pool : Optional [ConnectionPool ] = ...,
110
+ unix_socket_path : Optional [Text ] = ...,
111
+ encoding : Text = ...,
112
+ encoding_errors : Text = ...,
113
+ charset : Optional [Text ] = ...,
114
+ decode_responses : Literal [True ] = ...,
115
+ errors : Optional [Text ] = ...,
116
+ retry_on_timeout : bool = ...,
117
+ ssl : bool = ...,
118
+ ssl_keyfile : Optional [Text ] = ...,
119
+ ssl_certfile : Optional [Text ] = ...,
120
+ ssl_cert_reqs : Optional [Union [str , int ]] = ...,
121
+ ssl_ca_certs : Optional [Text ] = ...,
122
+ ssl_check_hostname : bool = ...,
123
+ max_connections : Optional [int ] = ...,
124
+ single_connection_client : bool = ...,
125
+ health_check_interval : float = ...,
126
+ client_name : Optional [Text ] = ...,
127
+ username : Optional [Text ] = ...,
128
+ ) -> Redis [str ]: ...
45
129
connection_pool : Any
46
130
response_callbacks : Any
131
+ @overload
132
+ def __new__ (
133
+ cls ,
134
+ host : Text = ...,
135
+ port : int = ...,
136
+ db : int = ...,
137
+ password : Optional [Text ] = ...,
138
+ socket_timeout : Optional [float ] = ...,
139
+ socket_connect_timeout : Optional [float ] = ...,
140
+ socket_keepalive : Optional [bool ] = ...,
141
+ socket_keepalive_options : Optional [Mapping [str , Union [int , str ]]] = ...,
142
+ connection_pool : Optional [ConnectionPool ] = ...,
143
+ unix_socket_path : Optional [Text ] = ...,
144
+ encoding : Text = ...,
145
+ encoding_errors : Text = ...,
146
+ charset : Optional [Text ] = ...,
147
+ errors : Optional [Text ] = ...,
148
+ retry_on_timeout : bool = ...,
149
+ ssl : bool = ...,
150
+ ssl_keyfile : Optional [Text ] = ...,
151
+ ssl_certfile : Optional [Text ] = ...,
152
+ ssl_cert_reqs : Optional [Union [str , int ]] = ...,
153
+ ssl_ca_certs : Optional [Text ] = ...,
154
+ ssl_check_hostname : bool = ...,
155
+ max_connections : Optional [int ] = ...,
156
+ single_connection_client : bool = ...,
157
+ health_check_interval : float = ...,
158
+ client_name : Optional [Text ] = ...,
159
+ username : Optional [Text ] = ...,
160
+ ) -> Redis [bytes ]: ...
161
+ @overload
162
+ def __new__ (
163
+ cls ,
164
+ host : Text = ...,
165
+ port : int = ...,
166
+ db : int = ...,
167
+ password : Optional [Text ] = ...,
168
+ socket_timeout : Optional [float ] = ...,
169
+ socket_connect_timeout : Optional [float ] = ...,
170
+ socket_keepalive : Optional [bool ] = ...,
171
+ socket_keepalive_options : Optional [Mapping [str , Union [int , str ]]] = ...,
172
+ connection_pool : Optional [ConnectionPool ] = ...,
173
+ unix_socket_path : Optional [Text ] = ...,
174
+ encoding : Text = ...,
175
+ encoding_errors : Text = ...,
176
+ charset : Optional [Text ] = ...,
177
+ errors : Optional [Text ] = ...,
178
+ decode_responses : Literal [True ] = ...,
179
+ retry_on_timeout : bool = ...,
180
+ ssl : bool = ...,
181
+ ssl_keyfile : Optional [Text ] = ...,
182
+ ssl_certfile : Optional [Text ] = ...,
183
+ ssl_cert_reqs : Optional [Union [str , int ]] = ...,
184
+ ssl_ca_certs : Optional [Text ] = ...,
185
+ ssl_check_hostname : bool = ...,
186
+ max_connections : Optional [int ] = ...,
187
+ single_connection_client : bool = ...,
188
+ health_check_interval : float = ...,
189
+ client_name : Optional [Text ] = ...,
190
+ username : Optional [Text ] = ...,
191
+ ) -> Redis [str ]: ...
192
+ @overload
47
193
def __init__ (
48
- self ,
194
+ self : Redis [str ],
195
+ host : Text = ...,
196
+ port : int = ...,
197
+ db : int = ...,
198
+ password : Optional [Text ] = ...,
199
+ socket_timeout : Optional [float ] = ...,
200
+ socket_connect_timeout : Optional [float ] = ...,
201
+ socket_keepalive : Optional [bool ] = ...,
202
+ socket_keepalive_options : Optional [Mapping [str , Union [int , str ]]] = ...,
203
+ connection_pool : Optional [ConnectionPool ] = ...,
204
+ unix_socket_path : Optional [Text ] = ...,
205
+ encoding : Text = ...,
206
+ encoding_errors : Text = ...,
207
+ charset : Optional [Text ] = ...,
208
+ errors : Optional [Text ] = ...,
209
+ decode_responses : Literal [True ] = ...,
210
+ retry_on_timeout : bool = ...,
211
+ ssl : bool = ...,
212
+ ssl_keyfile : Optional [Text ] = ...,
213
+ ssl_certfile : Optional [Text ] = ...,
214
+ ssl_cert_reqs : Optional [Union [str , int ]] = ...,
215
+ ssl_ca_certs : Optional [Text ] = ...,
216
+ ssl_check_hostname : bool = ...,
217
+ max_connections : Optional [int ] = ...,
218
+ single_connection_client : bool = ...,
219
+ health_check_interval : float = ...,
220
+ client_name : Optional [Text ] = ...,
221
+ username : Optional [Text ] = ...,
222
+ ) -> None : ...
223
+ @overload
224
+ def __init__ (
225
+ self : Redis [bytes ],
49
226
host : Text = ...,
50
227
port : int = ...,
51
228
db : int = ...,
@@ -60,7 +237,7 @@ class Redis(object):
60
237
encoding_errors : Text = ...,
61
238
charset : Optional [Text ] = ...,
62
239
errors : Optional [Text ] = ...,
63
- decode_responses : bool = ...,
240
+ decode_responses : Optional [ bool ] = ...,
64
241
retry_on_timeout : bool = ...,
65
242
ssl : bool = ...,
66
243
ssl_keyfile : Optional [Text ] = ...,
@@ -153,11 +330,11 @@ class Redis(object):
153
330
__contains__ : Any
154
331
def expire (self , name : _Key , time : Union [int , timedelta ]) -> bool : ...
155
332
def expireat (self , name , when ): ...
156
- def get (self , name : _Key ) -> Any : ... # Optional[Union[str, bytes]] depending on decode_responses
333
+ def get (self , name : _Key ) -> Optional [ _StrType ] : ...
157
334
def __getitem__ (self , name ): ...
158
335
def getbit (self , name : _Key , offset : int ) -> int : ...
159
336
def getrange (self , key , start , end ): ...
160
- def getset (self , name , value ): ...
337
+ def getset (self , name , value ) -> Optional [ _StrType ] : ...
161
338
def incr (self , name , amount = ...): ...
162
339
def incrby (self , name , amount = ...): ...
163
340
def incrbyfloat (self , name , amount = ...): ...
@@ -197,18 +374,18 @@ class Redis(object):
197
374
def watch (self , * names ): ...
198
375
def unlink (self , * names : _Key ) -> int : ...
199
376
def unwatch (self ): ...
200
- def blpop (self , keys : Union [_Value , Iterable [_Value ]], timeout : int = ...) -> Optional [Tuple [bytes , bytes ]]: ...
201
- def brpop (self , keys : Union [_Value , Iterable [_Value ]], timeout : int = ...) -> Optional [Tuple [bytes , bytes ]]: ...
377
+ def blpop (self , keys : Union [_Value , Iterable [_Value ]], timeout : int = ...) -> Optional [Tuple [_StrType , _StrType ]]: ...
378
+ def brpop (self , keys : Union [_Value , Iterable [_Value ]], timeout : int = ...) -> Optional [Tuple [_StrType , _StrType ]]: ...
202
379
def brpoplpush (self , src , dst , timeout = ...): ...
203
- def lindex (self , name : _Key , index : int ) -> Optional [bytes ]: ...
380
+ def lindex (self , name : _Key , index : int ) -> Optional [_StrType ]: ...
204
381
def linsert (
205
382
self , name : _Key , where : Literal ["BEFORE" , "AFTER" , "before" , "after" ], refvalue : _Value , value : _Value
206
383
) -> int : ...
207
384
def llen (self , name : _Key ) -> int : ...
208
385
def lpop (self , name ): ...
209
386
def lpush (self , name : _Value , * values : _Value ) -> int : ...
210
387
def lpushx (self , name , value ): ...
211
- def lrange (self , name : _Key , start : int , end : int ) -> List [bytes ]: ...
388
+ def lrange (self , name : _Key , start : int , end : int ) -> List [_StrType ]: ...
212
389
def lrem (self , name : _Key , count : int , value : _Value ) -> int : ...
213
390
def lset (self , name : _Key , index : int , value : _Value ) -> bool : ...
214
391
def ltrim (self , name : _Key , start : int , end : int ) -> bool : ...
@@ -228,7 +405,7 @@ class Redis(object):
228
405
alpha : bool = ...,
229
406
store : None = ...,
230
407
groups : bool = ...,
231
- ) -> List [bytes ]: ...
408
+ ) -> List [_StrType ]: ...
232
409
@overload
233
410
def sort (
234
411
self ,
@@ -256,15 +433,13 @@ class Redis(object):
256
433
store : _Key ,
257
434
groups : bool = ...,
258
435
) -> int : ...
259
- def scan (
260
- self , cursor : int = ..., match : Optional [_Key ] = ..., count : Optional [int ] = ...
261
- ) -> Tuple [int , List [Any ]]: ... # Tuple[int, List[_Key]] depending on decode_responses
262
- def scan_iter (
263
- self , match : Optional [Text ] = ..., count : Optional [int ] = ...
264
- ) -> Iterator [Any ]: ... # Iterator[_Key] depending on decode_responses
265
- def sscan (self , name : _Key , cursor : int = ..., match : Text = ..., count : int = ...) -> Tuple [int , List [bytes ]]: ...
436
+ def scan (self , cursor : int = ..., match : Optional [_Key ] = ..., count : Optional [int ] = ...) -> Tuple [int , List [_StrType ]]: ...
437
+ def scan_iter (self , match : Optional [Text ] = ..., count : Optional [int ] = ...) -> Iterator [_StrType ]: ...
438
+ def sscan (self , name : _Key , cursor : int = ..., match : Text = ..., count : int = ...) -> Tuple [int , List [_StrType ]]: ...
266
439
def sscan_iter (self , name , match = ..., count = ...): ...
267
- def hscan (self , name : _Key , cursor : int = ..., match : Text = ..., count : int = ...) -> Tuple [int , Dict [bytes , bytes ]]: ...
440
+ def hscan (
441
+ self , name : _Key , cursor : int = ..., match : Text = ..., count : int = ...
442
+ ) -> Tuple [int , Dict [_StrType , _StrType ]]: ...
268
443
def hscan_iter (self , name , match = ..., count = ...): ...
269
444
def zscan (self , name , cursor = ..., match = ..., count = ..., score_cast_func = ...): ...
270
445
def zscan_iter (self , name , match = ..., count = ..., score_cast_func = ...): ...
@@ -346,27 +521,27 @@ class Redis(object):
346
521
def pfmerge (self , dest : _Key , * sources : _Key ) -> bool : ...
347
522
def hdel (self , name : _Key , * keys : _Key ) -> int : ...
348
523
def hexists (self , name : _Key , key : _Key ) -> bool : ...
349
- def hget (self , name : _Key , key : _Key ) -> Optional [bytes ]: ...
350
- def hgetall (self , name : _Key ) -> Dict [bytes , bytes ]: ...
524
+ def hget (self , name : _Key , key : _Key ) -> Optional [_StrType ]: ...
525
+ def hgetall (self , name : _Key ) -> Dict [_StrType , _StrType ]: ...
351
526
def hincrby (self , name : _Key , key : _Key , amount : int = ...) -> int : ...
352
527
def hincrbyfloat (self , name : _Key , key : _Key , amount : float = ...) -> float : ...
353
- def hkeys (self , name : _Key ) -> List [bytes ]: ...
528
+ def hkeys (self , name : _Key ) -> List [_StrType ]: ...
354
529
def hlen (self , name : _Key ) -> int : ...
355
530
def hset (
356
531
self , name : _Key , key : Optional [_Key ], value : Optional [_Value ], mapping : Optional [Mapping [_Value , _Value ]] = ...
357
532
) -> int : ...
358
533
def hsetnx (self , name : _Key , key : _Key , value : _Value ) -> int : ...
359
534
def hmset (self , name : _Key , mapping : Mapping [_Value , _Value ]) -> bool : ...
360
- def hmget (self , name : _Key , keys : Union [_Key , Iterable [_Key ]], * args : _Key ) -> List [Optional [bytes ]]: ...
361
- def hvals (self , name : _Key ) -> List [bytes ]: ...
535
+ def hmget (self , name : _Key , keys : Union [_Key , Iterable [_Key ]], * args : _Key ) -> List [Optional [_StrType ]]: ...
536
+ def hvals (self , name : _Key ) -> List [_StrType ]: ...
362
537
def publish (self , channel : _Key , message : _Key ) -> int : ...
363
538
def eval (self , script , numkeys , * keys_and_args ): ...
364
539
def evalsha (self , sha , numkeys , * keys_and_args ): ...
365
540
def script_exists (self , * args ): ...
366
541
def script_flush (self ): ...
367
542
def script_kill (self ): ...
368
543
def script_load (self , script ): ...
369
- def register_script (self , script : Union [Text , bytes ]) -> Script : ...
544
+ def register_script (self , script : Union [Text , _StrType ]) -> Script : ...
370
545
def pubsub_channels (self , pattern : _Key = ...) -> List [Text ]: ...
371
546
def pubsub_numsub (self , * args : _Key ) -> List [Tuple [Text , int ]]: ...
372
547
def pubsub_numpat (self ) -> int : ...
0 commit comments