Skip to content

Commit ee078bf

Browse files
authored
Add type hints for hash commands (#1919)
* Add type hint for hash commands * switch to List * linters
1 parent a5e5996 commit ee078bf

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

redis/commands/core.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3792,71 +3792,77 @@ class HashCommands:
37923792
see: https://redis.io/topics/data-types-intro#redis-hashes
37933793
"""
37943794

3795-
def hdel(self, name, *keys):
3795+
def hdel(self, name: str, *keys: List) -> int:
37963796
"""
37973797
Delete ``keys`` from hash ``name``
37983798
37993799
For more information check https://redis.io/commands/hdel
38003800
"""
38013801
return self.execute_command("HDEL", name, *keys)
38023802

3803-
def hexists(self, name, key):
3803+
def hexists(self, name: str, key: str) -> bool:
38043804
"""
38053805
Returns a boolean indicating if ``key`` exists within hash ``name``
38063806
38073807
For more information check https://redis.io/commands/hexists
38083808
"""
38093809
return self.execute_command("HEXISTS", name, key)
38103810

3811-
def hget(self, name, key):
3811+
def hget(self, name: str, key: str) -> Optional[str]:
38123812
"""
38133813
Return the value of ``key`` within the hash ``name``
38143814
38153815
For more information check https://redis.io/commands/hget
38163816
"""
38173817
return self.execute_command("HGET", name, key)
38183818

3819-
def hgetall(self, name):
3819+
def hgetall(self, name: str) -> dict:
38203820
"""
38213821
Return a Python dict of the hash's name/value pairs
38223822
38233823
For more information check https://redis.io/commands/hgetall
38243824
"""
38253825
return self.execute_command("HGETALL", name)
38263826

3827-
def hincrby(self, name, key, amount=1):
3827+
def hincrby(self, name: str, key: str, amount: int = 1) -> int:
38283828
"""
38293829
Increment the value of ``key`` in hash ``name`` by ``amount``
38303830
38313831
For more information check https://redis.io/commands/hincrby
38323832
"""
38333833
return self.execute_command("HINCRBY", name, key, amount)
38343834

3835-
def hincrbyfloat(self, name, key, amount=1.0):
3835+
def hincrbyfloat(self, name: str, key: str, amount: float = 1.0) -> float:
38363836
"""
38373837
Increment the value of ``key`` in hash ``name`` by floating ``amount``
38383838
38393839
For more information check https://redis.io/commands/hincrbyfloat
38403840
"""
38413841
return self.execute_command("HINCRBYFLOAT", name, key, amount)
38423842

3843-
def hkeys(self, name):
3843+
def hkeys(self, name: str) -> List:
38443844
"""
38453845
Return the list of keys within hash ``name``
38463846
38473847
For more information check https://redis.io/commands/hkeys
38483848
"""
38493849
return self.execute_command("HKEYS", name)
38503850

3851-
def hlen(self, name):
3851+
def hlen(self, name: str) -> int:
38523852
"""
38533853
Return the number of elements in hash ``name``
38543854
38553855
For more information check https://redis.io/commands/hlen
38563856
"""
38573857
return self.execute_command("HLEN", name)
38583858

3859-
def hset(self, name, key=None, value=None, mapping=None):
3859+
def hset(
3860+
self,
3861+
name: str,
3862+
key: Optional[str] = None,
3863+
value: Optional[str] = None,
3864+
mapping: Optional[dict] = None,
3865+
) -> int:
38603866
"""
38613867
Set ``key`` to ``value`` within hash ``name``,
38623868
``mapping`` accepts a dict of key/value pairs that will be
@@ -3876,7 +3882,7 @@ def hset(self, name, key=None, value=None, mapping=None):
38763882

38773883
return self.execute_command("HSET", name, *items)
38783884

3879-
def hsetnx(self, name, key, value):
3885+
def hsetnx(self, name: str, key: str, value: str) -> bool:
38803886
"""
38813887
Set ``key`` to ``value`` within hash ``name`` if ``key`` does not
38823888
exist. Returns 1 if HSETNX created a field, otherwise 0.
@@ -3885,7 +3891,7 @@ def hsetnx(self, name, key, value):
38853891
"""
38863892
return self.execute_command("HSETNX", name, key, value)
38873893

3888-
def hmset(self, name, mapping):
3894+
def hmset(self, name: str, mapping: dict) -> str:
38893895
"""
38903896
Set key to value within hash ``name`` for each corresponding
38913897
key and value from the ``mapping`` dict.
@@ -3905,7 +3911,7 @@ def hmset(self, name, mapping):
39053911
items.extend(pair)
39063912
return self.execute_command("HMSET", name, *items)
39073913

3908-
def hmget(self, name, keys, *args):
3914+
def hmget(self, name: str, keys: List, *args: List) -> List:
39093915
"""
39103916
Returns a list of values ordered identically to ``keys``
39113917
@@ -3914,15 +3920,15 @@ def hmget(self, name, keys, *args):
39143920
args = list_or_args(keys, args)
39153921
return self.execute_command("HMGET", name, *args)
39163922

3917-
def hvals(self, name):
3923+
def hvals(self, name: str) -> List:
39183924
"""
39193925
Return the list of values within hash ``name``
39203926
39213927
For more information check https://redis.io/commands/hvals
39223928
"""
39233929
return self.execute_command("HVALS", name)
39243930

3925-
def hstrlen(self, name, key):
3931+
def hstrlen(self, name: str, key: str) -> int:
39263932
"""
39273933
Return the number of bytes stored in the value of ``key``
39283934
within hash ``name``

0 commit comments

Comments
 (0)