Skip to content

Commit 9ffa6c6

Browse files
j-t-1stefan6419846
andauthored
DEV: Remove ignore Ruff rule B028 (#3214)
B028: No explicit "stacklevel" keyword argument found. --------- Co-authored-by: Stefan <96178532+stefan6419846@users.noreply.github.com>
1 parent 40752eb commit 9ffa6c6

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

pypdf/_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ def rename_kwargs(
466466
f"{old_term} is deprecated as an argument. Use {new_term} instead"
467467
),
468468
category=DeprecationWarning,
469+
stacklevel=3,
469470
)
470471

471472

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ ignore = [
142142
"ARG001", # Unused function argument
143143
"ARG002", # Unused method argument
144144
"ARG004", # Unused static method argument
145-
"B028", # No explicit `stacklevel` keyword argument found
146145
"B904", # Within an `except` clause, raise exceptions with
147146
"B905", # `zip()` without an explicit `strict=` parameter
148147
"BLE001", # Do not catch blind exception: `Exception`

tests/test_utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test the pypdf._utils module."""
22
import io
3+
import subprocess
4+
import sys
35
from pathlib import Path
46

57
import pytest
@@ -276,6 +278,41 @@ def foo(old_param: int = 1, baz: int = 2, new_param: int = 1) -> None:
276278
foo(old_param=12)
277279

278280

281+
def test_rename_kwargs__stacklevel(tmp_path: Path) -> None:
282+
script = tmp_path / "script.py"
283+
script.write_text("""
284+
import functools
285+
import warnings
286+
287+
from pypdf._utils import rename_kwargs
288+
289+
def deprecation(**aliases: str):
290+
def decoration(func):
291+
@functools.wraps(func)
292+
def wrapper(*args, **kwargs):
293+
rename_kwargs(func.__name__, kwargs, aliases, fail=False)
294+
return func(*args, **kwargs)
295+
296+
return wrapper
297+
298+
return decoration
299+
300+
@deprecation(old_param="new_param")
301+
def foo(old_param: int = 1, baz: int = 2, new_param: int = 1) -> None:
302+
pass
303+
304+
warnings.simplefilter("always")
305+
foo(old_param=12)
306+
""")
307+
308+
result = subprocess.run([sys.executable, script], capture_output=True, text=True) # noqa: S603
309+
assert result.returncode == 0
310+
assert result.stderr == (
311+
f"{script}:23: DeprecationWarning: old_param is deprecated as an argument. "
312+
f"Use new_param instead\n foo(old_param=12)\n"
313+
)
314+
315+
279316
@pytest.mark.parametrize(
280317
("input_int", "expected_output"),
281318
[

0 commit comments

Comments
 (0)