From dcf9da92be44e5347fdb559c6717c031daae2b25 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 16 Feb 2024 13:38:55 +0200 Subject: [PATCH] recwarn: minor style improvements In preparation for next commit. --- src/_pytest/recwarn.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index ddb240eb2..5e1fea2a0 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -314,7 +314,7 @@ class WarningsChecker(WarningsRecorder): ): return - def found_str(): + def found_str() -> str: return pformat([record.message for record in self], indent=2) try: @@ -341,14 +341,19 @@ class WarningsChecker(WarningsRecorder): module=w.__module__, source=w.source, ) - # Check warnings has valid argument type (#10865). - wrn: warnings.WarningMessage - for wrn in self: - self._validate_message(wrn) - @staticmethod - def _validate_message(wrn: Any) -> None: - if not isinstance(msg := wrn.message.args[0], str): - raise TypeError( - f"Warning message must be str, got {msg!r} (type {type(msg).__name__})" - ) + # Currently in Python it is possible to pass other types than an + # `str` message when creating `Warning` instances, however this + # causes an exception when :func:`warnings.filterwarnings` is used + # to filter those warnings. See + # https://github.com/python/cpython/issues/103577 for a discussion. + # While this can be considered a bug in CPython, we put guards in + # pytest as the error message produced without this check in place + # is confusing (#10865). + for w in self: + msg = w.message.args[0] # type: ignore[union-attr] + if isinstance(msg, str): + continue + raise TypeError( + f"Warning message must be str, got {msg!r} (type {type(msg).__name__})" + )