1+ # pyright: reportUnknownMemberType=false
2+ from __future__ import annotations
3+
14import _thread
25import json
36import logging
47import random
58import time
69import typing
710
8- from redis import client
11+ import redis
912
1013from . import exceptions , utils
1114
1518DEFAULT_THREAD_SLEEP_TIME = 0.1
1619
1720
18- class PubSubWorkerThread (client .PubSubWorkerThread ): # type: ignore
19- def run (self ):
21+ class PubSubWorkerThread (redis . client .PubSubWorkerThread ): # type: ignore
22+ def run (self ) -> None :
2023 try :
2124 super ().run ()
2225 except Exception : # pragma: no cover
@@ -61,28 +64,29 @@ class RedisLock(utils.LockBase):
6164
6265 '''
6366
64- redis_kwargs : typing . Dict [str , typing .Any ]
65- thread : typing . Optional [ PubSubWorkerThread ]
67+ redis_kwargs : dict [str , typing .Any ]
68+ thread : PubSubWorkerThread | None
6669 channel : str
6770 timeout : float
68- connection : typing . Optional [ client .Redis ]
69- pubsub : typing . Optional [ client .PubSub ] = None
71+ connection : redis . client .Redis [ str ] | None
72+ pubsub : redis . client .PubSub | None = None
7073 close_connection : bool
7174
72- DEFAULT_REDIS_KWARGS : typing .ClassVar [typing . Dict [str , typing .Any ]] = dict (
75+ DEFAULT_REDIS_KWARGS : typing .ClassVar [dict [str , typing .Any ]] = dict (
7376 health_check_interval = 10 ,
77+ decode_responses = True ,
7478 )
7579
7680 def __init__ (
7781 self ,
7882 channel : str ,
79- connection : typing . Optional [ client .Redis ] = None ,
80- timeout : typing . Optional [ float ] = None ,
81- check_interval : typing . Optional [ float ] = None ,
82- fail_when_locked : typing . Optional [ bool ] = False ,
83+ connection : redis . client .Redis [ str ] | None = None ,
84+ timeout : float | None = None ,
85+ check_interval : float | None = None ,
86+ fail_when_locked : bool | None = False ,
8387 thread_sleep_time : float = DEFAULT_THREAD_SLEEP_TIME ,
8488 unavailable_timeout : float = DEFAULT_UNAVAILABLE_TIMEOUT ,
85- redis_kwargs : typing . Optional [ typing .Dict ] = None ,
89+ redis_kwargs : dict [ str , typing .Any ] | None = None ,
8690 ):
8791 # We don't want to close connections given as an argument
8892 self .close_connection = not connection
@@ -103,18 +107,22 @@ def __init__(
103107 fail_when_locked = fail_when_locked ,
104108 )
105109
106- def get_connection (self ) -> client .Redis :
110+ def get_connection (self ) -> redis . client .Redis [ str ] :
107111 if not self .connection :
108- self .connection = client .Redis (** self .redis_kwargs )
112+ self .connection = redis . client .Redis (** self .redis_kwargs )
109113
110114 return self .connection
111115
112- def channel_handler (self , message ) :
116+ def channel_handler (self , message : dict [ str , str ]) -> None :
113117 if message .get ('type' ) != 'message' : # pragma: no cover
114118 return
115119
120+ raw_data = message .get ('data' )
121+ if not raw_data :
122+ return
123+
116124 try :
117- data = json .loads (message . get ( 'data' ) )
125+ data = json .loads (raw_data )
118126 except TypeError : # pragma: no cover
119127 logger .debug ('TypeError while parsing: %r' , message )
120128 return
@@ -128,10 +136,10 @@ def client_name(self):
128136
129137 def acquire ( # type: ignore[override]
130138 self ,
131- timeout : typing . Optional [ float ] = None ,
132- check_interval : typing . Optional [ float ] = None ,
133- fail_when_locked : typing . Optional [ bool ] = None ,
134- ) -> ' RedisLock' :
139+ timeout : float | None = None ,
140+ check_interval : float | None = None ,
141+ fail_when_locked : bool | None = None ,
142+ ) -> RedisLock :
135143 timeout = utils .coalesce (timeout , self .timeout , 0.0 )
136144 check_interval = utils .coalesce (
137145 check_interval ,
@@ -189,7 +197,11 @@ def acquire( # type: ignore[override]
189197
190198 raise exceptions .AlreadyLocked (exceptions )
191199
192- def check_or_kill_lock (self , connection , timeout ):
200+ def check_or_kill_lock (
201+ self ,
202+ connection : redis .client .Redis [str ],
203+ timeout : float ,
204+ ):
193205 # Random channel name to get messages back from the lock
194206 response_channel = f'{ self .channel } -{ random .random ()} '
195207
@@ -217,7 +229,9 @@ def check_or_kill_lock(self, connection, timeout):
217229 for client_ in connection .client_list ('pubsub' ): # pragma: no cover
218230 if client_ .get ('name' ) == self .client_name :
219231 logger .warning ('Killing unavailable redis client: %r' , client_ )
220- connection .client_kill_filter (client_ .get ('id' ))
232+ connection .client_kill_filter ( # pyright: ignore
233+ client_ .get ('id' ),
234+ )
221235 return None
222236
223237 def release (self ):
0 commit comments