Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/filelock/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,17 @@ def lifetime(self, value: float | None) -> None:

:param value: the new value in seconds, or ``None`` to disable expiration

:raises ValueError: if *value* is a negative number
:raises TypeError: if *value* is not ``None`` and not a real number

"""
if value is not None:
if isinstance(value, bool) or not isinstance(value, (int, float)):
msg = f"lifetime must be a non-negative number or None, not {type(value).__name__}"
raise TypeError(msg)
if value < 0:
msg = f"lifetime must be non-negative, not {value!r}"
raise ValueError(msg)
self._context.lifetime = value

@property
Expand Down
28 changes: 28 additions & 0 deletions tests/test_lock_expiry.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ def test_lifetime_default_none(tmp_path: Path) -> None:
assert lock.lifetime is None


@pytest.mark.parametrize("bad_value", [-1, -0.5, -1e9])
def test_lifetime_setter_rejects_negative_number(bad_value: float, tmp_path: Path) -> None:
lock = FileLock(tmp_path / "test.lock")
with pytest.raises(ValueError, match="non-negative"):
lock.lifetime = bad_value


@pytest.mark.parametrize("bad_value", ["5", b"5", object(), [1], {1: 2}, complex(1, 0)])
def test_lifetime_setter_rejects_non_numeric(bad_value: object, tmp_path: Path) -> None:
lock = FileLock(tmp_path / "test.lock")
with pytest.raises(TypeError, match="lifetime must be"):
lock.lifetime = bad_value # ty: ignore[invalid-assignment]


@pytest.mark.parametrize("bad_value", [True, False]) # bool is an int subclass; reject it so it can't read as 1s/0s
def test_lifetime_setter_rejects_bool(bad_value: bool, tmp_path: Path) -> None:
lock = FileLock(tmp_path / "test.lock")
with pytest.raises(TypeError, match="lifetime must be"):
lock.lifetime = bad_value


@pytest.mark.parametrize("value", [0, 0.0])
def test_lifetime_setter_accepts_zero(value: float, tmp_path: Path) -> None:
lock = FileLock(tmp_path / "test.lock")
lock.lifetime = value
assert lock.lifetime == 0


def test_lifetime_singleton_mismatch(tmp_path: Path) -> None:
lock_path = tmp_path / "test.lock"
lock1 = FileLock(lock_path, is_singleton=True, lifetime=10.0)
Expand Down