Skip to content

Commit 4cd268d

Browse files
Merge pull request #4724 from nicoddemus/pytest-warns-kwargs
emit warning when pytest.warns receives unknown keyword arguments
2 parents 2461a43 + e276bd3 commit 4cd268d

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

changelog/4724.deprecation.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``pytest.warns()`` now emits a warning when it receives unknown keyword arguments.
2+
3+
This will be changed into an error in the future.

src/_pytest/deprecated.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from _pytest.warning_types import PytestDeprecationWarning
1616
from _pytest.warning_types import RemovedInPytest4Warning
17+
from _pytest.warning_types import UnformattedWarning
1718

1819

1920
YIELD_TESTS = "yield tests were removed in pytest 4.0 - {name} will be ignored"
@@ -87,3 +88,9 @@
8788
"pytest_logwarning is deprecated, no longer being called, and will be removed soon\n"
8889
"please use pytest_warning_captured instead"
8990
)
91+
92+
PYTEST_WARNS_UNKNOWN_KWARGS = UnformattedWarning(
93+
PytestDeprecationWarning,
94+
"pytest.warns() got unexpected keyword arguments: {args!r}.\n"
95+
"This will be an error in future versions.",
96+
)

src/_pytest/recwarn.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import six
1212

1313
import _pytest._code
14+
from _pytest.deprecated import PYTEST_WARNS_UNKNOWN_KWARGS
1415
from _pytest.deprecated import WARNS_EXEC
1516
from _pytest.fixtures import yield_fixture
1617
from _pytest.outcomes import fail
@@ -84,10 +85,12 @@ def warns(expected_warning, *args, **kwargs):
8485
8586
"""
8687
__tracebackhide__ = True
87-
match_expr = None
8888
if not args:
89-
if "match" in kwargs:
90-
match_expr = kwargs.pop("match")
89+
match_expr = kwargs.pop("match", None)
90+
if kwargs:
91+
warnings.warn(
92+
PYTEST_WARNS_UNKNOWN_KWARGS.format(args=sorted(kwargs)), stacklevel=2
93+
)
9194
return WarningsChecker(expected_warning, match_expr=match_expr)
9295
elif isinstance(args[0], str):
9396
warnings.warn(WARNS_EXEC, stacklevel=2)
@@ -97,12 +100,12 @@ def warns(expected_warning, *args, **kwargs):
97100
loc = frame.f_locals.copy()
98101
loc.update(kwargs)
99102

100-
with WarningsChecker(expected_warning, match_expr=match_expr):
103+
with WarningsChecker(expected_warning):
101104
code = _pytest._code.Source(code).compile()
102105
six.exec_(code, frame.f_globals, loc)
103106
else:
104107
func = args[0]
105-
with WarningsChecker(expected_warning, match_expr=match_expr):
108+
with WarningsChecker(expected_warning):
106109
return func(*args[1:], **kwargs)
107110

108111

testing/deprecated_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77

88
import pytest
9+
from _pytest.warning_types import PytestDeprecationWarning
910
from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG
1011

1112
pytestmark = pytest.mark.pytester_example_path("deprecated")
@@ -238,3 +239,11 @@ def test_python_deprecation(testdir):
238239
)
239240
else:
240241
assert msg not in result.stdout.str()
242+
243+
244+
def test_pytest_warns_unknown_kwargs():
245+
with pytest.warns(
246+
PytestDeprecationWarning,
247+
match=r"pytest.warns\(\) got unexpected keyword arguments: \['foo'\]",
248+
):
249+
pytest.warns(UserWarning, foo="hello")

0 commit comments

Comments
 (0)