@@ -32,7 +32,9 @@ class RedisAccessConfig(AccessConfig):
3232 default = None , description = "If not anonymous, use this uri, if specified."
3333 )
3434 password : Optional [str ] = Field (
35- default = None , description = "If not anonymous, use this password, if specified."
35+ default = None ,
36+ description = "Password used to connect to database if uri is "
37+ "not specified and connection is not anonymous."
3638 )
3739
3840
@@ -41,20 +43,32 @@ class RedisConnectionConfig(ConnectionConfig):
4143 default = RedisAccessConfig (), validate_default = True
4244 )
4345 host : Optional [str ] = Field (
44- default = None , description = "Hostname or IP address of a Redis instance to connect to."
46+ default = None ,
47+ description = "Hostname or IP address of a Redis instance to connect to "
48+ "if uri is not specified." ,
4549 )
4650 database : int = Field (default = 0 , description = "Database index to connect to." )
47- port : int = Field (default = 6379 , description = "port used to connect to database." )
51+ port : Optional [int ] = Field (
52+ default = 6379 , description = "Port used to connect to database if uri is not specified."
53+ )
4854 username : Optional [str ] = Field (
49- default = None , description = "Username used to connect to database."
55+ default = None , description = "Username used to connect to database if uri is not specified."
56+ )
57+ ssl : Optional [bool ] = Field (
58+ default = True ,
59+ description = "Whether the connection should use SSL encryption if uri is not specified." ,
5060 )
51- ssl : bool = Field (default = True , description = "Whether the connection should use SSL encryption." )
5261 connector_type : str = Field (default = CONNECTOR_TYPE , init = False )
5362
5463 @model_validator (mode = "after" )
5564 def validate_host_or_url (self ) -> "RedisConnectionConfig" :
56- if not self .access_config .get_secret_value ().uri and not self .host :
57- raise ValueError ("Please pass a hostname either directly or through uri" )
65+ if not self .access_config .get_secret_value ().uri :
66+ if not self .host :
67+ raise ValueError ("Please pass a hostname either directly or through uri" )
68+ if self .port is None :
69+ raise ValueError ("Since URI is not specified, port cannot be None" )
70+ if self .ssl is None :
71+ raise ValueError ("Since URI is not specified, ssl cannot be None" )
5872 return self
5973
6074 @requires_dependencies (["redis" ], extras = "redis" )
@@ -64,21 +78,20 @@ async def create_async_client(self) -> AsyncGenerator["Redis", None]:
6478
6579 access_config = self .access_config .get_secret_value ()
6680
67- options = {
68- "host" : self .host ,
69- "port" : self .port ,
70- "db" : self .database ,
71- "ssl" : self .ssl ,
72- "username" : self .username ,
73- }
74-
75- if access_config .password :
76- options ["password" ] = access_config .password
77-
7881 if access_config .uri :
7982 async with from_url (access_config .uri ) as client :
8083 yield client
8184 else :
85+ options = {
86+ "host" : self .host ,
87+ "port" : self .port ,
88+ "db" : self .database ,
89+ "ssl" : self .ssl ,
90+ "username" : self .username ,
91+ }
92+
93+ if access_config .password :
94+ options ["password" ] = access_config .password
8295 async with Redis (** options ) as client :
8396 yield client
8497
0 commit comments