From 497ffc51c1c88aea769035abe74890bd4b5129f6 Mon Sep 17 00:00:00 2001 From: Reagan Lee <96998476+reaganjlee@users.noreply.github.com> Date: Sat, 3 Feb 2024 15:39:49 -0800 Subject: [PATCH] bug fix implement @bluetech changes implement code review changes --- changelog/11906.bugfix.rst | 1 + src/_pytest/recwarn.py | 8 ++++---- testing/test_recwarn.py | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 changelog/11906.bugfix.rst diff --git a/changelog/11906.bugfix.rst b/changelog/11906.bugfix.rst new file mode 100644 index 000000000..68bede540 --- /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 62df274bd..e89fecd12 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -322,10 +322,10 @@ class WarningsChecker(WarningsRecorder): 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, ) diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index d8a6301b3..8f39818db 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -480,6 +480,8 @@ class TestWarns: raise ValueError("some exception") def test_multiple_arg_custom_warning(self) -> None: + """Test for issue #11906.""" + class CustomWarning(UserWarning): def __init__(self, a, b): pass