Skip to content

Commit 7dc1378

Browse files
authored
Add stacklevel to deprecation warnings for argument name change (#121)
1 parent fc70b4a commit 7dc1378

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/filelock/_api.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def acquire(
111111
self,
112112
timeout: Optional[float] = None,
113113
poll_interval: float = 0.05,
114+
*,
114115
poll_intervall: Optional[float] = None,
115116
) -> AcquireReturnProxy:
116117
"""
@@ -138,8 +139,8 @@ def acquire(
138139
139140
.. versionchanged:: 2.0.0
140141
141-
This method returns now a *proxy* object instead of *self*, so that it can be used in a with statement \
142-
without side effects.
142+
This method returns now a *proxy* object instead of *self*,
143+
so that it can be used in a with statement without side effects.
143144
144145
"""
145146
# Use the default timeout, if no timeout is provided.
@@ -148,7 +149,7 @@ def acquire(
148149

149150
if poll_intervall is not None:
150151
msg = "use poll_interval instead of poll_intervall"
151-
warnings.warn(msg, DeprecationWarning)
152+
warnings.warn(msg, DeprecationWarning, stacklevel=2)
152153
poll_interval = poll_intervall
153154

154155
# Increment the number right at the beginning. We can still undo it, if something fails.

tests/test_filelock.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import threading
44
from contextlib import contextmanager
5+
from inspect import getframeinfo, stack
56
from pathlib import Path, PurePath
67
from stat import S_IWGRP, S_IWOTH, S_IWUSR
78
from types import TracebackType
@@ -357,5 +358,11 @@ def test_poll_intervall_deprecated(lock_type: Type[BaseFileLock], tmp_path: Path
357358
lock_path = tmp_path / "a"
358359
lock = lock_type(str(lock_path))
359360

360-
with pytest.deprecated_call(match="use poll_interval instead of poll_intervall"):
361-
lock.acquire(poll_intervall=0.05)
361+
with pytest.deprecated_call(match="use poll_interval instead of poll_intervall") as checker:
362+
lock.acquire(poll_intervall=0.05) # the deprecation warning will be captured by the checker
363+
frameinfo = getframeinfo(stack()[0][0]) # get frameinfo of current file and lineno (+1 than the above lineno)
364+
for warning in checker:
365+
if warning.filename == frameinfo.filename and warning.lineno + 1 == frameinfo.lineno: # pragma: no cover
366+
break
367+
else: # pragma: no cover
368+
pytest.fail("No warnings of stacklevel=2 matching.")

whitelist.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ unlck
3939
util
4040
win32
4141
wronly
42+
stacklevel
43+
frameinfo
44+
getframeinfo
45+
lineno

0 commit comments

Comments
 (0)