Skip to content

Commit aaddeb6

Browse files
authored
Add support for LMPOP (#1843)
* Add support for LMPOP * add type hints * fix linters
1 parent b541da6 commit aaddeb6

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

redis/commands/core.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,25 @@ def brpoplpush(self, src, dst, timeout=0):
19171917
timeout = 0
19181918
return self.execute_command("BRPOPLPUSH", src, dst, timeout)
19191919

1920+
def lmpop(
1921+
self,
1922+
num_keys: int,
1923+
*args: List[str],
1924+
direction: str = None,
1925+
count: Optional[int] = 1,
1926+
) -> List:
1927+
"""
1928+
Pop ``count`` values (default 1) first non-empty list key from the list
1929+
of args provided key names.
1930+
1931+
For more information check https://redis.io/commands/lmpop
1932+
"""
1933+
args = [num_keys] + list(args) + [direction]
1934+
if count != 1:
1935+
args.extend(["COUNT", count])
1936+
1937+
return self.execute_command("LMPOP", *args)
1938+
19201939
def lindex(self, name, index):
19211940
"""
19221941
Return the item from list ``name`` at position ``index``

tests/test_commands.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,17 @@ def test_brpoplpush_empty_string(self, r):
14801480
r.rpush("a", "")
14811481
assert r.brpoplpush("a", "b") == b""
14821482

1483+
@pytest.mark.onlynoncluster
1484+
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
1485+
def test_lmpop(self, unstable_r):
1486+
unstable_r.rpush("foo", "1", "2", "3", "4", "5")
1487+
result = [b"foo", [b"1", b"2"]]
1488+
assert unstable_r.lmpop("2", "bar", "foo", direction="LEFT", count=2) == result
1489+
with pytest.raises(redis.ResponseError):
1490+
unstable_r.lmpop("2", "bar", "foo", direction="up", count=2)
1491+
unstable_r.rpush("bar", "a", "b", "c", "d")
1492+
assert unstable_r.lmpop("2", "bar", "foo", direction="LEFT") == [b"bar", [b"a"]]
1493+
14831494
def test_lindex(self, r):
14841495
r.rpush("a", "1", "2", "3")
14851496
assert r.lindex("a", "0") == b"1"

0 commit comments

Comments
 (0)