Skip to content

Commit

Permalink
Add stacklevel to deprecation warnings for argument name change (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Dec 26, 2021
1 parent fc70b4a commit 7dc1378
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/filelock/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def acquire(
self,
timeout: Optional[float] = None,
poll_interval: float = 0.05,
*,
poll_intervall: Optional[float] = None,
) -> AcquireReturnProxy:
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
11 changes: 9 additions & 2 deletions tests/test_filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
4 changes: 4 additions & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ unlck
util
win32
wronly
stacklevel
frameinfo
getframeinfo
lineno

0 comments on commit 7dc1378

Please sign in to comment.