1+ from typing import Any , Union
2+
13import redis
24from redis .cluster import ClusterNode , RedisCluster
35from redis .connection import Connection , SSLConnection
@@ -46,11 +48,11 @@ def __getattr__(self, item):
4648
4749def init_app (app : DifyApp ):
4850 global redis_client
49- connection_class = Connection
51+ connection_class : type [ Union [ Connection , SSLConnection ]] = Connection
5052 if dify_config .REDIS_USE_SSL :
5153 connection_class = SSLConnection
5254
53- redis_params = {
55+ redis_params : dict [ str , Any ] = {
5456 "username" : dify_config .REDIS_USERNAME ,
5557 "password" : dify_config .REDIS_PASSWORD ,
5658 "db" : dify_config .REDIS_DB ,
@@ -60,6 +62,7 @@ def init_app(app: DifyApp):
6062 }
6163
6264 if dify_config .REDIS_USE_SENTINEL :
65+ assert dify_config .REDIS_SENTINELS is not None , "REDIS_SENTINELS must be set when REDIS_USE_SENTINEL is True"
6366 sentinel_hosts = [
6467 (node .split (":" )[0 ], int (node .split (":" )[1 ])) for node in dify_config .REDIS_SENTINELS .split ("," )
6568 ]
@@ -74,11 +77,13 @@ def init_app(app: DifyApp):
7477 master = sentinel .master_for (dify_config .REDIS_SENTINEL_SERVICE_NAME , ** redis_params )
7578 redis_client .initialize (master )
7679 elif dify_config .REDIS_USE_CLUSTERS :
80+ assert dify_config .REDIS_CLUSTERS is not None , "REDIS_CLUSTERS must be set when REDIS_USE_CLUSTERS is True"
7781 nodes = [
78- ClusterNode (host = node .split (":" )[0 ], port = int (node .split . split (":" )[1 ]))
82+ ClusterNode (host = node .split (":" )[0 ], port = int (node .split (":" )[1 ]))
7983 for node in dify_config .REDIS_CLUSTERS .split ("," )
8084 ]
81- redis_client .initialize (RedisCluster (startup_nodes = nodes , password = dify_config .REDIS_CLUSTERS_PASSWORD ))
85+ # FIXME: mypy error here, try to figure out how to fix it
86+ redis_client .initialize (RedisCluster (startup_nodes = nodes , password = dify_config .REDIS_CLUSTERS_PASSWORD )) # type: ignore
8287 else :
8388 redis_params .update (
8489 {
0 commit comments