@@ -314,7 +314,7 @@ def __exit__(
314
314
):
315
315
return
316
316
317
- def found_str ():
317
+ def found_str () -> str :
318
318
return pformat ([record .message for record in self ], indent = 2 )
319
319
320
320
try :
@@ -341,14 +341,19 @@ def found_str():
341
341
module = w .__module__ ,
342
342
source = w .source ,
343
343
)
344
- # Check warnings has valid argument type (#10865).
345
- wrn : warnings .WarningMessage
346
- for wrn in self :
347
- self ._validate_message (wrn )
348
-
349
- @staticmethod
350
- def _validate_message (wrn : Any ) -> None :
351
- if not isinstance (msg := wrn .message .args [0 ], str ):
352
- raise TypeError (
353
- f"Warning message must be str, got { msg !r} (type { type (msg ).__name__ } )"
354
- )
344
+
345
+ # Currently in Python it is possible to pass other types than an
346
+ # `str` message when creating `Warning` instances, however this
347
+ # causes an exception when :func:`warnings.filterwarnings` is used
348
+ # to filter those warnings. See
349
+ # https://github.com/python/cpython/issues/103577 for a discussion.
350
+ # While this can be considered a bug in CPython, we put guards in
351
+ # pytest as the error message produced without this check in place
352
+ # is confusing (#10865).
353
+ for w in self :
354
+ msg = w .message .args [0 ] # type: ignore[union-attr]
355
+ if isinstance (msg , str ):
356
+ continue
357
+ raise TypeError (
358
+ f"Warning message must be str, got { msg !r} (type { type (msg ).__name__ } )"
359
+ )
0 commit comments