diff --git a/changelog/11906.bugfix.rst b/changelog/11906.bugfix.rst new file mode 100644 index 00000000000..68bede540a8 --- /dev/null +++ b/changelog/11906.bugfix.rst @@ -0,0 +1 @@ +Fix regression with :func:`pytest.warns` using custom warning subclasses which have more than one parameter in their `__init__`. diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index 62df274bd37..707e90499de 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -322,10 +322,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, ) @@ -336,7 +336,9 @@ def found_str(): @staticmethod def _validate_message(wrn: Any) -> None: - if not isinstance(msg := wrn.message.args[0], str): + if type(wrn.message) is UserWarning and not isinstance( + msg := wrn.message.args[0], str + ): raise TypeError( f"Warning message must be str, got {msg!r} (type {type(msg).__name__})" ) diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index e269bd7ddc9..2a1528eb9ce 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -504,3 +504,17 @@ def test_raise_type_error_on_non_string_warning_cpython() -> None: with warnings.catch_warnings(): warnings.filterwarnings("ignore", "test") warnings.warn(1) # type: ignore + + +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))