Skip to content

Commit

Permalink
Delete the first-defined (and thus "duplicate") Script class
Browse files Browse the repository at this point in the history
The type annotations are copied to the second definition, and
the mutable default arguments to the `keys` and `args` parameters
are replaced with `None`, as is best-practice.

Closes redis#3332
  • Loading branch information
kurtmckee committed Jul 23, 2024
1 parent fd0b0d3 commit d21bce6
Showing 1 changed file with 10 additions and 52 deletions.
62 changes: 10 additions & 52 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5459,55 +5459,6 @@ def hpttl(self, key: KeyT, *fields: str) -> ResponseT:
AsyncHashCommands = HashCommands


class Script:
"""
An executable Lua script object returned by ``register_script``
"""

def __init__(self, registered_client: "Redis", script: ScriptTextT):
self.registered_client = registered_client
self.script = script
# Precalculate and store the SHA1 hex digest of the script.

if isinstance(script, str):
# We need the encoding from the client in order to generate an
# accurate byte representation of the script
try:
encoder = registered_client.connection_pool.get_encoder()
except AttributeError:
# Cluster
encoder = registered_client.get_encoder()
script = encoder.encode(script)
self.sha = hashlib.sha1(script).hexdigest()

def __call__(
self,
keys: Union[Sequence[KeyT], None] = None,
args: Union[Iterable[EncodableT], None] = None,
client: Union["Redis", None] = None,
):
"""Execute the script, passing any required ``args``"""
keys = keys or []
args = args or []
if client is None:
client = self.registered_client
args = tuple(keys) + tuple(args)
# make sure the Redis server knows about the script
from redis.client import Pipeline

if isinstance(client, Pipeline):
# Make sure the pipeline can register the script before executing.
client.scripts.add(self)
try:
return client.evalsha(self.sha, len(keys), *args)
except NoScriptError:
# Maybe the client is pointed to a different server than the client
# that created this instance?
# Overwrite the sha just in case there was a discrepancy.
self.sha = client.script_load(self.script)
return client.evalsha(self.sha, len(keys), *args)


class AsyncScript:
"""
An executable Lua script object returned by ``register_script``
Expand Down Expand Up @@ -6288,7 +6239,7 @@ class Script:
An executable Lua script object returned by ``register_script``
"""

def __init__(self, registered_client, script):
def __init__(self, registered_client: "Redis", script: ScriptTextT):
self.registered_client = registered_client
self.script = script
# Precalculate and store the SHA1 hex digest of the script.
Expand All @@ -6300,8 +6251,15 @@ def __init__(self, registered_client, script):
script = encoder.encode(script)
self.sha = hashlib.sha1(script).hexdigest()

def __call__(self, keys=[], args=[], client=None):
"Execute the script, passing any required ``args``"
def __call__(
self,
keys: Union[Sequence[KeyT], None] = None,
args: Union[Iterable[EncodableT], None] = None,
client: Union["Redis", None] = None,
):
"""Execute the script, passing any required ``args``"""
keys = keys or []
args = args or []
if client is None:
client = self.registered_client
args = tuple(keys) + tuple(args)
Expand Down

0 comments on commit d21bce6

Please sign in to comment.