46
46
from .helpers import list_or_args
47
47
48
48
if TYPE_CHECKING :
49
- from redis .asyncio .client import Redis as AsyncRedis
50
- from redis .client import Redis
49
+ import redis .asyncio .client
50
+ import redis .client
51
51
52
52
53
53
class ACLCommands (CommandsProtocol ):
@@ -731,16 +731,19 @@ def client_pause(self, timeout: int, all: bool = True, **kwargs) -> ResponseT:
731
731
732
732
For more information see https://redis.io/commands/client-pause
733
733
734
- :param timeout: milliseconds to pause clients
735
- :param all: If true (default) all client commands are blocked.
736
- otherwise, clients are only blocked if they attempt to execute
737
- a write command.
734
+ Args:
735
+ timeout: milliseconds to pause clients
736
+ all: If true (default) all client commands are blocked.
737
+ otherwise, clients are only blocked if they attempt to execute
738
+ a write command.
739
+
738
740
For the WRITE mode, some commands have special behavior:
739
- EVAL/EVALSHA: Will block client for all scripts.
740
- PUBLISH: Will block client.
741
- PFCOUNT: Will block client.
742
- WAIT: Acknowledgments will be delayed, so this command will
743
- appear blocked.
741
+
742
+ * EVAL/EVALSHA: Will block client for all scripts.
743
+ * PUBLISH: Will block client.
744
+ * PFCOUNT: Will block client.
745
+ * WAIT: Acknowledgments will be delayed, so this command will
746
+ appear blocked.
744
747
"""
745
748
args = ["CLIENT PAUSE" , str (timeout )]
746
749
if not isinstance (timeout , int ):
@@ -1439,7 +1442,7 @@ class BitFieldOperation:
1439
1442
1440
1443
def __init__ (
1441
1444
self ,
1442
- client : Union ["Redis" , "AsyncRedis " ],
1445
+ client : Union ["redis.client. Redis" , "redis.asyncio.client.Redis " ],
1443
1446
key : str ,
1444
1447
default_overflow : Union [str , None ] = None ,
1445
1448
):
@@ -1583,7 +1586,7 @@ def bitcount(
1583
1586
return self .execute_command ("BITCOUNT" , * params , keys = [key ])
1584
1587
1585
1588
def bitfield (
1586
- self : Union ["Redis" , "AsyncRedis " ],
1589
+ self : Union ["redis.client. Redis" , "redis.asyncio.client.Redis " ],
1587
1590
key : KeyT ,
1588
1591
default_overflow : Union [str , None ] = None ,
1589
1592
) -> BitFieldOperation :
@@ -1596,7 +1599,7 @@ def bitfield(
1596
1599
return BitFieldOperation (self , key , default_overflow = default_overflow )
1597
1600
1598
1601
def bitfield_ro (
1599
- self : Union ["Redis" , "AsyncRedis " ],
1602
+ self : Union ["redis.client. Redis" , "redis.asyncio.client.Redis " ],
1600
1603
key : KeyT ,
1601
1604
encoding : str ,
1602
1605
offset : BitfieldOffsetT ,
@@ -5464,27 +5467,23 @@ class Script:
5464
5467
An executable Lua script object returned by ``register_script``
5465
5468
"""
5466
5469
5467
- def __init__ (self , registered_client : "Redis" , script : ScriptTextT ):
5470
+ def __init__ (self , registered_client : "redis.client. Redis" , script : ScriptTextT ):
5468
5471
self .registered_client = registered_client
5469
5472
self .script = script
5470
5473
# Precalculate and store the SHA1 hex digest of the script.
5471
5474
5472
5475
if isinstance (script , str ):
5473
5476
# We need the encoding from the client in order to generate an
5474
5477
# accurate byte representation of the script
5475
- try :
5476
- encoder = registered_client .connection_pool .get_encoder ()
5477
- except AttributeError :
5478
- # Cluster
5479
- encoder = registered_client .get_encoder ()
5478
+ encoder = self .get_encoder ()
5480
5479
script = encoder .encode (script )
5481
5480
self .sha = hashlib .sha1 (script ).hexdigest ()
5482
5481
5483
5482
def __call__ (
5484
5483
self ,
5485
5484
keys : Union [Sequence [KeyT ], None ] = None ,
5486
5485
args : Union [Iterable [EncodableT ], None ] = None ,
5487
- client : Union ["Redis" , None ] = None ,
5486
+ client : Union ["redis.client. Redis" , None ] = None ,
5488
5487
):
5489
5488
"""Execute the script, passing any required ``args``"""
5490
5489
keys = keys or []
@@ -5507,13 +5506,35 @@ def __call__(
5507
5506
self .sha = client .script_load (self .script )
5508
5507
return client .evalsha (self .sha , len (keys ), * args )
5509
5508
5509
+ def get_encoder (self ):
5510
+ """Get the encoder to encode string scripts into bytes."""
5511
+ try :
5512
+ return self .registered_client .get_encoder ()
5513
+ except AttributeError :
5514
+ # DEPRECATED
5515
+ # In version <=4.1.2, this was the code we used to get the encoder.
5516
+ # However, after 4.1.2 we added support for scripting in clustered
5517
+ # redis. ClusteredRedis doesn't have a `.connection_pool` attribute
5518
+ # so we changed the Script class to use
5519
+ # `self.registered_client.get_encoder` (see above).
5520
+ # However, that is technically a breaking change, as consumers who
5521
+ # use Scripts directly might inject a `registered_client` that
5522
+ # doesn't have a `.get_encoder` field. This try/except prevents us
5523
+ # from breaking backward-compatibility. Ideally, it would be
5524
+ # removed in the next major release.
5525
+ return self .registered_client .connection_pool .get_encoder ()
5526
+
5510
5527
5511
5528
class AsyncScript :
5512
5529
"""
5513
5530
An executable Lua script object returned by ``register_script``
5514
5531
"""
5515
5532
5516
- def __init__ (self , registered_client : "AsyncRedis" , script : ScriptTextT ):
5533
+ def __init__ (
5534
+ self ,
5535
+ registered_client : "redis.asyncio.client.Redis" ,
5536
+ script : ScriptTextT ,
5537
+ ):
5517
5538
self .registered_client = registered_client
5518
5539
self .script = script
5519
5540
# Precalculate and store the SHA1 hex digest of the script.
@@ -5533,7 +5554,7 @@ async def __call__(
5533
5554
self ,
5534
5555
keys : Union [Sequence [KeyT ], None ] = None ,
5535
5556
args : Union [Iterable [EncodableT ], None ] = None ,
5536
- client : Union ["AsyncRedis " , None ] = None ,
5557
+ client : Union ["redis.asyncio.client.Redis " , None ] = None ,
5537
5558
):
5538
5559
"""Execute the script, passing any required ``args``"""
5539
5560
keys = keys or []
@@ -5758,7 +5779,7 @@ def script_load(self, script: ScriptTextT) -> ResponseT:
5758
5779
"""
5759
5780
return self .execute_command ("SCRIPT LOAD" , script )
5760
5781
5761
- def register_script (self : "Redis" , script : ScriptTextT ) -> Script :
5782
+ def register_script (self : "redis.client. Redis" , script : ScriptTextT ) -> Script :
5762
5783
"""
5763
5784
Register a Lua ``script`` specifying the ``keys`` it will touch.
5764
5785
Returns a Script object that is callable and hides the complexity of
@@ -5772,7 +5793,10 @@ class AsyncScriptCommands(ScriptCommands):
5772
5793
async def script_debug (self , * args ) -> None :
5773
5794
return super ().script_debug ()
5774
5795
5775
- def register_script (self : "AsyncRedis" , script : ScriptTextT ) -> AsyncScript :
5796
+ def register_script (
5797
+ self : "redis.asyncio.client.Redis" ,
5798
+ script : ScriptTextT ,
5799
+ ) -> AsyncScript :
5776
5800
"""
5777
5801
Register a Lua ``script`` specifying the ``keys`` it will touch.
5778
5802
Returns a Script object that is callable and hides the complexity of
@@ -6283,62 +6307,6 @@ def command(self) -> ResponseT:
6283
6307
return self .execute_command ("COMMAND" )
6284
6308
6285
6309
6286
- class Script :
6287
- """
6288
- An executable Lua script object returned by ``register_script``
6289
- """
6290
-
6291
- def __init__ (self , registered_client , script ):
6292
- self .registered_client = registered_client
6293
- self .script = script
6294
- # Precalculate and store the SHA1 hex digest of the script.
6295
-
6296
- if isinstance (script , str ):
6297
- # We need the encoding from the client in order to generate an
6298
- # accurate byte representation of the script
6299
- encoder = self .get_encoder ()
6300
- script = encoder .encode (script )
6301
- self .sha = hashlib .sha1 (script ).hexdigest ()
6302
-
6303
- def __call__ (self , keys = [], args = [], client = None ):
6304
- "Execute the script, passing any required ``args``"
6305
- if client is None :
6306
- client = self .registered_client
6307
- args = tuple (keys ) + tuple (args )
6308
- # make sure the Redis server knows about the script
6309
- from redis .client import Pipeline
6310
-
6311
- if isinstance (client , Pipeline ):
6312
- # Make sure the pipeline can register the script before executing.
6313
- client .scripts .add (self )
6314
- try :
6315
- return client .evalsha (self .sha , len (keys ), * args )
6316
- except NoScriptError :
6317
- # Maybe the client is pointed to a different server than the client
6318
- # that created this instance?
6319
- # Overwrite the sha just in case there was a discrepancy.
6320
- self .sha = client .script_load (self .script )
6321
- return client .evalsha (self .sha , len (keys ), * args )
6322
-
6323
- def get_encoder (self ):
6324
- """Get the encoder to encode string scripts into bytes."""
6325
- try :
6326
- return self .registered_client .get_encoder ()
6327
- except AttributeError :
6328
- # DEPRECATED
6329
- # In version <=4.1.2, this was the code we used to get the encoder.
6330
- # However, after 4.1.2 we added support for scripting in clustered
6331
- # redis. ClusteredRedis doesn't have a `.connection_pool` attribute
6332
- # so we changed the Script class to use
6333
- # `self.registered_client.get_encoder` (see above).
6334
- # However, that is technically a breaking change, as consumers who
6335
- # use Scripts directly might inject a `registered_client` that
6336
- # doesn't have a `.get_encoder` field. This try/except prevents us
6337
- # from breaking backward-compatibility. Ideally, it would be
6338
- # removed in the next major release.
6339
- return self .registered_client .connection_pool .get_encoder ()
6340
-
6341
-
6342
6310
class AsyncModuleCommands (ModuleCommands ):
6343
6311
async def command_info (self ) -> None :
6344
6312
return super ().command_info ()
@@ -6415,9 +6383,12 @@ def function_list(
6415
6383
) -> Union [Awaitable [List ], List ]:
6416
6384
"""
6417
6385
Return information about the functions and libraries.
6418
- :param library: pecify a pattern for matching library names
6419
- :param withcode: cause the server to include the libraries source
6420
- implementation in the reply
6386
+
6387
+ Args:
6388
+
6389
+ library: specify a pattern for matching library names
6390
+ withcode: cause the server to include the libraries source implementation
6391
+ in the reply
6421
6392
"""
6422
6393
args = ["LIBRARYNAME" , library ]
6423
6394
if withcode :
0 commit comments