Skip to content

Commit

Permalink
Merge pull request #11992 from bluetech/backport-11991
Browse files Browse the repository at this point in the history
[8.0.x] recwarn: fix pytest.warns handling of Warnings with multiple arguments
  • Loading branch information
bluetech authored Feb 16, 2024
2 parents 92203d2 + a232abd commit 9369916
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/11906.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix regression with :func:`pytest.warns` using custom warning subclasses which have more than one parameter in their `__init__`.
8 changes: 4 additions & 4 deletions src/_pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ def found_str():
for w in self:
if not self.matches(w):
warnings.warn_explicit(
str(w.message),
w.message.__class__, # type: ignore[arg-type]
w.filename,
w.lineno,
message=w.message,
category=w.category,
filename=w.filename,
lineno=w.lineno,
module=w.__module__,
source=w.source,
)
14 changes: 14 additions & 0 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,17 @@ def test_it():
result = pytester.runpytest_subprocess()
assert result.ret == ExitCode.INTERRUPTED
result.assert_outcomes()


def test_multiple_arg_custom_warning() -> None:
"""Test for issue #11906."""

class CustomWarning(UserWarning):
def __init__(self, a, b):
pass

with pytest.warns(CustomWarning):
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
with pytest.warns(CustomWarning, match="not gonna match"):
a, b = 1, 2
warnings.warn(CustomWarning(a, b))

0 comments on commit 9369916

Please sign in to comment.