@@ -251,6 +251,15 @@ def _formatwarnmsg(msg):
251251 return _wm ._formatwarnmsg_impl (msg )
252252
253253
254+ def _validate_category (category ):
255+ if not isinstance (category , type ):
256+ raise TypeError (f"category must be a Warning subclass, not "
257+ f"'{ type (category ).__name__ } '" )
258+ if not issubclass (category , Warning ):
259+ raise TypeError (f"category must be a Warning subclass, not "
260+ f"class '{ category .__name__ } '" )
261+
262+
254263def filterwarnings (action , message = "" , category = Warning , module = "" , lineno = 0 ,
255264 append = False ):
256265 """Insert an entry into the list of warnings filters (at the front).
@@ -267,8 +276,7 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
267276 raise ValueError (f"invalid action: { action !r} " )
268277 if not isinstance (message , str ):
269278 raise TypeError ("message must be a string" )
270- if not isinstance (category , type ) or not issubclass (category , Warning ):
271- raise TypeError ("category must be a Warning subclass" )
279+ _validate_category (category )
272280 if not isinstance (module , str ):
273281 raise TypeError ("module must be a string" )
274282 if not isinstance (lineno , int ):
@@ -449,9 +457,8 @@ def warn(message, category=None, stacklevel=1, source=None,
449457 # Check category argument
450458 if category is None :
451459 category = UserWarning
452- if not (isinstance (category , type ) and issubclass (category , Warning )):
453- raise TypeError ("category must be a Warning subclass, "
454- "not '{:s}'" .format (type (category ).__name__ ))
460+ else :
461+ _validate_category (category )
455462 if not isinstance (skip_file_prefixes , tuple ):
456463 # The C version demands a tuple for implementation performance.
457464 raise TypeError ('skip_file_prefixes must be a tuple of strs.' )
0 commit comments