@@ -596,25 +596,31 @@ def test_warning_classes(self):
596596 class MyWarningClass (Warning ):
597597 pass
598598
599- class NonWarningSubclass :
600- pass
601-
602599 # passing a non-subclass of Warning should raise a TypeError
603- with self .assertRaises (TypeError ) as cm :
600+ expected = "category must be a Warning subclass, not 'str'"
601+ with self .assertRaisesRegex (TypeError , expected ):
604602 self .module .warn ('bad warning category' , '' )
605- self .assertIn ('category must be a Warning subclass, not ' ,
606- str (cm .exception ))
607-
608- with self .assertRaises (TypeError ) as cm :
609- self .module .warn ('bad warning category' , NonWarningSubclass )
610- self .assertIn ('category must be a Warning subclass, not ' ,
611- str (cm .exception ))
603+ with self .assertRaisesRegex (TypeError , expected ):
604+ self .module .simplefilter ('always' , '' )
605+ with self .assertRaisesRegex (TypeError , expected ):
606+ self .module .filterwarnings ('always' , '' , '' )
607+
608+ expected = "category must be a Warning subclass, not class 'int'"
609+ with self .assertRaisesRegex (TypeError , expected ):
610+ self .module .warn ('bad warning category' , int )
611+ with self .assertRaisesRegex (TypeError , expected ):
612+ self .module .simplefilter ('always' , int )
613+ with self .assertRaisesRegex (TypeError , expected ):
614+ self .module .filterwarnings ('always' , '' , int )
612615
613616 # check that warning instances also raise a TypeError
614- with self .assertRaises (TypeError ) as cm :
617+ expected = "category must be a Warning subclass, not '.*MyWarningClass'"
618+ with self .assertRaisesRegex (TypeError , expected ):
615619 self .module .warn ('bad warning category' , MyWarningClass ())
616- self .assertIn ('category must be a Warning subclass, not ' ,
617- str (cm .exception ))
620+ with self .assertRaisesRegex (TypeError , expected ):
621+ self .module .simplefilter ('always' , MyWarningClass ())
622+ with self .assertRaisesRegex (TypeError , expected ):
623+ self .module .filterwarnings ('always' , '' , MyWarningClass ())
618624
619625 with self .module .catch_warnings ():
620626 self .module .resetwarnings ()
0 commit comments