From 5c4177f2c29f76f3335c5e84c9a37a42c721c89d Mon Sep 17 00:00:00 2001 From: Roberto Santamaria Date: Thu, 22 May 2025 14:30:00 +0200 Subject: [PATCH 1/3] fix: avoid instantiating a connection on _repr__ --- redis/connection.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/redis/connection.py b/redis/connection.py index dab45906d2..8ab6bb2c70 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -1422,10 +1422,16 @@ def __init__( self._lock = threading.Lock() self.reset() - def __repr__(self) -> (str, str): + def __repr__(self) -> str: + arg = "" + if "host" in self.connection_kwargs: + arg = f"{self.connection_kwargs['host']}:{self.connection_kwargs['port']}" + elif "path" in self.connection_kwargs: + arg = self.connection_kwargs["path"] + return ( f"<{type(self).__module__}.{type(self).__name__}" - f"({repr(self.connection_class(**self.connection_kwargs))})>" + f"({self.connection_class.__name__}<{arg}>)>" ) def get_protocol(self): From 477230f1a01eea7e001f3a3084c937b36c752737 Mon Sep 17 00:00:00 2001 From: Roberto Santamaria Date: Fri, 23 May 2025 11:22:22 +0200 Subject: [PATCH 2/3] include full kwargs --- redis/connection.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/redis/connection.py b/redis/connection.py index c8440474a9..a20ca648ce 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -1425,15 +1425,11 @@ def __init__( self.reset() def __repr__(self) -> str: - arg = "" - if "host" in self.connection_kwargs: - arg = f"{self.connection_kwargs['host']}:{self.connection_kwargs['port']}" - elif "path" in self.connection_kwargs: - arg = self.connection_kwargs["path"] - + conn_kwargs = ",".join([f"{k}={v}" for k, v in self.connection_kwargs.items()]) return ( f"<{type(self).__module__}.{type(self).__name__}" - f"({self.connection_class.__name__}<{arg}>)>" + f"({self.connection_class.__module__}.{self.connection_class.__name__}" + f"({conn_kwargs}))>" ) def get_protocol(self): From 701b6f1cca9b1c69403633487fb66b21e9de26ac Mon Sep 17 00:00:00 2001 From: Petya Slavova Date: Wed, 28 May 2025 14:19:25 +0300 Subject: [PATCH 3/3] Adding repr fix in async connection pool. Fix failing tests. --- redis/asyncio/connection.py | 4 +++- redis/connection.py | 6 +++--- tests/test_asyncio/test_connection_pool.py | 9 +++++---- tests/test_connection_pool.py | 9 +++++---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index 326daaa8f8..95390bd66c 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -1112,9 +1112,11 @@ def __init__( self._event_dispatcher = EventDispatcher() def __repr__(self): + conn_kwargs = ",".join([f"{k}={v}" for k, v in self.connection_kwargs.items()]) return ( f"<{self.__class__.__module__}.{self.__class__.__name__}" - f"({self.connection_class(**self.connection_kwargs)!r})>" + f"(<{self.connection_class.__module__}.{self.connection_class.__name__}" + f"({conn_kwargs})>)>" ) def reset(self): diff --git a/redis/connection.py b/redis/connection.py index 9c43acfb00..131ae68c61 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -1435,9 +1435,9 @@ def __init__( def __repr__(self) -> str: conn_kwargs = ",".join([f"{k}={v}" for k, v in self.connection_kwargs.items()]) return ( - f"<{type(self).__module__}.{type(self).__name__}" - f"({self.connection_class.__module__}.{self.connection_class.__name__}" - f"({conn_kwargs}))>" + f"<{self.__class__.__module__}.{self.__class__.__name__}" + f"(<{self.connection_class.__module__}.{self.connection_class.__name__}" + f"({conn_kwargs})>)>" ) def get_protocol(self): diff --git a/tests/test_asyncio/test_connection_pool.py b/tests/test_asyncio/test_connection_pool.py index 09409e04a8..d016483840 100644 --- a/tests/test_asyncio/test_connection_pool.py +++ b/tests/test_asyncio/test_connection_pool.py @@ -294,13 +294,14 @@ def test_repr_contains_db_info_tcp(self): pool = redis.ConnectionPool( host="localhost", port=6379, client_name="test-client" ) - expected = "host=localhost,port=6379,db=0,client_name=test-client" + expected = "host=localhost,port=6379,client_name=test-client" assert expected in repr(pool) def test_repr_contains_db_info_unix(self): pool = redis.ConnectionPool( connection_class=redis.UnixDomainSocketConnection, path="abc", + db=0, client_name="test-client", ) expected = "path=abc,db=0,client_name=test-client" @@ -651,7 +652,7 @@ async def test_oom_error(self, r): await r.execute_command("DEBUG", "ERROR", "OOM blah blah") def test_connect_from_url_tcp(self): - connection = redis.Redis.from_url("redis://localhost") + connection = redis.Redis.from_url("redis://localhost:6379?db=0") pool = connection.connection_pool assert re.match( @@ -659,7 +660,7 @@ def test_connect_from_url_tcp(self): ).groups() == ( "ConnectionPool", "Connection", - "host=localhost,port=6379,db=0", + "db=0,host=localhost,port=6379", ) def test_connect_from_url_unix(self): @@ -671,7 +672,7 @@ def test_connect_from_url_unix(self): ).groups() == ( "ConnectionPool", "UnixDomainSocketConnection", - "path=/path/to/socket,db=0", + "path=/path/to/socket", ) @skip_if_redis_enterprise() diff --git a/tests/test_connection_pool.py b/tests/test_connection_pool.py index 9e67659fa9..67b2fd5030 100644 --- a/tests/test_connection_pool.py +++ b/tests/test_connection_pool.py @@ -205,13 +205,14 @@ def test_repr_contains_db_info_tcp(self): pool = redis.ConnectionPool( host="localhost", port=6379, client_name="test-client" ) - expected = "host=localhost,port=6379,db=0,client_name=test-client" + expected = "host=localhost,port=6379,client_name=test-client" assert expected in repr(pool) def test_repr_contains_db_info_unix(self): pool = redis.ConnectionPool( connection_class=redis.UnixDomainSocketConnection, path="abc", + db=0, client_name="test-client", ) expected = "path=abc,db=0,client_name=test-client" @@ -598,7 +599,7 @@ def test_oom_error(self, r): r.execute_command("DEBUG", "ERROR", "OOM blah blah") def test_connect_from_url_tcp(self): - connection = redis.Redis.from_url("redis://localhost") + connection = redis.Redis.from_url("redis://localhost:6379?db=0") pool = connection.connection_pool assert re.match( @@ -606,7 +607,7 @@ def test_connect_from_url_tcp(self): ).groups() == ( "ConnectionPool", "Connection", - "host=localhost,port=6379,db=0", + "db=0,host=localhost,port=6379", ) def test_connect_from_url_unix(self): @@ -618,7 +619,7 @@ def test_connect_from_url_unix(self): ).groups() == ( "ConnectionPool", "UnixDomainSocketConnection", - "path=/path/to/socket,db=0", + "path=/path/to/socket", ) @skip_if_redis_enterprise()