Skip to content

Commit daab667

Browse files
kristjanvalurgerzse
authored andcommitted
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 3e5830b commit daab667

File tree

8 files changed

+66
-60
lines changed

8 files changed

+66
-60
lines changed

redis/asyncio/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ def __init__(
342342
self._single_conn_lock = asyncio.Lock()
343343

344344
def __repr__(self):
345-
return f"{self.__class__.__name__}<{self.connection_pool!r}>"
345+
return (
346+
f"<{self.__class__.__module__}.{self.__class__.__name__}"
347+
f"({self.connection_pool!r})>"
348+
)
346349

347350
def __await__(self):
348351
return self.initialize().__await__()

redis/asyncio/connection.py

Lines changed: 3 additions & 3 deletions
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):
@@ -1061,8 +1061,8 @@ def __init__(
10611061

10621062
def __repr__(self):
10631063
return (
1064-
f"{self.__class__.__name__}"
1065-
f"<{self.connection_class(**self.connection_kwargs)!r}>"
1064+
f"<{self.__class__.__module__}.{self.__class__.__name__}"
1065+
f"({self.connection_class(**self.connection_kwargs)!r})>"
10661066
)
10671067

10681068
def reset(self):

redis/asyncio/sentinel.py

Lines changed: 11 additions & 5 deletions
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):
@@ -241,7 +244,10 @@ def __repr__(self):
241244
f"{sentinel.connection_pool.connection_kwargs['host']}:"
242245
f"{sentinel.connection_pool.connection_kwargs['port']}"
243246
)
244-
return f"{self.__class__.__name__}<sentinels=[{','.join(sentinel_addresses)}]>"
247+
return (
248+
f"<{self.__class__}.{self.__class__.__name__}"
249+
f"(sentinels=[{','.join(sentinel_addresses)}])>"
250+
)
245251

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

redis/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,10 @@ def __init__(
320320
self.response_callbacks.update(_RedisCallbacksRESP2)
321321

322322
def __repr__(self) -> str:
323-
return f"{type(self).__name__}<{repr(self.connection_pool)}>"
323+
return (
324+
f"<{type(self).__module__}.{type(self).__name__}"
325+
f"({repr(self.connection_pool)})>"
326+
)
324327

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

redis/connection.py

Lines changed: 3 additions & 3 deletions
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):
@@ -1026,8 +1026,8 @@ def __init__(
10261026

10271027
def __repr__(self) -> (str, str):
10281028
return (
1029-
f"{type(self).__name__}"
1030-
f"<{repr(self.connection_class(**self.connection_kwargs))}>"
1029+
f"<{type(self).__module__}.{type(self).__name__}"
1030+
f"({repr(self.connection_class(**self.connection_kwargs))})>"
10311031
)
10321032

10331033
def reset(self) -> None:

redis/sentinel.py

Lines changed: 12 additions & 3 deletions
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()
@@ -261,7 +267,10 @@ def __repr__(self):
261267
sentinel_addresses.append(
262268
"{host}:{port}".format_map(sentinel.connection_pool.connection_kwargs)
263269
)
264-
return f'{type(self).__name__}<sentinels=[{",".join(sentinel_addresses)}]>'
270+
return (
271+
f"<{type(self).__module__}.{type(self).__name__}"
272+
f'(sentinels=[{",".join(sentinel_addresses)}])>'
273+
)
265274

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

tests/test_asyncio/test_connection_pool.py

Lines changed: 15 additions & 22 deletions
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

Lines changed: 14 additions & 22 deletions
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)