Skip to content

Commit

Permalink
Update repr of important classes with module name and recommended "< … (
Browse files Browse the repository at this point in the history
redis#3001)

* Update repr of important classes with module name and recommended "< ... >" syntax.

* update tests which examine repr

* formatting
  • Loading branch information
kristjanvalur authored and gerzse committed Jul 10, 2024
1 parent 5b2a1c2 commit bc0461c
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 60 deletions.
5 changes: 4 additions & 1 deletion redis/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ def __init__(
self._single_conn_lock = asyncio.Lock()

def __repr__(self):
return f"{self.__class__.__name__}<{self.connection_pool!r}>"
return (
f"<{self.__class__.__module__}.{self.__class__.__name__}"
f"({self.connection_pool!r})>"
)

def __await__(self):
return self.initialize().__await__()
Expand Down
6 changes: 3 additions & 3 deletions redis/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def _close(self):

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

@abstractmethod
def repr_pieces(self):
Expand Down Expand Up @@ -1061,8 +1061,8 @@ def __init__(

def __repr__(self):
return (
f"{self.__class__.__name__}"
f"<{self.connection_class(**self.connection_kwargs)!r}>"
f"<{self.__class__.__module__}.{self.__class__.__name__}"
f"({self.connection_class(**self.connection_kwargs)!r})>"
)

def reset(self):
Expand Down
16 changes: 11 additions & 5 deletions redis/asyncio/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ def __init__(self, **kwargs):

def __repr__(self):
pool = self.connection_pool
s = f"{self.__class__.__name__}<service={pool.service_name}"
s = (
f"<{self.__class__.__module__}.{self.__class__.__name__}"
f"(service={pool.service_name}"
)
if self.host:
host_info = f",host={self.host},port={self.port}"
s += host_info
return s + ">"
return s + ")>"

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

def __repr__(self):
return (
f"{self.__class__.__name__}"
f"<service={self.service_name}({self.is_master and 'master' or 'slave'})>"
f"<{self.__class__.__module__}.{self.__class__.__name__}"
f"(service={self.service_name}({self.is_master and 'master' or 'slave'}))>"
)

def reset(self):
Expand Down Expand Up @@ -241,7 +244,10 @@ def __repr__(self):
f"{sentinel.connection_pool.connection_kwargs['host']}:"
f"{sentinel.connection_pool.connection_kwargs['port']}"
)
return f"{self.__class__.__name__}<sentinels=[{','.join(sentinel_addresses)}]>"
return (
f"<{self.__class__}.{self.__class__.__name__}"
f"(sentinels=[{','.join(sentinel_addresses)}])>"
)

def check_master_state(self, state: dict, service_name: str) -> bool:
if not state["is_master"] or state["is_sdown"] or state["is_odown"]:
Expand Down
5 changes: 4 additions & 1 deletion redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ def __init__(
self.response_callbacks.update(_RedisCallbacksRESP2)

def __repr__(self) -> str:
return f"{type(self).__name__}<{repr(self.connection_pool)}>"
return (
f"<{type(self).__module__}.{type(self).__name__}"
f"({repr(self.connection_pool)})>"
)

def get_encoder(self) -> "Encoder":
"""Get the connection pool's encoder"""
Expand Down
6 changes: 3 additions & 3 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __init__(

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

@abstractmethod
def repr_pieces(self):
Expand Down Expand Up @@ -1026,8 +1026,8 @@ def __init__(

def __repr__(self) -> (str, str):
return (
f"{type(self).__name__}"
f"<{repr(self.connection_class(**self.connection_kwargs))}>"
f"<{type(self).__module__}.{type(self).__name__}"
f"({repr(self.connection_class(**self.connection_kwargs))})>"
)

def reset(self) -> None:
Expand Down
15 changes: 12 additions & 3 deletions redis/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def __init__(self, **kwargs):

def __repr__(self):
pool = self.connection_pool
s = f"{type(self).__name__}<service={pool.service_name}%s>"
s = (
f"<{type(self).__module__}.{type(self).__name__}"
f"(service={pool.service_name}%s)>"
)
if self.host:
host_info = f",host={self.host},port={self.port}"
s = s % host_info
Expand Down Expand Up @@ -162,7 +165,10 @@ def __init__(self, service_name, sentinel_manager, **kwargs):

def __repr__(self):
role = "master" if self.is_master else "slave"
return f"{type(self).__name__}<service={self.service_name}({role})"
return (
f"<{type(self).__module__}.{type(self).__name__}"
f"(service={self.service_name}({role}))>"
)

def reset(self):
super().reset()
Expand Down Expand Up @@ -261,7 +267,10 @@ def __repr__(self):
sentinel_addresses.append(
"{host}:{port}".format_map(sentinel.connection_pool.connection_kwargs)
)
return f'{type(self).__name__}<sentinels=[{",".join(sentinel_addresses)}]>'
return (
f"<{type(self).__module__}.{type(self).__name__}"
f'(sentinels=[{",".join(sentinel_addresses)}])>'
)

def check_master_state(self, state, service_name):
if not state["is_master"] or state["is_sdown"] or state["is_odown"]:
Expand Down
37 changes: 15 additions & 22 deletions tests/test_asyncio/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,17 @@ async def test_repr_contains_db_info_tcp(self):
async with self.get_pool(
connection_kwargs=connection_kwargs, connection_class=redis.Connection
) as pool:
expected = (
"ConnectionPool<Connection<"
"host=localhost,port=6379,db=1,client_name=test-client>>"
)
assert repr(pool) == expected
expected = "host=localhost,port=6379,db=1,client_name=test-client"
assert expected in repr(pool)

async def test_repr_contains_db_info_unix(self):
connection_kwargs = {"path": "/abc", "db": 1, "client_name": "test-client"}
async with self.get_pool(
connection_kwargs=connection_kwargs,
connection_class=redis.UnixDomainSocketConnection,
) as pool:
expected = (
"ConnectionPool<UnixDomainSocketConnection<"
"path=/abc,db=1,client_name=test-client>>"
)
assert repr(pool) == expected
expected = "path=/abc,db=1,client_name=test-client"
assert expected in repr(pool)


class TestBlockingConnectionPool:
Expand Down Expand Up @@ -293,23 +287,17 @@ def test_repr_contains_db_info_tcp(self):
pool = redis.ConnectionPool(
host="localhost", port=6379, client_name="test-client"
)
expected = (
"ConnectionPool<Connection<"
"host=localhost,port=6379,db=0,client_name=test-client>>"
)
assert repr(pool) == expected
expected = "host=localhost,port=6379,db=0,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",
client_name="test-client",
)
expected = (
"ConnectionPool<UnixDomainSocketConnection<"
"path=abc,db=0,client_name=test-client>>"
)
assert repr(pool) == expected
expected = "path=abc,db=0,client_name=test-client"
assert expected in repr(pool)


class TestConnectionPoolURLParsing:
Expand Down Expand Up @@ -659,7 +647,10 @@ def test_connect_from_url_tcp(self):
connection = redis.Redis.from_url("redis://localhost")
pool = connection.connection_pool

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

assert re.match("(.*)<(.*)<(.*)>>", repr(pool)).groups() == (
assert re.match(
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
).groups() == (
"ConnectionPool",
"UnixDomainSocketConnection",
"path=/path/to/socket,db=0",
Expand Down
36 changes: 14 additions & 22 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,17 @@ def test_repr_contains_db_info_tcp(self):
pool = self.get_pool(
connection_kwargs=connection_kwargs, connection_class=redis.Connection
)
expected = (
"ConnectionPool<Connection<"
"host=localhost,port=6379,db=1,client_name=test-client>>"
)
assert repr(pool) == expected
expected = "host=localhost,port=6379,db=1,client_name=test-client"
assert expected in repr(pool)

def test_repr_contains_db_info_unix(self):
connection_kwargs = {"path": "/abc", "db": 1, "client_name": "test-client"}
pool = self.get_pool(
connection_kwargs=connection_kwargs,
connection_class=redis.UnixDomainSocketConnection,
)
expected = (
"ConnectionPool<UnixDomainSocketConnection<"
"path=/abc,db=1,client_name=test-client>>"
)
assert repr(pool) == expected
expected = "path=/abc,db=1,client_name=test-client"
assert expected in repr(pool)


class TestBlockingConnectionPool:
Expand Down Expand Up @@ -190,23 +184,17 @@ def test_repr_contains_db_info_tcp(self):
pool = redis.ConnectionPool(
host="localhost", port=6379, client_name="test-client"
)
expected = (
"ConnectionPool<Connection<"
"host=localhost,port=6379,db=0,client_name=test-client>>"
)
assert repr(pool) == expected
expected = "host=localhost,port=6379,db=0,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",
client_name="test-client",
)
expected = (
"ConnectionPool<UnixDomainSocketConnection<"
"path=abc,db=0,client_name=test-client>>"
)
assert repr(pool) == expected
expected = "path=abc,db=0,client_name=test-client"
assert expected in repr(pool)


class TestConnectionPoolURLParsing:
Expand Down Expand Up @@ -579,7 +567,9 @@ def test_connect_from_url_tcp(self):
connection = redis.Redis.from_url("redis://localhost")
pool = connection.connection_pool

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

assert re.match("(.*)<(.*)<(.*)>>", repr(pool)).groups() == (
assert re.match(
r"< .*?([^\.]+) \( < .*?([^\.]+) \( (.+) \) > \) >", repr(pool), re.VERBOSE
).groups() == (
"ConnectionPool",
"UnixDomainSocketConnection",
"path=/path/to/socket,db=0",
Expand Down

0 comments on commit bc0461c

Please sign in to comment.