Skip to content

Commit c1c602d

Browse files
fix: avoid instantiating a connection on _repr__ (#3653)
* fix: avoid instantiating a connection on _repr__ * include full kwargs * Adding repr fix in async connection pool. Fix failing tests. --------- Co-authored-by: petyaslavova <[email protected]>
1 parent f5cd264 commit c1c602d

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

redis/asyncio/connection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,9 +1112,11 @@ def __init__(
11121112
self._event_dispatcher = EventDispatcher()
11131113

11141114
def __repr__(self):
1115+
conn_kwargs = ",".join([f"{k}={v}" for k, v in self.connection_kwargs.items()])
11151116
return (
11161117
f"<{self.__class__.__module__}.{self.__class__.__name__}"
1117-
f"({self.connection_class(**self.connection_kwargs)!r})>"
1118+
f"(<{self.connection_class.__module__}.{self.connection_class.__name__}"
1119+
f"({conn_kwargs})>)>"
11181120
)
11191121

11201122
def reset(self):

redis/connection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,10 +1432,12 @@ def __init__(
14321432

14331433
self.reset()
14341434

1435-
def __repr__(self) -> (str, str):
1435+
def __repr__(self) -> str:
1436+
conn_kwargs = ",".join([f"{k}={v}" for k, v in self.connection_kwargs.items()])
14361437
return (
1437-
f"<{type(self).__module__}.{type(self).__name__}"
1438-
f"({repr(self.connection_class(**self.connection_kwargs))})>"
1438+
f"<{self.__class__.__module__}.{self.__class__.__name__}"
1439+
f"(<{self.connection_class.__module__}.{self.connection_class.__name__}"
1440+
f"({conn_kwargs})>)>"
14391441
)
14401442

14411443
def get_protocol(self):

tests/test_asyncio/test_connection_pool.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,14 @@ def test_repr_contains_db_info_tcp(self):
294294
pool = redis.ConnectionPool(
295295
host="localhost", port=6379, client_name="test-client"
296296
)
297-
expected = "host=localhost,port=6379,db=0,client_name=test-client"
297+
expected = "host=localhost,port=6379,client_name=test-client"
298298
assert expected in repr(pool)
299299

300300
def test_repr_contains_db_info_unix(self):
301301
pool = redis.ConnectionPool(
302302
connection_class=redis.UnixDomainSocketConnection,
303303
path="abc",
304+
db=0,
304305
client_name="test-client",
305306
)
306307
expected = "path=abc,db=0,client_name=test-client"
@@ -651,15 +652,15 @@ async def test_oom_error(self, r):
651652
await r.execute_command("DEBUG", "ERROR", "OOM blah blah")
652653

653654
def test_connect_from_url_tcp(self):
654-
connection = redis.Redis.from_url("redis://localhost")
655+
connection = redis.Redis.from_url("redis://localhost:6379?db=0")
655656
pool = connection.connection_pool
656657

657658
assert re.match(
658659
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
659660
).groups() == (
660661
"ConnectionPool",
661662
"Connection",
662-
"host=localhost,port=6379,db=0",
663+
"db=0,host=localhost,port=6379",
663664
)
664665

665666
def test_connect_from_url_unix(self):
@@ -671,7 +672,7 @@ def test_connect_from_url_unix(self):
671672
).groups() == (
672673
"ConnectionPool",
673674
"UnixDomainSocketConnection",
674-
"path=/path/to/socket,db=0",
675+
"path=/path/to/socket",
675676
)
676677

677678
@skip_if_redis_enterprise()

tests/test_connection_pool.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,14 @@ def test_repr_contains_db_info_tcp(self):
205205
pool = redis.ConnectionPool(
206206
host="localhost", port=6379, client_name="test-client"
207207
)
208-
expected = "host=localhost,port=6379,db=0,client_name=test-client"
208+
expected = "host=localhost,port=6379,client_name=test-client"
209209
assert expected in repr(pool)
210210

211211
def test_repr_contains_db_info_unix(self):
212212
pool = redis.ConnectionPool(
213213
connection_class=redis.UnixDomainSocketConnection,
214214
path="abc",
215+
db=0,
215216
client_name="test-client",
216217
)
217218
expected = "path=abc,db=0,client_name=test-client"
@@ -598,15 +599,15 @@ def test_oom_error(self, r):
598599
r.execute_command("DEBUG", "ERROR", "OOM blah blah")
599600

600601
def test_connect_from_url_tcp(self):
601-
connection = redis.Redis.from_url("redis://localhost")
602+
connection = redis.Redis.from_url("redis://localhost:6379?db=0")
602603
pool = connection.connection_pool
603604

604605
assert re.match(
605606
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
606607
).groups() == (
607608
"ConnectionPool",
608609
"Connection",
609-
"host=localhost,port=6379,db=0",
610+
"db=0,host=localhost,port=6379",
610611
)
611612

612613
def test_connect_from_url_unix(self):
@@ -618,7 +619,7 @@ def test_connect_from_url_unix(self):
618619
).groups() == (
619620
"ConnectionPool",
620621
"UnixDomainSocketConnection",
621-
"path=/path/to/socket,db=0",
622+
"path=/path/to/socket",
622623
)
623624

624625
@skip_if_redis_enterprise()

0 commit comments

Comments
 (0)