Skip to content

fix: avoid instantiating a connection on _repr__ #3653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion redis/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 5 additions & 3 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,10 +1432,12 @@ def __init__(

self.reset()

def __repr__(self) -> (str, str):
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"({repr(self.connection_class(**self.connection_kwargs))})>"
f"<{self.__class__.__module__}.{self.__class__.__name__}"
f"(<{self.connection_class.__module__}.{self.connection_class.__name__}"
f"({conn_kwargs})>)>"
)

def get_protocol(self):
Expand Down
9 changes: 5 additions & 4 deletions tests/test_asyncio/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -651,15 +652,15 @@ 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(
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
).groups() == (
"ConnectionPool",
"Connection",
"host=localhost,port=6379,db=0",
"db=0,host=localhost,port=6379",
)

def test_connect_from_url_unix(self):
Expand All @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -598,15 +599,15 @@ 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(
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
).groups() == (
"ConnectionPool",
"Connection",
"host=localhost,port=6379,db=0",
"db=0,host=localhost,port=6379",
)

def test_connect_from_url_unix(self):
Expand All @@ -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()
Expand Down
Loading