[8.0.x] recwarn: fix pytest.warns handling of Warnings with multiple arguments
(cherry picked from commit fbe18fc7a9
)
This commit is contained in:
parent
92203d2b78
commit
a232abd56c
|
@ -0,0 +1 @@
|
||||||
|
Fix regression with :func:`pytest.warns` using custom warning subclasses which have more than one parameter in their `__init__`.
|
|
@ -344,10 +344,10 @@ class WarningsChecker(WarningsRecorder):
|
||||||
for w in self:
|
for w in self:
|
||||||
if not self.matches(w):
|
if not self.matches(w):
|
||||||
warnings.warn_explicit(
|
warnings.warn_explicit(
|
||||||
str(w.message),
|
message=w.message,
|
||||||
w.message.__class__, # type: ignore[arg-type]
|
category=w.category,
|
||||||
w.filename,
|
filename=w.filename,
|
||||||
w.lineno,
|
lineno=w.lineno,
|
||||||
module=w.__module__,
|
module=w.__module__,
|
||||||
source=w.source,
|
source=w.source,
|
||||||
)
|
)
|
||||||
|
|
|
@ -550,3 +550,17 @@ class TestWarns:
|
||||||
result = pytester.runpytest_subprocess()
|
result = pytester.runpytest_subprocess()
|
||||||
assert result.ret == ExitCode.INTERRUPTED
|
assert result.ret == ExitCode.INTERRUPTED
|
||||||
result.assert_outcomes()
|
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))
|
||||||
|
|
Loading…
Reference in New Issue