Skip to content

Commit 50245cd

Browse files
Update repr of important classes with module name and recommended "< … (#3001)
* Update repr of important classes with module name and recommended "< ... >" syntax. * update tests which examine repr * formatting
1 parent 3502c4d commit 50245cd

File tree

8 files changed

+66
-60
lines changed

8 files changed

+66
-60
lines changed

redis/asyncio/client.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,10 @@ def __init__(
337337
self._single_conn_lock = asyncio.Lock()
338338

339339
def __repr__(self):
340-
return f"{self.__class__.__name__}<{self.connection_pool!r}>"
340+
return (
341+
f"<{self.__class__.__module__}.{self.__class__.__name__}"
342+
f"({self.connection_pool!r})>"
343+
)
341344

342345
def __await__(self):
343346
return self.initialize().__await__()

redis/asyncio/connection.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def _close(self):
225225

226226
def __repr__(self):
227227
repr_args = ",".join((f"{k}={v}" for k, v in self.repr_pieces()))
228-
return f"{self.__class__.__name__}<{repr_args}>"
228+
return f"<{self.__class__.__module__}.{self.__class__.__name__}({repr_args})>"
229229

230230
@abstractmethod
231231
def repr_pieces(self):
@@ -1031,8 +1031,8 @@ def __init__(
10311031

10321032
def __repr__(self):
10331033
return (
1034-
f"{self.__class__.__name__}"
1035-
f"<{self.connection_class(**self.connection_kwargs)!r}>"
1034+
f"<{self.__class__.__module__}.{self.__class__.__name__}"
1035+
f"({self.connection_class(**self.connection_kwargs)!r})>"
10361036
)
10371037

10381038
def reset(self):

redis/asyncio/sentinel.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ def __init__(self, **kwargs):
3030

3131
def __repr__(self):
3232
pool = self.connection_pool
33-
s = f"{self.__class__.__name__}<service={pool.service_name}"
33+
s = (
34+
f"<{self.__class__.__module__}.{self.__class__.__name__}"
35+
f"(service={pool.service_name}"
36+
)
3437
if self.host:
3538
host_info = f",host={self.host},port={self.port}"
3639
s += host_info
37-
return s + ">"
40+
return s + ")>"
3841

3942
async def connect_to(self, address):
4043
self.host, self.port = address
@@ -120,8 +123,8 @@ def __init__(self, service_name, sentinel_manager, **kwargs):
120123

121124
def __repr__(self):
122125
return (
123-
f"{self.__class__.__name__}"
124-
f"<service={self.service_name}({self.is_master and 'master' or 'slave'})>"
126+
f"<{self.__class__.__module__}.{self.__class__.__name__}"
127+
f"(service={self.service_name}({self.is_master and 'master' or 'slave'}))>"
125128
)
126129

127130
def reset(self):
@@ -242,7 +245,10 @@ def __repr__(self):
242245
f"{sentinel.connection_pool.connection_kwargs['host']}:"
243246
f"{sentinel.connection_pool.connection_kwargs['port']}"
244247
)
245-
return f"{self.__class__.__name__}<sentinels=[{','.join(sentinel_addresses)}]>"
248+
return (
249+
f"<{self.__class__}.{self.__class__.__name__}"
250+
f"(sentinels=[{','.join(sentinel_addresses)}])>"
251+
)
246252

247253
def check_master_state(self, state: dict, service_name: str) -> bool:
248254
if not state["is_master"] or state["is_sdown"] or state["is_odown"]:

redis/client.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,10 @@ def __init__(
334334
self.cache_whitelist = cache_whitelist
335335

336336
def __repr__(self) -> str:
337-
return f"{type(self).__name__}<{repr(self.connection_pool)}>"
337+
return (
338+
f"<{type(self).__module__}.{type(self).__name__}"
339+
f"({repr(self.connection_pool)})>"
340+
)
338341

339342
def get_encoder(self) -> "Encoder":
340343
"""Get the connection pool's encoder"""

redis/connection.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def __init__(
217217

218218
def __repr__(self):
219219
repr_args = ",".join([f"{k}={v}" for k, v in self.repr_pieces()])
220-
return f"{self.__class__.__name__}<{repr_args}>"
220+
return f"<{self.__class__.__module__}.{self.__class__.__name__}({repr_args})>"
221221

222222
@abstractmethod
223223
def repr_pieces(self):
@@ -1004,8 +1004,8 @@ def __init__(
10041004

10051005
def __repr__(self) -> (str, str):
10061006
return (
1007-
f"{type(self).__name__}"
1008-
f"<{repr(self.connection_class(**self.connection_kwargs))}>"
1007+
f"<{type(self).__module__}.{type(self).__name__}"
1008+
f"({repr(self.connection_class(**self.connection_kwargs))})>"
10091009
)
10101010

10111011
def reset(self) -> None:

redis/sentinel.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ def __init__(self, **kwargs):
2424

2525
def __repr__(self):
2626
pool = self.connection_pool
27-
s = f"{type(self).__name__}<service={pool.service_name}%s>"
27+
s = (
28+
f"<{type(self).__module__}.{type(self).__name__}"
29+
f"(service={pool.service_name}%s)>"
30+
)
2831
if self.host:
2932
host_info = f",host={self.host},port={self.port}"
3033
s = s % host_info
@@ -162,7 +165,10 @@ def __init__(self, service_name, sentinel_manager, **kwargs):
162165

163166
def __repr__(self):
164167
role = "master" if self.is_master else "slave"
165-
return f"{type(self).__name__}<service={self.service_name}({role})"
168+
return (
169+
f"<{type(self).__module__}.{type(self).__name__}"
170+
f"(service={self.service_name}({role}))>"
171+
)
166172

167173
def reset(self):
168174
super().reset()
@@ -262,7 +268,10 @@ def __repr__(self):
262268
sentinel_addresses.append(
263269
"{host}:{port}".format_map(sentinel.connection_pool.connection_kwargs)
264270
)
265-
return f'{type(self).__name__}<sentinels=[{",".join(sentinel_addresses)}]>'
271+
return (
272+
f"<{type(self).__module__}.{type(self).__name__}"
273+
f'(sentinels=[{",".join(sentinel_addresses)}])>'
274+
)
266275

267276
def check_master_state(self, state, service_name):
268277
if not state["is_master"] or state["is_sdown"] or state["is_odown"]:

tests/test_asyncio/test_connection_pool.py

+15-22
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,17 @@ async def test_repr_contains_db_info_tcp(self):
180180
async with self.get_pool(
181181
connection_kwargs=connection_kwargs, connection_class=redis.Connection
182182
) as pool:
183-
expected = (
184-
"ConnectionPool<Connection<"
185-
"host=localhost,port=6379,db=1,client_name=test-client>>"
186-
)
187-
assert repr(pool) == expected
183+
expected = "host=localhost,port=6379,db=1,client_name=test-client"
184+
assert expected in repr(pool)
188185

189186
async def test_repr_contains_db_info_unix(self):
190187
connection_kwargs = {"path": "/abc", "db": 1, "client_name": "test-client"}
191188
async with self.get_pool(
192189
connection_kwargs=connection_kwargs,
193190
connection_class=redis.UnixDomainSocketConnection,
194191
) as pool:
195-
expected = (
196-
"ConnectionPool<UnixDomainSocketConnection<"
197-
"path=/abc,db=1,client_name=test-client>>"
198-
)
199-
assert repr(pool) == expected
192+
expected = "path=/abc,db=1,client_name=test-client"
193+
assert expected in repr(pool)
200194

201195

202196
class TestBlockingConnectionPool:
@@ -293,23 +287,17 @@ def test_repr_contains_db_info_tcp(self):
293287
pool = redis.ConnectionPool(
294288
host="localhost", port=6379, client_name="test-client"
295289
)
296-
expected = (
297-
"ConnectionPool<Connection<"
298-
"host=localhost,port=6379,db=0,client_name=test-client>>"
299-
)
300-
assert repr(pool) == expected
290+
expected = "host=localhost,port=6379,db=0,client_name=test-client"
291+
assert expected in repr(pool)
301292

302293
def test_repr_contains_db_info_unix(self):
303294
pool = redis.ConnectionPool(
304295
connection_class=redis.UnixDomainSocketConnection,
305296
path="abc",
306297
client_name="test-client",
307298
)
308-
expected = (
309-
"ConnectionPool<UnixDomainSocketConnection<"
310-
"path=abc,db=0,client_name=test-client>>"
311-
)
312-
assert repr(pool) == expected
299+
expected = "path=abc,db=0,client_name=test-client"
300+
assert expected in repr(pool)
313301

314302

315303
class TestConnectionPoolURLParsing:
@@ -659,7 +647,10 @@ def test_connect_from_url_tcp(self):
659647
connection = redis.Redis.from_url("redis://localhost")
660648
pool = connection.connection_pool
661649

662-
assert re.match("(.*)<(.*)<(.*)>>", repr(pool)).groups() == (
650+
print(repr(pool))
651+
assert re.match(
652+
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
653+
).groups() == (
663654
"ConnectionPool",
664655
"Connection",
665656
"host=localhost,port=6379,db=0",
@@ -669,7 +660,9 @@ def test_connect_from_url_unix(self):
669660
connection = redis.Redis.from_url("unix:///path/to/socket")
670661
pool = connection.connection_pool
671662

672-
assert re.match("(.*)<(.*)<(.*)>>", repr(pool)).groups() == (
663+
assert re.match(
664+
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
665+
).groups() == (
673666
"ConnectionPool",
674667
"UnixDomainSocketConnection",
675668
"path=/path/to/socket,db=0",

tests/test_connection_pool.py

+14-22
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,17 @@ def test_repr_contains_db_info_tcp(self):
9595
pool = self.get_pool(
9696
connection_kwargs=connection_kwargs, connection_class=redis.Connection
9797
)
98-
expected = (
99-
"ConnectionPool<Connection<"
100-
"host=localhost,port=6379,db=1,client_name=test-client>>"
101-
)
102-
assert repr(pool) == expected
98+
expected = "host=localhost,port=6379,db=1,client_name=test-client"
99+
assert expected in repr(pool)
103100

104101
def test_repr_contains_db_info_unix(self):
105102
connection_kwargs = {"path": "/abc", "db": 1, "client_name": "test-client"}
106103
pool = self.get_pool(
107104
connection_kwargs=connection_kwargs,
108105
connection_class=redis.UnixDomainSocketConnection,
109106
)
110-
expected = (
111-
"ConnectionPool<UnixDomainSocketConnection<"
112-
"path=/abc,db=1,client_name=test-client>>"
113-
)
114-
assert repr(pool) == expected
107+
expected = "path=/abc,db=1,client_name=test-client"
108+
assert expected in repr(pool)
115109

116110

117111
class TestBlockingConnectionPool:
@@ -190,23 +184,17 @@ def test_repr_contains_db_info_tcp(self):
190184
pool = redis.ConnectionPool(
191185
host="localhost", port=6379, client_name="test-client"
192186
)
193-
expected = (
194-
"ConnectionPool<Connection<"
195-
"host=localhost,port=6379,db=0,client_name=test-client>>"
196-
)
197-
assert repr(pool) == expected
187+
expected = "host=localhost,port=6379,db=0,client_name=test-client"
188+
assert expected in repr(pool)
198189

199190
def test_repr_contains_db_info_unix(self):
200191
pool = redis.ConnectionPool(
201192
connection_class=redis.UnixDomainSocketConnection,
202193
path="abc",
203194
client_name="test-client",
204195
)
205-
expected = (
206-
"ConnectionPool<UnixDomainSocketConnection<"
207-
"path=abc,db=0,client_name=test-client>>"
208-
)
209-
assert repr(pool) == expected
196+
expected = "path=abc,db=0,client_name=test-client"
197+
assert expected in repr(pool)
210198

211199

212200
class TestConnectionPoolURLParsing:
@@ -579,7 +567,9 @@ def test_connect_from_url_tcp(self):
579567
connection = redis.Redis.from_url("redis://localhost")
580568
pool = connection.connection_pool
581569

582-
assert re.match("(.*)<(.*)<(.*)>>", repr(pool)).groups() == (
570+
assert re.match(
571+
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
572+
).groups() == (
583573
"ConnectionPool",
584574
"Connection",
585575
"host=localhost,port=6379,db=0",
@@ -589,7 +579,9 @@ def test_connect_from_url_unix(self):
589579
connection = redis.Redis.from_url("unix:///path/to/socket")
590580
pool = connection.connection_pool
591581

592-
assert re.match("(.*)<(.*)<(.*)>>", repr(pool)).groups() == (
582+
assert re.match(
583+
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
584+
).groups() == (
593585
"ConnectionPool",
594586
"UnixDomainSocketConnection",
595587
"path=/path/to/socket,db=0",

0 commit comments

Comments
 (0)