From 50245cdc6fecd4f7432e821816c79de23666fe63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Mon, 11 Dec 2023 00:44:29 +0000 Subject: [PATCH] =?UTF-8?q?Update=20repr=20of=20important=20classes=20with?= =?UTF-8?q?=20module=20name=20and=20recommended=20"<=20=E2=80=A6=20(#3001)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update repr of important classes with module name and recommended "< ... >" syntax. * update tests which examine repr * formatting --- redis/asyncio/client.py | 5 ++- redis/asyncio/connection.py | 6 ++-- redis/asyncio/sentinel.py | 16 +++++++--- redis/client.py | 5 ++- redis/connection.py | 6 ++-- redis/sentinel.py | 15 +++++++-- tests/test_asyncio/test_connection_pool.py | 37 +++++++++------------- tests/test_connection_pool.py | 36 ++++++++------------- 8 files changed, 66 insertions(+), 60 deletions(-) diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py index 8a8f54dc9c..9724a3dd54 100644 --- a/redis/asyncio/client.py +++ b/redis/asyncio/client.py @@ -337,7 +337,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__() diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index 77312211a9..739cd7f64e 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -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): @@ -1031,8 +1031,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): diff --git a/redis/asyncio/sentinel.py b/redis/asyncio/sentinel.py index 56f5e9d651..d88babc59c 100644 --- a/redis/asyncio/sentinel.py +++ b/redis/asyncio/sentinel.py @@ -30,11 +30,14 @@ def __init__(self, **kwargs): def __repr__(self): pool = self.connection_pool - s = f"{self.__class__.__name__}" + return s + ")>" async def connect_to(self, address): self.host, self.port = address @@ -120,8 +123,8 @@ def __init__(self, service_name, sentinel_manager, **kwargs): def __repr__(self): return ( - f"{self.__class__.__name__}" - f"" + f"<{self.__class__.__module__}.{self.__class__.__name__}" + f"(service={self.service_name}({self.is_master and 'master' or 'slave'}))>" ) def reset(self): @@ -242,7 +245,10 @@ def __repr__(self): f"{sentinel.connection_pool.connection_kwargs['host']}:" f"{sentinel.connection_pool.connection_kwargs['port']}" ) - return f"{self.__class__.__name__}" + 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"]: diff --git a/redis/client.py b/redis/client.py index 2ff3706d16..53748afbcc 100755 --- a/redis/client.py +++ b/redis/client.py @@ -334,7 +334,10 @@ def __init__( self.cache_whitelist = cache_whitelist 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""" diff --git a/redis/connection.py b/redis/connection.py index fead6135e0..8129bf03a4 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -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): @@ -1004,8 +1004,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: diff --git a/redis/sentinel.py b/redis/sentinel.py index a1ae5c5275..dfcd8ff64b 100644 --- a/redis/sentinel.py +++ b/redis/sentinel.py @@ -24,7 +24,10 @@ def __init__(self, **kwargs): def __repr__(self): pool = self.connection_pool - s = f"{type(self).__name__}" + 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 @@ -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__}" + ) def reset(self): super().reset() @@ -262,7 +268,10 @@ def __repr__(self): sentinel_addresses.append( "{host}:{port}".format_map(sentinel.connection_pool.connection_kwargs) ) - return f'{type(self).__name__}' + 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"]: diff --git a/tests/test_asyncio/test_connection_pool.py b/tests/test_asyncio/test_connection_pool.py index ed90fc73fc..5e4d3f206f 100644 --- a/tests/test_asyncio/test_connection_pool.py +++ b/tests/test_asyncio/test_connection_pool.py @@ -180,11 +180,8 @@ 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>" - ) - 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"} @@ -192,11 +189,8 @@ async def test_repr_contains_db_info_unix(self): connection_kwargs=connection_kwargs, connection_class=redis.UnixDomainSocketConnection, ) as pool: - expected = ( - "ConnectionPool>" - ) - assert repr(pool) == expected + expected = "path=/abc,db=1,client_name=test-client" + assert expected in repr(pool) class TestBlockingConnectionPool: @@ -293,11 +287,8 @@ def test_repr_contains_db_info_tcp(self): pool = redis.ConnectionPool( host="localhost", port=6379, client_name="test-client" ) - expected = ( - "ConnectionPool>" - ) - 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( @@ -305,11 +296,8 @@ def test_repr_contains_db_info_unix(self): path="abc", client_name="test-client", ) - expected = ( - "ConnectionPool>" - ) - assert repr(pool) == expected + expected = "path=abc,db=0,client_name=test-client" + assert expected in repr(pool) class TestConnectionPoolURLParsing: @@ -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", @@ -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", diff --git a/tests/test_connection_pool.py b/tests/test_connection_pool.py index d1e984ee9c..dee7c554d3 100644 --- a/tests/test_connection_pool.py +++ b/tests/test_connection_pool.py @@ -95,11 +95,8 @@ def test_repr_contains_db_info_tcp(self): pool = self.get_pool( connection_kwargs=connection_kwargs, connection_class=redis.Connection ) - expected = ( - "ConnectionPool>" - ) - 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"} @@ -107,11 +104,8 @@ def test_repr_contains_db_info_unix(self): connection_kwargs=connection_kwargs, connection_class=redis.UnixDomainSocketConnection, ) - expected = ( - "ConnectionPool>" - ) - assert repr(pool) == expected + expected = "path=/abc,db=1,client_name=test-client" + assert expected in repr(pool) class TestBlockingConnectionPool: @@ -190,11 +184,8 @@ def test_repr_contains_db_info_tcp(self): pool = redis.ConnectionPool( host="localhost", port=6379, client_name="test-client" ) - expected = ( - "ConnectionPool>" - ) - 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( @@ -202,11 +193,8 @@ def test_repr_contains_db_info_unix(self): path="abc", client_name="test-client", ) - expected = ( - "ConnectionPool>" - ) - assert repr(pool) == expected + expected = "path=abc,db=0,client_name=test-client" + assert expected in repr(pool) class TestConnectionPoolURLParsing: @@ -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", @@ -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",