diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2020-02-08 09:43:24 -0800 |
---|---|---|
committer | Andy McCurdy <andy@andymccurdy.com> | 2020-02-12 13:50:29 -0800 |
commit | 07e1c4b65d7f08486e7c7f39638abfedeccd6f39 (patch) | |
tree | cd72250c3f14957a9ec760770b39c360e80f5fc9 | |
parent | 3d8d523bef4d77d712117ece76b2b08c47b432f5 (diff) | |
download | redis-py-07e1c4b65d7f08486e7c7f39638abfedeccd6f39.tar.gz |
Improve Redis.hmset() warning message
It now describe what is deprecated and displays for the callers line by
using stacklevel=2.
The warning is now tested and not emitted during normal test runs.
Fixes #1282
-rwxr-xr-x | redis/client.py | 7 | ||||
-rw-r--r-- | tests/test_commands.py | 28 |
2 files changed, 21 insertions, 14 deletions
diff --git a/redis/client.py b/redis/client.py index 8ebcece..fe697f6 100755 --- a/redis/client.py +++ b/redis/client.py @@ -3026,7 +3026,12 @@ class Redis(object): Set key to value within hash ``name`` for each corresponding key and value from the ``mapping`` dict. """ - warnings.warn(DeprecationWarning('Use hset')) + warnings.warn( + '%s.hmset() is deprecated. Use %s.hset() instead.' + % (self.__class__.__name__, self.__class__.__name__), + DeprecationWarning, + stacklevel=2, + ) if not mapping: raise DataError("'hmset' with 'mapping' of length 0") items = [] diff --git a/tests/test_commands.py b/tests/test_commands.py index 15a76de..7f1e33e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -59,7 +59,6 @@ class TestResponseCallbacks(object): class TestRedisCommands(object): - def test_command_on_invalid_key_type(self, r): r.lpush('a', '1') with pytest.raises(redis.ResponseError): @@ -1102,7 +1101,7 @@ class TestRedisCommands(object): @skip_if_server_version_lt('2.8.0') def test_hscan(self, r): - r.hmset('a', {'a': 1, 'b': 2, 'c': 3}) + r.hset('a', mapping={'a': 1, 'b': 2, 'c': 3}) cursor, dic = r.hscan('a') assert cursor == 0 assert dic == {b'a': b'1', b'b': b'2', b'c': b'3'} @@ -1111,7 +1110,7 @@ class TestRedisCommands(object): @skip_if_server_version_lt('2.8.0') def test_hscan_iter(self, r): - r.hmset('a', {'a': 1, 'b': 2, 'c': 3}) + r.hset('a', mapping={'a': 1, 'b': 2, 'c': 3}) dic = dict(r.hscan_iter('a')) assert dic == {b'a': b'1', b'b': b'2', b'c': b'3'} dic = dict(r.hscan_iter('a', match='a')) @@ -1587,7 +1586,7 @@ class TestRedisCommands(object): # HASH COMMANDS def test_hget_and_hset(self, r): - r.hmset('a', mapping={'1': 1, '2': 2, '3': 3}) + r.hset('a', mapping={'1': 1, '2': 2, '3': 3}) assert r.hget('a', '1') == b'1' assert r.hget('a', '2') == b'2' assert r.hget('a', '3') == b'3' @@ -1619,20 +1618,20 @@ class TestRedisCommands(object): r.hset("x") def test_hdel(self, r): - r.hmset('a', {'1': 1, '2': 2, '3': 3}) + r.hset('a', mapping={'1': 1, '2': 2, '3': 3}) assert r.hdel('a', '2') == 1 assert r.hget('a', '2') is None assert r.hdel('a', '1', '3') == 2 assert r.hlen('a') == 0 def test_hexists(self, r): - r.hmset('a', {'1': 1, '2': 2, '3': 3}) + r.hset('a', mapping={'1': 1, '2': 2, '3': 3}) assert r.hexists('a', '1') assert not r.hexists('a', '4') def test_hgetall(self, r): h = {b'a1': b'1', b'a2': b'2', b'a3': b'3'} - r.hmset('a', h) + r.hset('a', mapping=h) assert r.hgetall('a') == h def test_hincrby(self, r): @@ -1648,22 +1647,25 @@ class TestRedisCommands(object): def test_hkeys(self, r): h = {b'a1': b'1', b'a2': b'2', b'a3': b'3'} - r.hmset('a', h) + r.hset('a', mapping=h) local_keys = list(iterkeys(h)) remote_keys = r.hkeys('a') assert (sorted(local_keys) == sorted(remote_keys)) def test_hlen(self, r): - r.hmset('a', {'1': 1, '2': 2, '3': 3}) + r.hset('a', mapping={'1': 1, '2': 2, '3': 3}) assert r.hlen('a') == 3 def test_hmget(self, r): - assert r.hmset('a', {'a': 1, 'b': 2, 'c': 3}) + assert r.hset('a', mapping={'a': 1, 'b': 2, 'c': 3}) assert r.hmget('a', 'a', 'b', 'c') == [b'1', b'2', b'3'] def test_hmset(self, r): + warning_message = (r'^Redis\.hmset\(\) is deprecated\. ' + r'Use Redis\.hset\(\) instead\.$') h = {b'a': b'1', b'b': b'2', b'c': b'3'} - assert r.hmset('a', h) + with pytest.warns(DeprecationWarning, match=warning_message): + assert r.hmset('a', h) assert r.hgetall('a') == h def test_hsetnx(self, r): @@ -1675,14 +1677,14 @@ class TestRedisCommands(object): def test_hvals(self, r): h = {b'a1': b'1', b'a2': b'2', b'a3': b'3'} - r.hmset('a', h) + r.hset('a', mapping=h) local_vals = list(itervalues(h)) remote_vals = r.hvals('a') assert sorted(local_vals) == sorted(remote_vals) @skip_if_server_version_lt('3.2.0') def test_hstrlen(self, r): - r.hmset('a', {'1': '22', '2': '333'}) + r.hset('a', mapping={'1': '22', '2': '333'}) assert r.hstrlen('a', '1') == 2 assert r.hstrlen('a', '2') == 3 |