From 62c6f5368995a96c8beff5da1a8c5f1ea30364fc Mon Sep 17 00:00:00 2001 From: Volodymyr Kochetkov Date: Fri, 12 Jan 2024 08:57:36 +0200 Subject: [PATCH] feat: 10865 --- changelog/10865.bugfix.rst | 1 + src/_pytest/recwarn.py | 8 ++++++++ testing/python/test_warning_attributes.py | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 changelog/10865.bugfix.rst create mode 100644 testing/python/test_warning_attributes.py diff --git a/changelog/10865.bugfix.rst b/changelog/10865.bugfix.rst new file mode 100644 index 000000000..0d1f9a9ff --- /dev/null +++ b/changelog/10865.bugfix.rst @@ -0,0 +1 @@ +Fix a TypeError in a warning arguments call muted by warnings filter. diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index aa58d43b4..2a34730aa 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -329,3 +329,11 @@ class WarningsChecker(WarningsRecorder): module=w.__module__, source=w.source, ) + # Check warnings has valid argument type + wrn: warnings.WarningMessage + for wrn in self: + if isinstance(wrn.message, Warning): + if not isinstance(wrn.message.args[0], str): + raise TypeError( + f"Warning message must be str, got {type(wrn.message.args[0])}" + ) diff --git a/testing/python/test_warning_attributes.py b/testing/python/test_warning_attributes.py new file mode 100644 index 000000000..8335685b4 --- /dev/null +++ b/testing/python/test_warning_attributes.py @@ -0,0 +1,17 @@ +from _pytest.pytester import Pytester + + +class TestWarningAttributes: + def test_raise_type_error(self, pytester: Pytester) -> None: + pytester.makepyfile( + """ + import pytest + import warnings + + def test_example_one(): + with pytest.warns(UserWarning): + warnings.warn(1) + """ + ) + result = pytester.runpytest() + result.stdout.fnmatch_lines(["*1 failed*"])