Skip to content

gh-125893: Add type check for category argument in warnings.simplefilter and warnings.filterwarning #136305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added testcase and refactored warning logic for debug
  • Loading branch information
hasrat17 committed Jul 10, 2025
commit 022e4b350c94b0bee18ce29a35c75ad5c3d40556
14 changes: 11 additions & 3 deletions Lib/_py_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,18 @@ def _formatwarnmsg(msg):
return _wm._formatwarnmsg_impl(msg)

def _valid_warning_category(category):
Copy link
Member

Choose a reason for hiding this comment

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

This function is a bit expensive. It's likely worth doing something like this:

if not __debug__:
    return True

"""Return True if category is a Warning subclass or tuple of such."""
"""
Return True if category is a Warning subclass or tuple of such.
Always perform class checks; only perform tuple iteration in debug mode.
"""
if isinstance(category, type) and issubclass(category, Warning):
return True
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)
if __debug__:
return all(isinstance(c, type) and issubclass(c, Warning)
for c in category)
return True
return False

def filterwarnings(action, message="", category=Warning, module="", lineno=0,
append=False):
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,33 @@ def test_argument_validation(self):
with self.assertRaises(ValueError):
self.module.simplefilter('ignore', lineno=-1)

def test_invalid_category_types(self):
with self.assertRaises(TypeError):
self.module.filterwarnings("ignore", category="notawarning")
with self.assertRaises(TypeError):
self.module.filterwarnings("ignore", category=123)
with self.assertRaises(TypeError):
self.module.filterwarnings("ignore", category=17.02)
with self.assertRaises(TypeError):
self.module.filterwarnings("ignore", category=True)
with self.assertRaises(TypeError):
self.module.filterwarnings(
"ignore", category=(UserWarning, 17)
)

with self.assertRaises(TypeError):
self.module.simplefilter("ignore", category="notawarning")
with self.assertRaises(TypeError):
self.module.simplefilter("ignore", category=123)
with self.assertRaises(TypeError):
self.module.filterwarnings("ignore", category=17.02)
with self.assertRaises(TypeError):
self.module.filterwarnings("ignore", category=True)
with self.assertRaises(TypeError):
self.module.simplefilter(
"ignore", category=(UserWarning, 'abc')
)

def test_catchwarnings_with_simplefilter_ignore(self):
with self.module.catch_warnings(module=self.module):
self.module.resetwarnings()
Expand Down
2 changes: 1 addition & 1 deletion Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Emmanuel Arias
Alicia Arlen
Jeffrey Armstrong
Justin Turner Arthur
Hasrat Ali Arzoo
Jason Asbahr
David Ascher
Ammar Askar
Expand Down Expand Up @@ -2149,6 +2150,5 @@ Jelle Zijlstra
Gennadiy Zlobin
Doug Zongker
Peter Åstrand
Hasrat Ali Arzoo

(Entries should be added in rough alphabetical order by last names)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added validation for the ``category`` argument in
``warnings.filterwarnings()`` and ``warnings.simplefilter()``
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy