Skip to content

Add AsyncFunctionCommands #2009

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

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Changes from all 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
34 changes: 22 additions & 12 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5485,6 +5485,9 @@ def readonly(self, **kwargs) -> ResponseT:
return self.execute_command("READONLY", **kwargs)


AsyncClusterCommands = ClusterCommands


class FunctionCommands:
"""
Redis Function commands
Expand All @@ -5497,7 +5500,7 @@ def function_load(
code: str,
replace: Optional[bool] = False,
description: Optional[str] = None,
) -> str:
) -> Union[Awaitable[str], str]:
"""
Load a library to Redis.
:param engine: the name of the execution engine for the library
Expand All @@ -5517,15 +5520,15 @@ def function_load(
pieces.append(code)
return self.execute_command("FUNCTION LOAD", *pieces)

def function_delete(self, library: str) -> str:
def function_delete(self, library: str) -> Union[Awaitable[str], str]:
"""
Delete the library called ``library`` and all its functions.

For more information check https://redis.io/commands/function-delete
"""
return self.execute_command("FUNCTION DELETE", library)

def function_flush(self, mode: str = "SYNC") -> str:
def function_flush(self, mode: str = "SYNC") -> Union[Awaitable[str], str]:
"""
Deletes all the libraries.

Expand All @@ -5535,7 +5538,7 @@ def function_flush(self, mode: str = "SYNC") -> str:

def function_list(
self, library: Optional[str] = "*", withcode: Optional[bool] = False
) -> List:
) -> Union[Awaitable[list], list]:
"""
Return information about the functions and libraries.
:param library: pecify a pattern for matching library names
Expand All @@ -5549,18 +5552,22 @@ def function_list(

def _fcall(
self, command: str, function, numkeys: int, *keys_and_args: Optional[List]
) -> str:
) -> Union[Awaitable[str], str]:
return self.execute_command(command, function, numkeys, *keys_and_args)

def fcall(self, function, numkeys: int, *keys_and_args: Optional[List]) -> str:
def fcall(
self, function, numkeys: int, *keys_and_args: Optional[List]
) -> Union[Awaitable[str], str]:
"""
Invoke a function.

For more information check https://redis.io/commands/fcall
"""
return self._fcall("FCALL", function, numkeys, *keys_and_args)

def fcall_ro(self, function, numkeys: int, *keys_and_args: Optional[List]) -> str:
def fcall_ro(
self, function, numkeys: int, *keys_and_args: Optional[List]
) -> Union[Awaitable[str], str]:
"""
This is a read-only variant of the FCALL command that cannot
execute commands that modify data.
Expand All @@ -5569,7 +5576,7 @@ def fcall_ro(self, function, numkeys: int, *keys_and_args: Optional[List]) -> st
"""
return self._fcall("FCALL_RO", function, numkeys, *keys_and_args)

def function_dump(self) -> str:
def function_dump(self) -> Union[Awaitable[str], str]:
"""
Return the serialized payload of loaded libraries.

Expand All @@ -5582,7 +5589,9 @@ def function_dump(self) -> str:

return self.execute_command("FUNCTION DUMP", **options)

def function_restore(self, payload: str, policy: Optional[str] = "APPEND") -> str:
def function_restore(
self, payload: str, policy: Optional[str] = "APPEND"
) -> Union[Awaitable[str], str]:
"""
Restore libraries from the serialized ``payload``.
You can use the optional policy argument to provide a policy
Expand All @@ -5592,15 +5601,15 @@ def function_restore(self, payload: str, policy: Optional[str] = "APPEND") -> st
"""
return self.execute_command("FUNCTION RESTORE", payload, policy)

def function_kill(self) -> str:
def function_kill(self) -> Union[Awaitable[str], str]:
"""
Kill a function that is currently executing.

For more information check https://redis.io/commands/function-kill
"""
return self.execute_command("FUNCTION KILL")

def function_stats(self) -> list:
def function_stats(self) -> Union[Awaitable[list], list]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Andrew-Chen-Wang Can you educate me. Why don't we use Union[Awaitable[List], List]

As you know type hints aren't my jam ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're basically the same. In Python 3.6, you can only do List[element type], but in 3.7+ you can use the built in list like list[element type]. However, in this case, I'm not using the getattr method at all so either List or list works.

Also this was before I found out the other type hints were merged 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HAHAHA Awesome. Thanks again @Andrew-Chen-Wang. I will always appreciate the education here.

"""
Return information about the function that's currently running
and information about the available execution engines.
Expand All @@ -5610,7 +5619,7 @@ def function_stats(self) -> list:
return self.execute_command("FUNCTION STATS")


AsyncClusterCommands = ClusterCommands
AsyncFunctionCommands = FunctionCommands


class DataAccessCommands(
Expand Down Expand Up @@ -5671,6 +5680,7 @@ class AsyncCoreCommands(
AsyncModuleCommands,
AsyncPubSubCommands,
AsyncScriptCommands,
AsyncFunctionCommands,
):
"""
A class containing all of the implemented redis commands. This class is
Expand Down