From 842634e7fddeb32ba20aab0dacf557a958a4b00b Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sun, 30 Oct 2022 13:31:03 +0200 Subject: [PATCH] Remove `deprecated` dependency (#2386) No need for an external library just for 5 annotations. --- CHANGES | 1 + redis/commands/bf/commands.py | 6 ++---- redis/commands/json/commands.py | 11 +++++------ redis/commands/search/commands.py | 7 +++---- redis/utils.py | 28 ++++++++++++++++++++++++++++ requirements.txt | 1 - setup.py | 1 - 7 files changed, 39 insertions(+), 16 deletions(-) diff --git a/CHANGES b/CHANGES index 2ced3d8f96..f5c267bdda 100644 --- a/CHANGES +++ b/CHANGES @@ -22,6 +22,7 @@ * Fix RedisCluster to immediately raise AuthenticationError without a retry * ClusterPipeline Doesn't Handle ConnectionError for Dead Hosts (#2225) * Remove compatibility code for old versions of Hiredis, drop Packaging dependency + * The `deprecated` library is no longer a dependency * 4.1.3 (Feb 8, 2022) * Fix flushdb and flushall (#1926) diff --git a/redis/commands/bf/commands.py b/redis/commands/bf/commands.py index 60409fd232..f6bf281639 100644 --- a/redis/commands/bf/commands.py +++ b/redis/commands/bf/commands.py @@ -1,8 +1,6 @@ -from deprecated import deprecated - from redis.client import NEVER_DECODE from redis.exceptions import ModuleError -from redis.utils import HIREDIS_AVAILABLE +from redis.utils import HIREDIS_AVAILABLE, deprecated_function BF_RESERVE = "BF.RESERVE" BF_ADD = "BF.ADD" @@ -327,7 +325,7 @@ def query(self, key, *items): """ # noqa return self.execute_command(TOPK_QUERY, key, *items) - @deprecated(version="4.4.0", reason="deprecated since redisbloom 2.4.0") + @deprecated_function(version="4.4.0", reason="deprecated since redisbloom 2.4.0") def count(self, key, *items): """ Return count for one `item` or more from `key`. diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py index 9391c2a2c1..7fd4039203 100644 --- a/redis/commands/json/commands.py +++ b/redis/commands/json/commands.py @@ -2,9 +2,8 @@ from json import JSONDecodeError, loads from typing import Dict, List, Optional, Union -from deprecated import deprecated - from redis.exceptions import DataError +from redis.utils import deprecated_function from ._util import JsonType from .decoders import decode_dict_keys @@ -137,7 +136,7 @@ def numincrby(self, name: str, path: str, number: int) -> str: "JSON.NUMINCRBY", name, str(path), self._encode(number) ) - @deprecated(version="4.0.0", reason="deprecated since redisjson 1.0.0") + @deprecated_function(version="4.0.0", reason="deprecated since redisjson 1.0.0") def nummultby(self, name: str, path: str, number: int) -> str: """Multiply the numeric (integer or floating point) JSON value under ``path`` at key ``name`` with the provided ``number``. @@ -368,19 +367,19 @@ def debug( pieces.append(str(path)) return self.execute_command("JSON.DEBUG", *pieces) - @deprecated( + @deprecated_function( version="4.0.0", reason="redisjson-py supported this, call get directly." ) def jsonget(self, *args, **kwargs): return self.get(*args, **kwargs) - @deprecated( + @deprecated_function( version="4.0.0", reason="redisjson-py supported this, call get directly." ) def jsonmget(self, *args, **kwargs): return self.mget(*args, **kwargs) - @deprecated( + @deprecated_function( version="4.0.0", reason="redisjson-py supported this, call get directly." ) def jsonset(self, *args, **kwargs): diff --git a/redis/commands/search/commands.py b/redis/commands/search/commands.py index ceca20f51d..f02805ee2b 100644 --- a/redis/commands/search/commands.py +++ b/redis/commands/search/commands.py @@ -2,9 +2,8 @@ import time from typing import Dict, Optional, Union -from deprecated import deprecated - from redis.client import Pipeline +from redis.utils import deprecated_function from ..helpers import parse_to_dict from ._util import to_string @@ -238,7 +237,7 @@ def _add_document_hash( return self.execute_command(*args) - @deprecated( + @deprecated_function( version="2.0.0", reason="deprecated since redisearch 2.0, call hset instead" ) def add_document( @@ -294,7 +293,7 @@ def add_document( **fields, ) - @deprecated( + @deprecated_function( version="2.0.0", reason="deprecated since redisearch 2.0, call hset instead" ) def add_document_hash(self, doc_id, score=1.0, language=None, replace=False): diff --git a/redis/utils.py b/redis/utils.py index 25d2491dfe..693d4e64b5 100644 --- a/redis/utils.py +++ b/redis/utils.py @@ -1,4 +1,5 @@ from contextlib import contextmanager +from functools import wraps from typing import Any, Dict, Mapping, Union try: @@ -80,3 +81,30 @@ def merge_result(command, res): result.add(value) return list(result) + + +def warn_deprecated(name, reason="", version="", stacklevel=2): + import warnings + + msg = f"Call to deprecated {name}." + if reason: + msg += f" ({reason})" + if version: + msg += f" -- Deprecated since version {version}." + warnings.warn(msg, category=DeprecationWarning, stacklevel=stacklevel) + + +def deprecated_function(reason="", version="", name=None): + """ + Decorator to mark a function as deprecated. + """ + + def decorator(func): + @wraps(func) + def wrapper(*args, **kwargs): + warn_deprecated(name or func.__name__, reason, version, stacklevel=3) + return func(*args, **kwargs) + + return wrapper + + return decorator diff --git a/requirements.txt b/requirements.txt index b764668e7a..82c46c92c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ async-timeout>=4.0.2 -deprecated>=1.2.3 typing-extensions; python_version<"3.8" diff --git a/setup.py b/setup.py index db2db93b79..befa9dc131 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,6 @@ author_email="oss@redis.com", python_requires=">=3.7", install_requires=[ - "deprecated>=1.2.3", 'importlib-metadata >= 1.0; python_version < "3.8"', 'typing-extensions; python_version<"3.8"', "async-timeout>=4.0.2",