Skip to content
Open
13 changes: 11 additions & 2 deletions Lib/_py_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ def _formatwarnmsg(msg):
msg.filename, msg.lineno, msg.line)
return _wm._formatwarnmsg_impl(msg)

def _valid_warning_category(category):
"""Return True if category is a Warning subclass or tuple of such."""
if isinstance(category, tuple):
return all(isinstance(c, type) and issubclass(c, Warning) for c in category)
return isinstance(category, type) and issubclass(category, Warning)

def filterwarnings(action, message="", category=Warning, module="", lineno=0,
append=False):
Expand All @@ -267,8 +272,9 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
raise ValueError(f"invalid action: {action!r}")
if not isinstance(message, str):
raise TypeError("message must be a string")
if not isinstance(category, type) or not issubclass(category, Warning):
raise TypeError("category must be a Warning subclass")
if not _valid_warning_category(category):
raise TypeError("category must be a Warning subclass, "
"not '{:s}'".format(type(category).__name__))
if not isinstance(module, str):
raise TypeError("module must be a string")
if not isinstance(lineno, int):
Expand Down Expand Up @@ -303,6 +309,9 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
"""
if action not in {"error", "ignore", "always", "all", "default", "module", "once"}:
raise ValueError(f"invalid action: {action!r}")
if not _valid_warning_category(category):
raise TypeError("category must be a Warning subclass, "
"not '{:s}'".format(type(category).__name__))
if not isinstance(lineno, int):
raise TypeError("lineno must be an int")
if lineno < 0:
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -2149,5 +2149,6 @@ Jelle Zijlstra
Gennadiy Zlobin
Doug Zongker
Peter Åstrand
Hasrat Ali Arzoo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to keep this sorted by last name. You should be closer to the top.


(Entries should be added in rough alphabetical order by last names)
Loading