From 32963b9950503934ba646b5c48fc403cd91c36cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sun, 17 Sep 2023 11:18:21 +0000 Subject: [PATCH] Deprecate the "auto_close_connection_pool" argument to Redis() and Redis.from_url() --- redis/asyncio/client.py | 33 ++++++++++++++++++++------- tests/test_asyncio/test_connection.py | 16 ++++++++++--- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py index b28df10cac..cce1b5815d 100644 --- a/redis/asyncio/client.py +++ b/redis/asyncio/client.py @@ -114,7 +114,7 @@ def from_url( cls, url: str, single_connection_client: bool = False, - auto_close_connection_pool: bool = True, + auto_close_connection_pool: Optional[bool] = None, **kwargs, ): """ @@ -164,12 +164,17 @@ class initializer. In the case of conflicting arguments, querystring connection_pool=connection_pool, single_connection_client=single_connection_client, ) - # We should probably deprecate the `auto_close_connection_pool` - # argument. - # If the caller doesn't want the pool auto-closed, a better - # pattern is to create the pool manually (maybe using from_url()), - # pass it in using the `connection_pool`, and hold on to it to close - # it later. + if auto_close_connection_pool is not None: + warnings.warn( + DeprecationWarning( + '"auto_close_connection_pool" is deprecated ' + "since version 5.0.0. " + "Please create a ConnectionPool explicitly and " + "provide to the Redis() constructor instead." + ) + ) + else: + auto_close_connection_pool = True client.auto_close_connection_pool = auto_close_connection_pool return client @@ -223,7 +228,7 @@ def __init__( username: Optional[str] = None, retry: Optional[Retry] = None, # deprecated. create a pool and use connection_pool instead - auto_close_connection_pool: bool = True, + auto_close_connection_pool: Optional[bool] = None, redis_connect_func=None, credential_provider: Optional[CredentialProvider] = None, protocol: Optional[int] = 2, @@ -237,6 +242,18 @@ def __init__( """ kwargs: Dict[str, Any] + if auto_close_connection_pool is not None: + warnings.warn( + DeprecationWarning( + '"auto_close_connection_pool" is deprecated ' + "since version 5.0.0. " + "Please create a ConnectionPool explicitly and " + "provide to the Redis() constructor instead." + ) + ) + else: + auto_close_connection_pool = True + if not connection_pool: # Create internal connection pool, expected to be closed by Redis instance if not retry_on_error: diff --git a/tests/test_asyncio/test_connection.py b/tests/test_asyncio/test_connection.py index d904e2c1a3..1a64ee1a23 100644 --- a/tests/test_asyncio/test_connection.py +++ b/tests/test_asyncio/test_connection.py @@ -305,15 +305,24 @@ async def get_redis_connection(): await r1.close() +async def test_pool_from_url_deprecation(request): + url: str = request.config.getoption("--redis-url") + url_args = parse_url(url) + + with pytest.deprecated_call(): + return Redis.from_url(url, auto_close_connection_pool=False) + + async def test_pool_auto_close_disable(request): - """Verify that auto_close_connection_pool can be disabled""" + """Verify that auto_close_connection_pool can be disabled (deprecated)""" url: str = request.config.getoption("--redis-url") url_args = parse_url(url) async def get_redis_connection(): url_args["auto_close_connection_pool"] = False - return Redis(**url_args) + with pytest.deprecated_call(): + return Redis(**url_args) r1 = await get_redis_connection() assert r1.auto_close_connection_pool is False @@ -394,7 +403,8 @@ async def test_redis_pool_auto_close_arg(request, auto_close): pool = ConnectionPool.from_url(url) async def get_redis_connection(): - client = Redis(connection_pool=pool, auto_close_connection_pool=auto_close) + with pytest.deprecated_call(): + client = Redis(connection_pool=pool, auto_close_connection_pool=auto_close) return client called = 0