Skip to content

Commit 9369916

Browse files
authored
Merge pull request #11992 from bluetech/backport-11991
[8.0.x] recwarn: fix pytest.warns handling of Warnings with multiple arguments
2 parents 92203d2 + a232abd commit 9369916

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

Diff for: changelog/11906.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix regression with :func:`pytest.warns` using custom warning subclasses which have more than one parameter in their `__init__`.

Diff for: src/_pytest/recwarn.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ def found_str():
344344
for w in self:
345345
if not self.matches(w):
346346
warnings.warn_explicit(
347-
str(w.message),
348-
w.message.__class__, # type: ignore[arg-type]
349-
w.filename,
350-
w.lineno,
347+
message=w.message,
348+
category=w.category,
349+
filename=w.filename,
350+
lineno=w.lineno,
351351
module=w.__module__,
352352
source=w.source,
353353
)

Diff for: testing/test_recwarn.py

+14
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,17 @@ def test_it():
550550
result = pytester.runpytest_subprocess()
551551
assert result.ret == ExitCode.INTERRUPTED
552552
result.assert_outcomes()
553+
554+
555+
def test_multiple_arg_custom_warning() -> None:
556+
"""Test for issue #11906."""
557+
558+
class CustomWarning(UserWarning):
559+
def __init__(self, a, b):
560+
pass
561+
562+
with pytest.warns(CustomWarning):
563+
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
564+
with pytest.warns(CustomWarning, match="not gonna match"):
565+
a, b = 1, 2
566+
warnings.warn(CustomWarning(a, b))

0 commit comments

Comments
 (0)