@@ -130,6 +130,12 @@ def _formatwarnmsg(msg):
130130 msg .filename , msg .lineno , msg .line )
131131 return _formatwarnmsg_impl (msg )
132132
133+ def _valid_warning_category (category ):
134+ """Return True if category is a Warning subclass or tuple of such."""
135+ if isinstance (category , tuple ):
136+ return all (isinstance (c , type ) and issubclass (c , Warning ) for c in category )
137+ return isinstance (category , type ) and issubclass (category , Warning )
138+
133139def filterwarnings (action , message = "" , category = Warning , module = "" , lineno = 0 ,
134140 append = False ):
135141 """Insert an entry into the list of warnings filters (at the front).
@@ -145,7 +151,7 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
145151 assert action in ("error" , "ignore" , "always" , "default" , "module" ,
146152 "once" ), "invalid action: %r" % (action ,)
147153 assert isinstance (message , str ), "message must be a string"
148- if not ( isinstance ( category , type ) and issubclass ( category , Warning ) ):
154+ if not _valid_warning_category ( category ):
149155 raise TypeError ("category must be a Warning subclass, "
150156 "not '{:s}'" .format (type (category ).__name__ ))
151157 assert isinstance (module , str ), "module must be a string"
@@ -178,7 +184,7 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
178184 """
179185 assert action in ("error" , "ignore" , "always" , "default" , "module" ,
180186 "once" ), "invalid action: %r" % (action ,)
181- if not ( isinstance ( category , type ) and issubclass ( category , Warning ) ):
187+ if not _valid_warning_category ( category ):
182188 raise TypeError ("category must be a Warning subclass, "
183189 "not '{:s}'" .format (type (category ).__name__ ))
184190 assert isinstance (lineno , int ) and lineno >= 0 , \
0 commit comments