Skip to content

Commit fc1e254

Browse files
authored
Make Redis connection arguments in repr optional (#664)
Ref #659
1 parent 692e0dd commit fc1e254

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

health_check/contrib/redis.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ def __repr__(self):
6262
client = self.client
6363

6464
try:
65-
conn_kwargs = client.connection_pool.connection_kwargs
66-
host = conn_kwargs["host"]
67-
db = conn_kwargs["db"]
68-
return f"Redis(client=RedisClient(host={host}, db={db}))"
69-
except (AttributeError, KeyError):
65+
safe_connection_str = ", ".join(
66+
f"{key}={value!r}"
67+
for key, value in sorted(
68+
client.connection_pool.connection_kwargs.items()
69+
)
70+
if key in {"host", "db"}
71+
)
72+
return f"Redis({safe_connection_str})"
73+
except AttributeError:
7074
pass
7175

7276
try:

tests/contrib/test_redis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_redis__repr_standard_client(self):
156156
check = RedisHealthCheck(
157157
client_factory=lambda: RedisClient(host="myhost", port=6379, db=2)
158158
)
159-
assert repr(check) == "Redis(client=RedisClient(host=myhost, db=2))"
159+
assert repr(check) == "Redis(db=2, host='myhost')"
160160

161161
def test_redis__repr_from_url(self):
162162
"""Verify repr includes host and db when client is created via from_url."""
@@ -167,7 +167,7 @@ def test_redis__repr_from_url(self):
167167
"redis://cache.example.com:6379/3"
168168
)
169169
)
170-
assert "host=cache.example.com" in repr(check), (
170+
assert "host='cache.example.com'" in repr(check), (
171171
"repr should include the host from the Redis URL"
172172
)
173173
assert "db=3" in repr(check), "repr should include the db from the Redis URL"
@@ -180,7 +180,7 @@ def test_redis__repr_deprecated_client(self):
180180
check = RedisHealthCheck(
181181
client=RedisClient(host="oldhost", port=6379, db=5)
182182
)
183-
assert "host=oldhost" in repr(check), (
183+
assert "host='oldhost'" in repr(check), (
184184
"repr should include the host from the deprecated client"
185185
)
186186
assert "db=5" in repr(check), (

0 commit comments

Comments
 (0)