Skip to content

Commit b34c890

Browse files
committed
Deprecate the "auto_close_connection_pool" argument to Redis() and Redis.from_url()
1 parent fb64a65 commit b34c890

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

redis/asyncio/client.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def from_url(
114114
cls,
115115
url: str,
116116
single_connection_client: bool = False,
117-
auto_close_connection_pool: bool = True,
117+
auto_close_connection_pool: Optional[bool] = None,
118118
**kwargs,
119119
):
120120
"""
@@ -164,12 +164,17 @@ class initializer. In the case of conflicting arguments, querystring
164164
connection_pool=connection_pool,
165165
single_connection_client=single_connection_client,
166166
)
167-
# We should probably deprecate the `auto_close_connection_pool`
168-
# argument.
169-
# If the caller doesn't want the pool auto-closed, a better
170-
# pattern is to create the pool manually (maybe using from_url()),
171-
# pass it in using the `connection_pool`, and hold on to it to close
172-
# it later.
167+
if auto_close_connection_pool is not None:
168+
warnings.warn(
169+
DeprecationWarning(
170+
'"auto_close_connection_pool" is deprecated '
171+
"since version 5.0.0. "
172+
"Please create a ConnectionPool explicitly and "
173+
"provide to the Redis() constructor instead."
174+
)
175+
)
176+
else:
177+
auto_close_connection_pool = True
173178
client.auto_close_connection_pool = auto_close_connection_pool
174179
return client
175180

@@ -223,7 +228,7 @@ def __init__(
223228
username: Optional[str] = None,
224229
retry: Optional[Retry] = None,
225230
# deprecated. create a pool and use connection_pool instead
226-
auto_close_connection_pool: bool = True,
231+
auto_close_connection_pool: Optional[bool] = None,
227232
redis_connect_func=None,
228233
credential_provider: Optional[CredentialProvider] = None,
229234
protocol: Optional[int] = 2,
@@ -237,6 +242,18 @@ def __init__(
237242
"""
238243
kwargs: Dict[str, Any]
239244

245+
if auto_close_connection_pool is not None:
246+
warnings.warn(
247+
DeprecationWarning(
248+
'"auto_close_connection_pool" is deprecated '
249+
"since version 5.0.0. "
250+
"Please create a ConnectionPool explicitly and "
251+
"provide to the Redis() constructor instead."
252+
)
253+
)
254+
else:
255+
auto_close_connection_pool = True
256+
240257
if not connection_pool:
241258
# Create internal connection pool, expected to be closed by Redis instance
242259
if not retry_on_error:

tests/test_asyncio/test_connection.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,23 @@ async def get_redis_connection():
305305
await r1.close()
306306

307307

308+
async def test_pool_from_url_deprecation(request):
309+
url: str = request.config.getoption("--redis-url")
310+
311+
with pytest.deprecated_call():
312+
return Redis.from_url(url, auto_close_connection_pool=False)
313+
314+
308315
async def test_pool_auto_close_disable(request):
309-
"""Verify that auto_close_connection_pool can be disabled"""
316+
"""Verify that auto_close_connection_pool can be disabled (deprecated)"""
310317

311318
url: str = request.config.getoption("--redis-url")
312319
url_args = parse_url(url)
313320

314321
async def get_redis_connection():
315322
url_args["auto_close_connection_pool"] = False
316-
return Redis(**url_args)
323+
with pytest.deprecated_call():
324+
return Redis(**url_args)
317325

318326
r1 = await get_redis_connection()
319327
assert r1.auto_close_connection_pool is False
@@ -394,7 +402,8 @@ async def test_redis_pool_auto_close_arg(request, auto_close):
394402
pool = ConnectionPool.from_url(url)
395403

396404
async def get_redis_connection():
397-
client = Redis(connection_pool=pool, auto_close_connection_pool=auto_close)
405+
with pytest.deprecated_call():
406+
client = Redis(connection_pool=pool, auto_close_connection_pool=auto_close)
398407
return client
399408

400409
called = 0

0 commit comments

Comments
 (0)