diff --git a/docs/changelog.rst b/docs/changelog.rst index 9ffe3256..96f459b9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,11 @@ Changelog ========= +v3.4.0 (2021-11-16) +------------------- +- Add correct spelling of poll interval parameter for :meth:`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. diff --git a/src/filelock/_api.py b/src/filelock/_api.py index 1fd00333..dd37f0ac 100644 --- a/src/filelock/_api.py +++ b/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 @@ -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 @@ -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 @@ -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) diff --git a/tests/test_filelock.py b/tests/test_filelock.py index d523fc79..8606777d 100644 --- a/tests/test_filelock.py +++ b/tests/test_filelock.py @@ -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)