From 0cb6d4625c9e3c64bdaaf124e040469d5785c959 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Thu, 22 Dec 2022 15:05:14 +0200 Subject: [PATCH] add str support for set ex parameter --- redis/commands/core.py | 2 ++ tests/test_commands.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/redis/commands/core.py b/redis/commands/core.py index 4cd0a9f73b..aa0042807c 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -2255,6 +2255,8 @@ def set( pieces.append(int(ex.total_seconds())) elif isinstance(ex, int): pieces.append(ex) + elif isinstance(ex, str) and ex.isdigit(): + pieces.append(int(ex)) else: raise DataError("ex must be datetime.timedelta or int") if px is not None: diff --git a/tests/test_commands.py b/tests/test_commands.py index 36d004c98f..94249e9419 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1600,6 +1600,13 @@ def test_set_ex(self, r): with pytest.raises(exceptions.DataError): assert r.set("a", "1", ex=10.0) + @skip_if_server_version_lt("2.6.0") + def test_set_ex_str(self, r): + assert r.set("a", "1", ex="10") + assert 0 < r.ttl("a") <= 10 + with pytest.raises(exceptions.DataError): + assert r.set("a", "1", ex="10.5") + @skip_if_server_version_lt("2.6.0") def test_set_ex_timedelta(self, r): expire_at = datetime.timedelta(seconds=60)