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

Fix misspelled keyword argument poll_interval for method acquire #119

Merged
merged 5 commits into from Nov 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog.rst
@@ -1,6 +1,11 @@
Changelog
=========

v3.4.0 (2021-11-16)
-------------------
- Add correct spelling of poll interval parameter for :meth:`acquire <filelock.BaseFileLock.acquire>` method, raise
deprecation warning when using the misspelled form :pr:`119` - by :user:`XuehaiPan`.

v3.3.2 (2021-10-29)
-------------------
- Accept path types (like ``pathlib.Path`` and ``pathlib.PurePath``) in the constructor for ``FileLock`` objects.
Expand Down
25 changes: 18 additions & 7 deletions src/filelock/_api.py
@@ -1,6 +1,7 @@
import logging
import os
import time
import warnings
from abc import ABC, abstractmethod
from threading import Lock
from types import TracebackType
Expand Down Expand Up @@ -106,13 +107,19 @@ def is_locked(self) -> bool:
"""
return self._lock_file_fd is not None

def acquire(self, timeout: Optional[float] = None, poll_intervall: float = 0.05) -> AcquireReturnProxy:
def acquire(
self,
timeout: Optional[float] = None,
poll_interval: float = 0.05,
poll_intervall: Optional[float] = None,
) -> AcquireReturnProxy:
"""
Try to acquire the file lock.

:param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~timeout` is and
if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired
:param poll_intervall: interval of trying to acquire the lock file
:param poll_interval: interval of trying to acquire the lock file
:param poll_intervall: deprecated, kept for backwards compatibility, use ``poll_interval`` instead
:raises Timeout: if fails to acquire lock within the timeout period
:return: a context object that will unlock the file when the context is exited

Expand All @@ -131,15 +138,19 @@ def acquire(self, timeout: Optional[float] = None, poll_intervall: float = 0.05)

.. versionchanged:: 2.0.0

This method returns now a *proxy* object instead of *self*,
so that it can be used in a with statement without side effects.

This method returns now a *proxy* object instead of *self*, so that it can be used in a with statement \
without side effects.

"""
# Use the default timeout, if no timeout is provided.
if timeout is None:
timeout = self.timeout

if poll_intervall is not None:
msg = "use poll_interval instead of poll_intervall"
warnings.warn(msg, DeprecationWarning)
poll_interval = poll_intervall

# Increment the number right at the beginning. We can still undo it, if something fails.
with self._thread_lock:
self._lock_counter += 1
Expand All @@ -162,8 +173,8 @@ def acquire(self, timeout: Optional[float] = None, poll_intervall: float = 0.05)
raise Timeout(self._lock_file)
else:
msg = "Lock %s not acquired on %s, waiting %s seconds ..."
_LOGGER.debug(msg, lock_id, lock_filename, poll_intervall)
time.sleep(poll_intervall)
_LOGGER.debug(msg, lock_id, lock_filename, poll_interval)
time.sleep(poll_interval)
except BaseException: # Something did go wrong, so decrement the counter.
with self._thread_lock:
self._lock_counter = max(0, self._lock_counter - 1)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_filelock.py
Expand Up @@ -350,3 +350,12 @@ def test_cleanup_soft_lock(tmp_path: Path) -> None:
with lock:
assert lock_path.exists()
assert not lock_path.exists()


@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
def test_poll_intervall_deprecated(lock_type: Type[BaseFileLock], tmp_path: Path) -> None:
lock_path = tmp_path / "a"
lock = lock_type(str(lock_path))

with pytest.deprecated_call(match="use poll_interval instead of poll_intervall"):
lock.acquire(poll_intervall=0.05)