Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints for list commands #1917

Merged
merged 6 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 43 additions & 30 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hashlib
import time
import warnings
from typing import List, Optional, Union

from redis.exceptions import ConnectionError, DataError, NoScriptError, RedisError

Expand Down Expand Up @@ -1069,6 +1070,11 @@ def wait(self, num_replicas, timeout, **kwargs):
"""
return self.execute_command("WAIT", num_replicas, timeout, **kwargs)

def hello(self):
chayim marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError(
"HELLO is intentionally not implemented in the client."
)


class BasicKeyCommands:
"""
Expand Down Expand Up @@ -1855,7 +1861,7 @@ class ListCommands:
see: https://redis.io/topics/data-types#lists
"""

def blpop(self, keys, timeout=0):
def blpop(self, keys: list, timeout: float) -> list:
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
"""
LPOP a value off of the first non-empty list
named in the ``keys`` list.
Expand All @@ -1874,7 +1880,7 @@ def blpop(self, keys, timeout=0):
keys.append(timeout)
return self.execute_command("BLPOP", *keys)

def brpop(self, keys, timeout=0):
def brpop(self, keys: list, timeout: float) -> list:
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
"""
RPOP a value off of the first non-empty list
named in the ``keys`` list.
Expand All @@ -1893,7 +1899,7 @@ def brpop(self, keys, timeout=0):
keys.append(timeout)
return self.execute_command("BRPOP", *keys)

def brpoplpush(self, src, dst, timeout=0):
def brpoplpush(self, src: str, dst: str, timeout: float) -> Optional[str]:
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
"""
Pop a value off the tail of ``src``, push it on the head of ``dst``
and then return it.
Expand All @@ -1908,7 +1914,7 @@ def brpoplpush(self, src, dst, timeout=0):
timeout = 0
return self.execute_command("BRPOPLPUSH", src, dst, timeout)

def lindex(self, name, index):
def lindex(self, name: str, index: int) -> Optional[str]:
"""
Return the item from list ``name`` at position ``index``

Expand All @@ -1919,7 +1925,7 @@ def lindex(self, name, index):
"""
return self.execute_command("LINDEX", name, index)

def linsert(self, name, where, refvalue, value):
def linsert(self, name: str, where: str, refvalue: str, value: str) -> int:
"""
Insert ``value`` in list ``name`` either immediately before or after
[``where``] ``refvalue``
Expand All @@ -1931,15 +1937,15 @@ def linsert(self, name, where, refvalue, value):
"""
return self.execute_command("LINSERT", name, where, refvalue, value)

def llen(self, name):
def llen(self, name: str) -> int:
"""
Return the length of the list ``name``

For more information check https://redis.io/commands/llen
"""
return self.execute_command("LLEN", name)

def lpop(self, name, count=None):
def lpop(self, name: str, count: Optional[int] = None) -> Union[str, list, None]:
"""
Removes and returns the first elements of the list ``name``.

Expand All @@ -1954,23 +1960,23 @@ def lpop(self, name, count=None):
else:
return self.execute_command("LPOP", name)

def lpush(self, name, *values):
def lpush(self, name: str, *values: list) -> int:
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
"""
Push ``values`` onto the head of the list ``name``

For more information check https://redis.io/commands/lpush
"""
return self.execute_command("LPUSH", name, *values)

def lpushx(self, name, *values):
def lpushx(self, name: str, *values: list) -> int:
"""
Push ``value`` onto the head of the list ``name`` if ``name`` exists
dvora-h marked this conversation as resolved.
Show resolved Hide resolved

For more information check https://redis.io/commands/lpushx
"""
return self.execute_command("LPUSHX", name, *values)

def lrange(self, name, start, end):
def lrange(self, name: str, start: int, end: int) -> list:
"""
Return a slice of the list ``name`` between
position ``start`` and ``end``
Expand All @@ -1982,7 +1988,7 @@ def lrange(self, name, start, end):
"""
return self.execute_command("LRANGE", name, start, end)

def lrem(self, name, count, value):
def lrem(self, name: str, count: int, value: str) -> int:
"""
Remove the first ``count`` occurrences of elements equal to ``value``
from the list stored at ``name``.
Expand All @@ -1996,15 +2002,15 @@ def lrem(self, name, count, value):
"""
return self.execute_command("LREM", name, count, value)

def lset(self, name, index, value):
def lset(self, name: str, index: int, value: str) -> str:
"""
Set ``position`` of list ``name`` to ``value``
Set element at ``index`` of list ``name`` to ``value``

For more information check https://redis.io/commands/lset
"""
return self.execute_command("LSET", name, index, value)

def ltrim(self, name, start, end):
def ltrim(self, name: str, start: int, end: int) -> str:
"""
Trim the list ``name``, removing all values not within the slice
between ``start`` and ``end``
Expand All @@ -2016,7 +2022,7 @@ def ltrim(self, name, start, end):
"""
return self.execute_command("LTRIM", name, start, end)

def rpop(self, name, count=None):
def rpop(self, name: str, count: Optional[int] = None) -> Union[str, list, None]:
"""
Removes and returns the last elements of the list ``name``.

Expand All @@ -2031,7 +2037,7 @@ def rpop(self, name, count=None):
else:
return self.execute_command("RPOP", name)

def rpoplpush(self, src, dst):
def rpoplpush(self, src: str, dst: str) -> str:
"""
RPOP a value off of the ``src`` list and atomically LPUSH it
on to the ``dst`` list. Returns the value.
Expand All @@ -2040,23 +2046,30 @@ def rpoplpush(self, src, dst):
"""
return self.execute_command("RPOPLPUSH", src, dst)

def rpush(self, name, *values):
def rpush(self, name: str, *values: list) -> int:
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
"""
Push ``values`` onto the tail of the list ``name``

For more information check https://redis.io/commands/rpush
"""
return self.execute_command("RPUSH", name, *values)

def rpushx(self, name, value):
def rpushx(self, name: str, *values: list) -> int:
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
"""
Push ``value`` onto the tail of the list ``name`` if ``name`` exists

For more information check https://redis.io/commands/rpushx
"""
return self.execute_command("RPUSHX", name, value)
return self.execute_command("RPUSHX", name, *values)

def lpos(self, name, value, rank=None, count=None, maxlen=None):
def lpos(
self,
name: str,
value: str,
rank: Optional[int] = None,
count: Optional[int] = None,
maxlen: Optional[int] = None,
) -> Union[str, list, None]:
"""
Get position of ``value`` within the list ``name``

Expand Down Expand Up @@ -2096,16 +2109,16 @@ def lpos(self, name, value, rank=None, count=None, maxlen=None):

def sort(
self,
name,
start=None,
num=None,
by=None,
get=None,
desc=False,
alpha=False,
store=None,
groups=False,
):
name: str,
start: Optional[int] = None,
num: Optional[int] = None,
by: Optional[str] = None,
get: Optional[List[str]] = None,
desc: bool = False,
alpha: bool = False,
store: Optional[str] = None,
groups: Optional[bool] = False,
) -> Union[list, int]:
"""
Sort and return the list, set or sorted set at ``name``.

Expand Down
6 changes: 3 additions & 3 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,15 +1359,15 @@ def test_cluster_brpop(self, r):
def test_cluster_brpoplpush(self, r):
r.rpush("{foo}a", "1", "2")
r.rpush("{foo}b", "3", "4")
assert r.brpoplpush("{foo}a", "{foo}b") == b"2"
assert r.brpoplpush("{foo}a", "{foo}b") == b"1"
assert r.brpoplpush("{foo}a", "{foo}b", timeout=0) == b"2"
assert r.brpoplpush("{foo}a", "{foo}b", timeout=0) == b"1"
assert r.brpoplpush("{foo}a", "{foo}b", timeout=1) is None
assert r.lrange("{foo}a", 0, -1) == []
assert r.lrange("{foo}b", 0, -1) == [b"1", b"2", b"3", b"4"]

def test_cluster_brpoplpush_empty_string(self, r):
r.rpush("{foo}a", "")
assert r.brpoplpush("{foo}a", "{foo}b") == b""
assert r.brpoplpush("{foo}a", "{foo}b", timeout=0) == b""

def test_cluster_rpoplpush(self, r):
r.rpush("{foo}a", "a1", "a2", "a3")
Expand Down
10 changes: 5 additions & 5 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1462,16 +1462,16 @@ def test_brpop(self, r):
def test_brpoplpush(self, r):
r.rpush("a", "1", "2")
r.rpush("b", "3", "4")
assert r.brpoplpush("a", "b") == b"2"
assert r.brpoplpush("a", "b") == b"1"
assert r.brpoplpush("a", "b", timeout=1) == b"2"
assert r.brpoplpush("a", "b", timeout=1) == b"1"
assert r.brpoplpush("a", "b", timeout=1) is None
assert r.lrange("a", 0, -1) == []
assert r.lrange("b", 0, -1) == [b"1", b"2", b"3", b"4"]

@pytest.mark.onlynoncluster
def test_brpoplpush_empty_string(self, r):
r.rpush("a", "")
assert r.brpoplpush("a", "b") == b""
assert r.brpoplpush("a", "b", timeout=1) == b""

def test_lindex(self, r):
r.rpush("a", "1", "2", "3")
Expand Down Expand Up @@ -1621,8 +1621,8 @@ def test_rpushx(self, r):
assert r.rpushx("a", "b") == 0
assert r.lrange("a", 0, -1) == []
r.rpush("a", "1", "2", "3")
assert r.rpushx("a", "4") == 4
assert r.lrange("a", 0, -1) == [b"1", b"2", b"3", b"4"]
assert r.rpushx("a", "4", "5") == 5
assert r.lrange("a", 0, -1) == [b"1", b"2", b"3", b"4", b"5"]

# SCAN COMMANDS
@pytest.mark.onlynoncluster
Expand Down