Skip to content

Commit a5e5996

Browse files
authored
Add type hints for list commands (#1917)
* add type hints for list commands * fix cluster tests * linters * delete breaking changes and switch to List * linters
1 parent 0d26117 commit a5e5996

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

redis/commands/core.py

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import hashlib
33
import time
44
import warnings
5-
from typing import List, Optional
5+
from typing import List, Optional, Union
66

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

@@ -1864,7 +1864,7 @@ class ListCommands:
18641864
see: https://redis.io/topics/data-types#lists
18651865
"""
18661866

1867-
def blpop(self, keys, timeout=0):
1867+
def blpop(self, keys: List, timeout: Optional[int] = 0) -> List:
18681868
"""
18691869
LPOP a value off of the first non-empty list
18701870
named in the ``keys`` list.
@@ -1883,7 +1883,7 @@ def blpop(self, keys, timeout=0):
18831883
keys.append(timeout)
18841884
return self.execute_command("BLPOP", *keys)
18851885

1886-
def brpop(self, keys, timeout=0):
1886+
def brpop(self, keys: List, timeout: Optional[int] = 0) -> List:
18871887
"""
18881888
RPOP a value off of the first non-empty list
18891889
named in the ``keys`` list.
@@ -1902,7 +1902,9 @@ def brpop(self, keys, timeout=0):
19021902
keys.append(timeout)
19031903
return self.execute_command("BRPOP", *keys)
19041904

1905-
def brpoplpush(self, src, dst, timeout=0):
1905+
def brpoplpush(
1906+
self, src: str, dst: str, timeout: Optional[int] = 0
1907+
) -> Optional[str]:
19061908
"""
19071909
Pop a value off the tail of ``src``, push it on the head of ``dst``
19081910
and then return it.
@@ -1957,7 +1959,7 @@ def lmpop(
19571959

19581960
return self.execute_command("LMPOP", *args)
19591961

1960-
def lindex(self, name, index):
1962+
def lindex(self, name: str, index: int) -> Optional[str]:
19611963
"""
19621964
Return the item from list ``name`` at position ``index``
19631965
@@ -1968,7 +1970,7 @@ def lindex(self, name, index):
19681970
"""
19691971
return self.execute_command("LINDEX", name, index)
19701972

1971-
def linsert(self, name, where, refvalue, value):
1973+
def linsert(self, name: str, where: str, refvalue: str, value: str) -> int:
19721974
"""
19731975
Insert ``value`` in list ``name`` either immediately before or after
19741976
[``where``] ``refvalue``
@@ -1980,15 +1982,15 @@ def linsert(self, name, where, refvalue, value):
19801982
"""
19811983
return self.execute_command("LINSERT", name, where, refvalue, value)
19821984

1983-
def llen(self, name):
1985+
def llen(self, name: str) -> int:
19841986
"""
19851987
Return the length of the list ``name``
19861988
19871989
For more information check https://redis.io/commands/llen
19881990
"""
19891991
return self.execute_command("LLEN", name)
19901992

1991-
def lpop(self, name, count=None):
1993+
def lpop(self, name: str, count: Optional[int] = None) -> Union[str, List, None]:
19921994
"""
19931995
Removes and returns the first elements of the list ``name``.
19941996
@@ -2003,23 +2005,23 @@ def lpop(self, name, count=None):
20032005
else:
20042006
return self.execute_command("LPOP", name)
20052007

2006-
def lpush(self, name, *values):
2008+
def lpush(self, name: str, *values: List) -> int:
20072009
"""
20082010
Push ``values`` onto the head of the list ``name``
20092011
20102012
For more information check https://redis.io/commands/lpush
20112013
"""
20122014
return self.execute_command("LPUSH", name, *values)
20132015

2014-
def lpushx(self, name, *values):
2016+
def lpushx(self, name: str, *values: List) -> int:
20152017
"""
20162018
Push ``value`` onto the head of the list ``name`` if ``name`` exists
20172019
20182020
For more information check https://redis.io/commands/lpushx
20192021
"""
20202022
return self.execute_command("LPUSHX", name, *values)
20212023

2022-
def lrange(self, name, start, end):
2024+
def lrange(self, name: str, start: int, end: int) -> List:
20232025
"""
20242026
Return a slice of the list ``name`` between
20252027
position ``start`` and ``end``
@@ -2031,7 +2033,7 @@ def lrange(self, name, start, end):
20312033
"""
20322034
return self.execute_command("LRANGE", name, start, end)
20332035

2034-
def lrem(self, name, count, value):
2036+
def lrem(self, name: str, count: int, value: str) -> int:
20352037
"""
20362038
Remove the first ``count`` occurrences of elements equal to ``value``
20372039
from the list stored at ``name``.
@@ -2045,15 +2047,15 @@ def lrem(self, name, count, value):
20452047
"""
20462048
return self.execute_command("LREM", name, count, value)
20472049

2048-
def lset(self, name, index, value):
2050+
def lset(self, name: str, index: int, value: str) -> str:
20492051
"""
2050-
Set ``position`` of list ``name`` to ``value``
2052+
Set element at ``index`` of list ``name`` to ``value``
20512053
20522054
For more information check https://redis.io/commands/lset
20532055
"""
20542056
return self.execute_command("LSET", name, index, value)
20552057

2056-
def ltrim(self, name, start, end):
2058+
def ltrim(self, name: str, start: int, end: int) -> str:
20572059
"""
20582060
Trim the list ``name``, removing all values not within the slice
20592061
between ``start`` and ``end``
@@ -2065,7 +2067,7 @@ def ltrim(self, name, start, end):
20652067
"""
20662068
return self.execute_command("LTRIM", name, start, end)
20672069

2068-
def rpop(self, name, count=None):
2070+
def rpop(self, name: str, count: Optional[int] = None) -> Union[str, List, None]:
20692071
"""
20702072
Removes and returns the last elements of the list ``name``.
20712073
@@ -2080,7 +2082,7 @@ def rpop(self, name, count=None):
20802082
else:
20812083
return self.execute_command("RPOP", name)
20822084

2083-
def rpoplpush(self, src, dst):
2085+
def rpoplpush(self, src: str, dst: str) -> str:
20842086
"""
20852087
RPOP a value off of the ``src`` list and atomically LPUSH it
20862088
on to the ``dst`` list. Returns the value.
@@ -2089,23 +2091,30 @@ def rpoplpush(self, src, dst):
20892091
"""
20902092
return self.execute_command("RPOPLPUSH", src, dst)
20912093

2092-
def rpush(self, name, *values):
2094+
def rpush(self, name: str, *values: List) -> int:
20932095
"""
20942096
Push ``values`` onto the tail of the list ``name``
20952097
20962098
For more information check https://redis.io/commands/rpush
20972099
"""
20982100
return self.execute_command("RPUSH", name, *values)
20992101

2100-
def rpushx(self, name, value):
2102+
def rpushx(self, name: str, value: str) -> int:
21012103
"""
21022104
Push ``value`` onto the tail of the list ``name`` if ``name`` exists
21032105
21042106
For more information check https://redis.io/commands/rpushx
21052107
"""
21062108
return self.execute_command("RPUSHX", name, value)
21072109

2108-
def lpos(self, name, value, rank=None, count=None, maxlen=None):
2110+
def lpos(
2111+
self,
2112+
name: str,
2113+
value: str,
2114+
rank: Optional[int] = None,
2115+
count: Optional[int] = None,
2116+
maxlen: Optional[int] = None,
2117+
) -> Union[str, List, None]:
21092118
"""
21102119
Get position of ``value`` within the list ``name``
21112120
@@ -2145,16 +2154,16 @@ def lpos(self, name, value, rank=None, count=None, maxlen=None):
21452154

21462155
def sort(
21472156
self,
2148-
name,
2149-
start=None,
2150-
num=None,
2151-
by=None,
2152-
get=None,
2153-
desc=False,
2154-
alpha=False,
2155-
store=None,
2156-
groups=False,
2157-
):
2157+
name: str,
2158+
start: Optional[int] = None,
2159+
num: Optional[int] = None,
2160+
by: Optional[str] = None,
2161+
get: Optional[List[str]] = None,
2162+
desc: bool = False,
2163+
alpha: bool = False,
2164+
store: Optional[str] = None,
2165+
groups: Optional[bool] = False,
2166+
) -> Union[List, int]:
21582167
"""
21592168
Sort and return the list, set or sorted set at ``name``.
21602169

0 commit comments

Comments
 (0)