From 71270edeced76ab126b2932bf754417c037ddd9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Thu, 12 Oct 2023 15:10:40 +0000 Subject: [PATCH 1/3] Update repr of important classes with module name and recommended "< ... >" syntax. --- redis/asyncio/client.py | 2 +- redis/asyncio/connection.py | 6 +++--- redis/asyncio/sentinel.py | 10 +++++----- redis/client.py | 2 +- redis/connection.py | 6 +++--- redis/sentinel.py | 6 +++--- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py index e4d2e776bc..b5bf13a744 100644 --- a/redis/asyncio/client.py +++ b/redis/asyncio/client.py @@ -337,7 +337,7 @@ 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__}({self.connection_pool!r})>" def __await__(self): return self.initialize().__await__() diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index 1ef9960ff3..d11180ff98 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -206,7 +206,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): @@ -1011,8 +1011,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 6834fb194f..6856c9f8c0 100644 --- a/redis/asyncio/sentinel.py +++ b/redis/asyncio/sentinel.py @@ -30,11 +30,11 @@ 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 +120,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): @@ -241,7 +241,7 @@ 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__}(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 b526d7787b..547fc7feac 100755 --- a/redis/client.py +++ b/redis/client.py @@ -311,7 +311,7 @@ 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__}({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 b39ba28f76..14fa241edb 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): @@ -1003,8 +1003,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 41f308d1ee..7b193e83d8 100644 --- a/redis/sentinel.py +++ b/redis/sentinel.py @@ -24,7 +24,7 @@ def __init__(self, **kwargs): def __repr__(self): pool = self.connection_pool - s = f"{type(self).__name__}" + s = f"<{type(self).__module__}.{type(self).__name__}(service={pool.service_name}%s)>" if self.host: host_info = f",host={self.host},port={self.port}" s = s % host_info @@ -162,7 +162,7 @@ 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() @@ -261,7 +261,7 @@ 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__}(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"]: From d24bbd69d68f9af9c67dc45e9c6c65cb045b20d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Thu, 12 Oct 2023 15:13:55 +0000 Subject: [PATCH 2/3] update tests which examine repr --- tests/test_asyncio/test_connection_pool.py | 37 +++++++++------------- tests/test_connection_pool.py | 36 ++++++++------------- 2 files changed, 29 insertions(+), 44 deletions(-) diff --git a/tests/test_asyncio/test_connection_pool.py b/tests/test_asyncio/test_connection_pool.py index c93fa91a39..10a1a72157 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: @@ -634,7 +622,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", @@ -644,7 +635,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 ef70a8ff35..8f725787cd 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: @@ -554,7 +542,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", @@ -564,7 +554,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", From d2d7304f5e8bc9523750a900f0c670ec35e7067a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Thu, 12 Oct 2023 15:48:46 +0000 Subject: [PATCH 3/3] formatting --- redis/asyncio/client.py | 5 ++++- redis/asyncio/sentinel.py | 10 ++++++++-- redis/client.py | 5 ++++- redis/sentinel.py | 15 ++++++++++++--- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py index b5bf13a744..1b181e9573 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__.__module__}.{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/sentinel.py b/redis/asyncio/sentinel.py index 6856c9f8c0..fbd7c4c25d 100644 --- a/redis/asyncio/sentinel.py +++ b/redis/asyncio/sentinel.py @@ -30,7 +30,10 @@ def __init__(self, **kwargs): def __repr__(self): pool = self.connection_pool - s = f"<{self.__class__.__module__}.{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 @@ -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__}.{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"]: diff --git a/redis/client.py b/redis/client.py index 547fc7feac..4c104cfc1e 100755 --- a/redis/client.py +++ b/redis/client.py @@ -311,7 +311,10 @@ def __init__( self.response_callbacks.update(_RedisCallbacksRESP2) def __repr__(self) -> str: - return f"<{type(self).__module__}.{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/sentinel.py b/redis/sentinel.py index 7b193e83d8..bc56fabdf3 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).__module__}.{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 @@ -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).__module__}.{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() @@ -261,7 +267,10 @@ def __repr__(self): sentinel_addresses.append( "{host}:{port}".format_map(sentinel.connection_pool.connection_kwargs) ) - return f'<{type(self).__module__}.{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"]: