Skip to content

Commit

Permalink
Add type hints for hash commands (#1919)
Browse files Browse the repository at this point in the history
* Add type hint for hash commands

* switch to List

* linters
  • Loading branch information
dvora-h committed Feb 3, 2022
1 parent a5e5996 commit ee078bf
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3792,71 +3792,77 @@ class HashCommands:
see: https://redis.io/topics/data-types-intro#redis-hashes
"""

def hdel(self, name, *keys):
def hdel(self, name: str, *keys: List) -> int:
"""
Delete ``keys`` from hash ``name``
For more information check https://redis.io/commands/hdel
"""
return self.execute_command("HDEL", name, *keys)

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

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

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

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

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

def hkeys(self, name):
def hkeys(self, name: str) -> List:
"""
Return the list of keys within hash ``name``
For more information check https://redis.io/commands/hkeys
"""
return self.execute_command("HKEYS", name)

def hlen(self, name):
def hlen(self, name: str) -> int:
"""
Return the number of elements in hash ``name``
For more information check https://redis.io/commands/hlen
"""
return self.execute_command("HLEN", name)

def hset(self, name, key=None, value=None, mapping=None):
def hset(
self,
name: str,
key: Optional[str] = None,
value: Optional[str] = None,
mapping: Optional[dict] = None,
) -> int:
"""
Set ``key`` to ``value`` within hash ``name``,
``mapping`` accepts a dict of key/value pairs that will be
Expand All @@ -3876,7 +3882,7 @@ def hset(self, name, key=None, value=None, mapping=None):

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

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

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

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

def hvals(self, name):
def hvals(self, name: str) -> List:
"""
Return the list of values within hash ``name``
For more information check https://redis.io/commands/hvals
"""
return self.execute_command("HVALS", name)

def hstrlen(self, name, key):
def hstrlen(self, name: str, key: str) -> int:
"""
Return the number of bytes stored in the value of ``key``
within hash ``name``
Expand Down

0 comments on commit ee078bf

Please sign in to comment.