From 34641353c3432312e291ab1a7fcd329e7073655b Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Sat, 20 Nov 2021 22:48:11 +0800 Subject: [PATCH 1/2] Add stacklevel to deprecation warnings for argument name change --- src/filelock/_api.py | 7 ++++--- whitelist.txt | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/filelock/_api.py b/src/filelock/_api.py index dd37f0ac..95956480 100644 --- a/src/filelock/_api.py +++ b/src/filelock/_api.py @@ -111,6 +111,7 @@ def acquire( self, timeout: Optional[float] = None, poll_interval: float = 0.05, + *, poll_intervall: Optional[float] = None, ) -> AcquireReturnProxy: """ @@ -138,8 +139,8 @@ def acquire( .. 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. @@ -148,7 +149,7 @@ def acquire( if poll_intervall is not None: msg = "use poll_interval instead of poll_intervall" - warnings.warn(msg, DeprecationWarning) + warnings.warn(msg, DeprecationWarning, stacklevel=2) poll_interval = poll_intervall # Increment the number right at the beginning. We can still undo it, if something fails. diff --git a/whitelist.txt b/whitelist.txt index 537a6e8b..fc82ceb1 100644 --- a/whitelist.txt +++ b/whitelist.txt @@ -39,3 +39,4 @@ unlck util win32 wronly +stacklevel From 2d8ba9a0fd9708065f5355de9735eaed5bc9f342 Mon Sep 17 00:00:00 2001 From: Xuehai Pan Date: Sat, 4 Dec 2021 16:15:04 +0800 Subject: [PATCH 2/2] Add tests for stacklevel in deprecation warnings --- tests/test_filelock.py | 11 +++++++++-- whitelist.txt | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/test_filelock.py b/tests/test_filelock.py index 8606777d..ff2a3737 100644 --- a/tests/test_filelock.py +++ b/tests/test_filelock.py @@ -2,6 +2,7 @@ import sys import threading from contextlib import contextmanager +from inspect import getframeinfo, stack from pathlib import Path, PurePath from stat import S_IWGRP, S_IWOTH, S_IWUSR from types import TracebackType @@ -357,5 +358,11 @@ def test_poll_intervall_deprecated(lock_type: Type[BaseFileLock], tmp_path: Path 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) + with pytest.deprecated_call(match="use poll_interval instead of poll_intervall") as checker: + lock.acquire(poll_intervall=0.05) # the deprecation warning will be captured by the checker + frameinfo = getframeinfo(stack()[0][0]) # get frameinfo of current file and lineno (+1 than the above lineno) + for warning in checker: + if warning.filename == frameinfo.filename and warning.lineno + 1 == frameinfo.lineno: # pragma: no cover + break + else: # pragma: no cover + pytest.fail("No warnings of stacklevel=2 matching.") diff --git a/whitelist.txt b/whitelist.txt index fc82ceb1..68824591 100644 --- a/whitelist.txt +++ b/whitelist.txt @@ -40,3 +40,6 @@ util win32 wronly stacklevel +frameinfo +getframeinfo +lineno