Skip to content
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

Correct the typedef of lock.extend() to accept floats #3420

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Fix lock.extend() typedef to accept float TTL extension
* Move doctests (doc code examples) to main branch
* Update `ResponseT` type hint
* Allow to control the minimum SSL version
Expand Down
7 changes: 4 additions & 3 deletions redis/asyncio/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING, Awaitable, Optional, Union

from redis.exceptions import LockError, LockNotOwnedError
from redis.typing import Number

if TYPE_CHECKING:
from redis.asyncio import Redis, RedisCluster
Expand Down Expand Up @@ -82,7 +83,7 @@ def __init__(
timeout: Optional[float] = None,
sleep: float = 0.1,
blocking: bool = True,
blocking_timeout: Optional[float] = None,
blocking_timeout: Optional[Number] = None,
thread_local: bool = True,
):
"""
Expand Down Expand Up @@ -167,7 +168,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):
async def acquire(
self,
blocking: Optional[bool] = None,
blocking_timeout: Optional[float] = None,
blocking_timeout: Optional[Number] = None,
token: Optional[Union[str, bytes]] = None,
):
"""
Expand Down Expand Up @@ -262,7 +263,7 @@ async def do_release(self, expected_token: bytes) -> None:
raise LockNotOwnedError("Cannot release a lock that's no longer owned")

def extend(
self, additional_time: float, replace_ttl: bool = False
self, additional_time: Number, replace_ttl: bool = False
) -> Awaitable[bool]:
"""
Adds more time to an already acquired lock.
Expand Down
2 changes: 1 addition & 1 deletion redis/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def do_release(self, expected_token: str) -> None:
lock_name=self.name,
)

def extend(self, additional_time: int, replace_ttl: bool = False) -> bool:
def extend(self, additional_time: Number, replace_ttl: bool = False) -> bool:
"""
Adds more time to an already acquired lock.

Expand Down
9 changes: 5 additions & 4 deletions tests/test_asyncio/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ async def test_extend_lock_replace_ttl(self, r):
await lock.release()

async def test_extend_lock_float(self, r):
lock = self.get_lock(r, "foo", timeout=10.0)
lock = self.get_lock(r, "foo", timeout=10.5)
assert await lock.acquire(blocking=False)
assert 8000 < (await r.pttl("foo")) <= 10000
assert await lock.extend(10.0)
assert 16000 < (await r.pttl("foo")) <= 20000
assert 10400 < (await r.pttl("foo")) <= 10500
old_ttl = await r.pttl("foo")
assert await lock.extend(10.5)
assert old_ttl + 10400 < (await r.pttl("foo")) <= old_ttl + 10500
await lock.release()

async def test_extending_unlocked_lock_raises_error(self, r):
Expand Down
9 changes: 5 additions & 4 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,12 @@ def test_extend_lock_replace_ttl(self, r):
lock.release()

def test_extend_lock_float(self, r):
lock = self.get_lock(r, "foo", timeout=10.0)
lock = self.get_lock(r, "foo", timeout=10.5)
assert lock.acquire(blocking=False)
assert 8000 < r.pttl("foo") <= 10000
assert lock.extend(10.0)
assert 16000 < r.pttl("foo") <= 20000
assert 10400 < r.pttl("foo") <= 10500
old_ttl = r.pttl("foo")
assert lock.extend(10.5)
assert old_ttl + 10400 < r.pttl("foo") <= old_ttl + 10500
lock.release()

def test_extending_unlocked_lock_raises_error(self, r):
Expand Down