Skip to content

Commit 459ff39

Browse files
committed
Best for a library to avoid logging deprecation warnings
1 parent 3566461 commit 459ff39

File tree

2 files changed

+23
-42
lines changed

2 files changed

+23
-42
lines changed

redisvl/redis/connection.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from redisvl.redis.utils import convert_bytes, is_cluster_url
2424
from redisvl.types import AsyncRedisClient, RedisClient, SyncRedisClient
2525
from redisvl.utils.log import get_logger
26-
from redisvl.utils.utils import deprecated_function
26+
from redisvl.utils.utils import deprecated_argument, deprecated_function
2727

2828
logger = get_logger(__name__)
2929

@@ -500,6 +500,7 @@ def get_redis_connection(
500500
return client
501501

502502
@staticmethod
503+
@deprecated_argument("url", "redis_url")
503504
async def _get_aredis_connection(
504505
redis_url: Optional[str] = None,
505506
**kwargs,
@@ -528,11 +529,6 @@ async def _get_aredis_connection(
528529
_deprecated_url = kwargs.pop("url", None)
529530
url = _deprecated_url or redis_url or get_address_from_env()
530531

531-
if _deprecated_url is not None:
532-
logger.warning(
533-
"The `url` parameter is deprecated. Please use `redis_url` instead."
534-
)
535-
536532
client: AsyncRedisClient
537533
if url.startswith("redis+sentinel"):
538534
client = RedisConnectionFactory._redis_sentinel_client(
@@ -563,6 +559,7 @@ async def _get_aredis_connection(
563559
return client
564560

565561
@staticmethod
562+
@deprecated_argument("url", "redis_url")
566563
def get_async_redis_connection(
567564
redis_url: Optional[str] = None,
568565
**kwargs,
@@ -591,10 +588,6 @@ def get_async_redis_connection(
591588
)
592589
_deprecated_url = kwargs.pop("url", None)
593590
url = _deprecated_url or redis_url or get_address_from_env()
594-
if _deprecated_url is not None:
595-
logger.warning(
596-
"The `url` parameter is deprecated. Please use `redis_url` instead."
597-
)
598591

599592
if url.startswith("redis+sentinel"):
600593
return RedisConnectionFactory._redis_sentinel_client(

tests/unit/test_url_deprecation.py

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from redisvl.redis.connection import RedisConnectionFactory
7+
from redisvl.utils.utils import assert_no_warnings
78

89

910
class DummyAsyncClient:
@@ -15,68 +16,55 @@ async def echo(self, *args, **kwargs):
1516

1617

1718
@pytest.mark.asyncio
18-
async def test__get_aredis_connection_deprecates_url_kwarg_only(caplog):
19+
async def test__get_aredis_connection_deprecates_url_kwarg_only():
1920
# Patch AsyncRedis.from_url to avoid real network calls
2021
with patch(
2122
"redisvl.redis.connection.AsyncRedis.from_url", return_value=DummyAsyncClient()
2223
):
23-
caplog.set_level(logging.WARNING, logger="redisvl.redis.connection")
24-
await RedisConnectionFactory._get_aredis_connection(
25-
url="redis://localhost:6379"
26-
)
24+
with pytest.warns(DeprecationWarning) as record:
25+
await RedisConnectionFactory._get_aredis_connection(
26+
url="redis://localhost:6379"
27+
)
2728

28-
assert (
29-
"The `url` parameter is deprecated. Please use `redis_url` instead."
30-
in caplog.text
29+
assert any(
30+
str(w.message)
31+
== (
32+
"Argument url is deprecated and will be removed in the next major release. "
33+
"Use redis_url instead."
34+
)
35+
for w in record
3136
)
3237

3338

3439
@pytest.mark.asyncio
35-
async def test__get_aredis_connection_no_deprecation_with_redis_url(caplog):
40+
async def test__get_aredis_connection_no_deprecation_with_redis_url():
3641
# Patch AsyncRedis.from_url to avoid real network calls
3742
with patch(
3843
"redisvl.redis.connection.AsyncRedis.from_url", return_value=DummyAsyncClient()
3944
):
40-
caplog.set_level(logging.WARNING, logger="redisvl.redis.connection")
41-
await RedisConnectionFactory._get_aredis_connection(
42-
redis_url="redis://localhost:6379"
43-
)
44-
45-
assert (
46-
"The `url` parameter is deprecated. Please use `redis_url` instead."
47-
not in caplog.text
48-
)
45+
with assert_no_warnings():
46+
await RedisConnectionFactory._get_aredis_connection(
47+
redis_url="redis://localhost:6379"
48+
)
4949

5050

51-
def test_get_async_redis_connection_deprecates_url_kwarg_only(caplog):
51+
def test_get_async_redis_connection_deprecates_url_kwarg_only():
5252
# Patch AsyncRedis.from_url to avoid real network calls
5353
with patch(
5454
"redisvl.redis.connection.AsyncRedis.from_url", return_value=MagicMock()
5555
):
56-
caplog.set_level(logging.WARNING, logger="redisvl.redis.connection")
5756
with pytest.warns(DeprecationWarning):
5857
RedisConnectionFactory.get_async_redis_connection(
5958
url="redis://localhost:6379"
6059
)
6160

62-
assert (
63-
"The `url` parameter is deprecated. Please use `redis_url` instead."
64-
in caplog.text
65-
)
66-
6761

68-
def test_get_async_redis_connection_no_deprecation_with_redis_url(caplog):
62+
def test_get_async_redis_connection_no_deprecation_with_redis_url():
6963
# Patch AsyncRedis.from_url to avoid real network calls
7064
with patch(
7165
"redisvl.redis.connection.AsyncRedis.from_url", return_value=MagicMock()
7266
):
73-
caplog.set_level(logging.WARNING, logger="redisvl.redis.connection")
7467
with pytest.warns(DeprecationWarning):
7568
RedisConnectionFactory.get_async_redis_connection(
7669
redis_url="redis://localhost:6379"
7770
)
78-
79-
assert (
80-
"The `url` parameter is deprecated. Please use `redis_url` instead."
81-
not in caplog.text
82-
)

0 commit comments

Comments
 (0)