@@ -479,15 +479,38 @@ def test_catch_warning_within_raise(self) -> None:
479
479
warnings .warn ("some warning" , category = FutureWarning )
480
480
raise ValueError ("some exception" )
481
481
482
- def test_multiple_arg_custom_warning (self ) -> None :
483
- """Test for issue #11906."""
482
+ def test_raise_type_error_on_non_string_warning () -> None :
483
+ """Check pytest.warns validates warning messages are strings (#10865)."""
484
+ with pytest .raises (TypeError , match = "Warning message must be str" ):
485
+ with pytest .warns (UserWarning ):
486
+ warnings .warn (1 ) # type: ignore
487
+
488
+ def test_no_raise_type_error_on_string_warning () -> None :
489
+ """Check pytest.warns validates warning messages are strings (#10865)."""
490
+ with pytest .warns (UserWarning ):
491
+ warnings .warn ("Warning" )
492
+
493
+ @pytest .mark .skipif (
494
+ hasattr (sys , "pypy_version_info" ),
495
+ reason = "Not for pypy" ,
496
+ )
497
+ def test_raise_type_error_on_non_string_warning_cpython () -> None :
498
+ # Check that we get the same behavior with the stdlib, at least if filtering
499
+ # (see https://github.com/python/cpython/issues/103577 for details)
500
+ with pytest .raises (TypeError ):
501
+ with warnings .catch_warnings ():
502
+ warnings .filterwarnings ("ignore" , "test" )
503
+ warnings .warn (1 ) # type: ignore
484
504
485
- class CustomWarning (UserWarning ):
486
- def __init__ (self , a , b ):
487
- pass
505
+ def test_multiple_arg_custom_warning (self ) -> None :
506
+ """Test for issue #11906."""
488
507
489
- with pytest .warns (CustomWarning ):
490
- with pytest .raises (pytest .fail .Exception , match = "DID NOT WARN" ):
491
- with pytest .warns (CustomWarning , match = "not gonna match" ):
492
- a , b = 1 , 2
493
- warnings .warn (CustomWarning (a , b ))
508
+ class CustomWarning (UserWarning ):
509
+ def __init__ (self , a , b ):
510
+ pass
511
+
512
+ with pytest .warns (CustomWarning ):
513
+ with pytest .raises (pytest .fail .Exception , match = "DID NOT WARN" ):
514
+ with pytest .warns (CustomWarning , match = "not gonna match" ):
515
+ a , b = 1 , 2
516
+ warnings .warn (CustomWarning (a , b ))
0 commit comments