Skip to content

Commit 04fa392

Browse files
committed
Deprecate url in async Redis connection APIs in favor of redis_url; add tests; remove unused import in CLI
1 parent 627121d commit 04fa392

File tree

3 files changed

+109
-11
lines changed

3 files changed

+109
-11
lines changed

redisvl/cli/index.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from redisvl.redis.utils import convert_bytes, make_dict
99
from redisvl.schema.schema import IndexSchema
1010
from redisvl.utils.log import get_logger
11-
from redisvl.utils.utils import lazy_import
1211

1312
logger = get_logger("[RedisVL]")
1413

redisvl/redis/connection.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
from dataclasses import dataclass
32
from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar, Union, overload
43
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse
54
from warnings import warn
@@ -23,8 +22,11 @@
2322
)
2423
from redisvl.redis.utils import convert_bytes, is_cluster_url
2524
from redisvl.types import AsyncRedisClient, RedisClient, SyncRedisClient
25+
from redisvl.utils.log import get_logger
2626
from redisvl.utils.utils import deprecated_function
2727

28+
logger = get_logger(__name__)
29+
2830

2931
def _strip_cluster_from_url_and_kwargs(
3032
url: str, **kwargs
@@ -499,7 +501,6 @@ def get_redis_connection(
499501

500502
@staticmethod
501503
async def _get_aredis_connection(
502-
url: Optional[str] = None,
503504
redis_url: Optional[str] = None,
504505
**kwargs,
505506
) -> AsyncRedisClient:
@@ -509,9 +510,11 @@ async def _get_aredis_connection(
509510
only used internally by the library now.
510511
511512
Args:
512-
url (Optional[str]): The URL of the Redis server.
513-
redis_url (Optional[str]): Alias for url for consistency with public APIs.
514-
If neither is provided, the environment variable REDIS_URL is used.
513+
redis_url (Optional[str]): The URL of the Redis server. If neither
514+
`redis_url` nor `url` are provided, the environment variable
515+
REDIS_URL is used.
516+
url (Optional[str]): Former parameter for the URL of the Redis
517+
server. Use `redis_url` instead. (Deprecated)
515518
**kwargs: Additional keyword arguments to be passed to the async
516519
Redis client constructor.
517520
@@ -522,7 +525,13 @@ async def _get_aredis_connection(
522525
ValueError: If url is not provided and REDIS_URL environment
523526
variable is not set.
524527
"""
525-
url = url or redis_url or get_address_from_env()
528+
_deprecated_url = kwargs.pop("url", None)
529+
url = _deprecated_url or redis_url or get_address_from_env()
530+
531+
if _deprecated_url is not None:
532+
logger.warning(
533+
"The `url` parameter is deprecated. Please use `redis_url` instead."
534+
)
526535

527536
client: AsyncRedisClient
528537
if url.startswith("redis+sentinel"):
@@ -555,14 +564,17 @@ async def _get_aredis_connection(
555564

556565
@staticmethod
557566
def get_async_redis_connection(
558-
url: Optional[str] = None,
567+
redis_url: Optional[str] = None,
559568
**kwargs,
560569
) -> AsyncRedisClient:
561570
"""Creates and returns an asynchronous Redis client.
562571
563572
Args:
564-
url (Optional[str]): The URL of the Redis server. If not provided,
565-
the environment variable REDIS_URL is used.
573+
redis_url (Optional[str]): The URL of the Redis server. If neither
574+
`redis_url` nor `url` are provided, the environment variable
575+
REDIS_URL is used.
576+
url (Optional[str]): Former parameter for the URL of the Redis
577+
server. Use `redis_url` instead. (Deprecated)
566578
**kwargs: Additional keyword arguments to be passed to the async
567579
Redis client constructor.
568580
@@ -577,7 +589,12 @@ def get_async_redis_connection(
577589
"get_async_redis_connection will become async in the next major release.",
578590
DeprecationWarning,
579591
)
580-
url = url or get_address_from_env()
592+
_deprecated_url = kwargs.pop("url", None)
593+
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+
)
581598

582599
if url.startswith("redis+sentinel"):
583600
return RedisConnectionFactory._redis_sentinel_client(

tests/unit/test_url_deprecation.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import logging
2+
from unittest.mock import AsyncMock, MagicMock, patch
3+
4+
import pytest
5+
6+
from redisvl.redis.connection import RedisConnectionFactory
7+
8+
9+
class DummyAsyncClient:
10+
async def client_setinfo(self, *args, **kwargs):
11+
return None
12+
13+
async def echo(self, *args, **kwargs):
14+
return None
15+
16+
17+
@pytest.mark.asyncio
18+
async def test__get_aredis_connection_deprecates_url_kwarg_only(caplog):
19+
# Patch AsyncRedis.from_url to avoid real network calls
20+
with patch(
21+
"redisvl.redis.connection.AsyncRedis.from_url", return_value=DummyAsyncClient()
22+
):
23+
caplog.set_level(logging.WARNING, logger="redisvl.redis.connection")
24+
await RedisConnectionFactory._get_aredis_connection(
25+
url="redis://localhost:6379"
26+
)
27+
28+
assert (
29+
"The `url` parameter is deprecated. Please use `redis_url` instead."
30+
in caplog.text
31+
)
32+
33+
34+
@pytest.mark.asyncio
35+
async def test__get_aredis_connection_no_deprecation_with_redis_url(caplog):
36+
# Patch AsyncRedis.from_url to avoid real network calls
37+
with patch(
38+
"redisvl.redis.connection.AsyncRedis.from_url", return_value=DummyAsyncClient()
39+
):
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+
)
49+
50+
51+
def test_get_async_redis_connection_deprecates_url_kwarg_only(caplog):
52+
# Patch AsyncRedis.from_url to avoid real network calls
53+
with patch(
54+
"redisvl.redis.connection.AsyncRedis.from_url", return_value=MagicMock()
55+
):
56+
caplog.set_level(logging.WARNING, logger="redisvl.redis.connection")
57+
with pytest.warns(DeprecationWarning):
58+
RedisConnectionFactory.get_async_redis_connection(
59+
url="redis://localhost:6379"
60+
)
61+
62+
assert (
63+
"The `url` parameter is deprecated. Please use `redis_url` instead."
64+
in caplog.text
65+
)
66+
67+
68+
def test_get_async_redis_connection_no_deprecation_with_redis_url(caplog):
69+
# Patch AsyncRedis.from_url to avoid real network calls
70+
with patch(
71+
"redisvl.redis.connection.AsyncRedis.from_url", return_value=MagicMock()
72+
):
73+
caplog.set_level(logging.WARNING, logger="redisvl.redis.connection")
74+
with pytest.warns(DeprecationWarning):
75+
RedisConnectionFactory.get_async_redis_connection(
76+
redis_url="redis://localhost:6379"
77+
)
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)