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 4 commits into
base: main
Choose a base branch
from

Conversation

hasrat17
Copy link

@hasrat17 hasrat17 commented Jul 4, 2025

This PR addresses gh-125893 by adding a type check for the category argument in warnings.simplefilter and warnings.filterwarning,

Previously, warnings.filterwarnings correctly raised an error when the category argument was not a class, but warnings.simplefilter accepted invalid types without raising any error. This inconsistency could lead to confusion and improper warning filtering.

Both warnings.simplefilter and warnings.filterwarning were not raising any warnings while ran with python -O but now this is also handled.

$ ./amd64/python.exe -O -c "import warnings; warnings.resetwarnings(); warnings.simplefilter('default', category='Hello');  print(warnings.filters)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\hasra\OneDrive\Documents\opensource\cpython\Lib\warnings.py", line 182, in simplefilter
    raise TypeError("category must be a Warning subclass, "
TypeError: category must be a Warning subclass, not 'str'
./amd64/python.exe -O -c "import warnings; warnings.resetwarnings(); warnings.filterwarnings('ignore', category=Warning); warnings.filterwarnings('ignore', category='Hello'); print(warnings.filters)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\hasra\OneDrive\Documents\opensource\cpython\Lib\warnings.py", line 149, in filterwarnings
    raise TypeError("category must be a Warning subclass, "
TypeError: category must be a Warning subclass, not 'str'

@bedevere-app
Copy link

bedevere-app bot commented Jul 4, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Sorry for the delay on the review. Please add a test case to test_warnings. You can refer to the devguide if you're unsure how to do that.

Misc/ACKS Outdated
@@ -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.

@ZeroIntensity
Copy link
Member

Oh, also, please add a news entry.

@python-cla-bot
Copy link

python-cla-bot bot commented Jul 10, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@hasrat17
Copy link
Author

Thanks for your review! @ZeroIntensity I've addressed all the feedback and updated accordingly.

@hasrat17 hasrat17 requested a review from ZeroIntensity July 10, 2025 15:32
@hasrat17
Copy link
Author

Hi @ZeroIntensity
I’ve addressed all the review feedback and updated the branch. Could you please take a look at this PR or assign it to the appropriate reviewer? Thank you!

@@ -0,0 +1,2 @@
Added validation for the ``category`` argument in
``warnings.filterwarnings()`` and ``warnings.simplefilter()``
Copy link
Member

Choose a reason for hiding this comment

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

Sphinx markup:

Suggested change
``warnings.filterwarnings()`` and ``warnings.simplefilter()``
:func:`warnings.filterwarnings()` and :func:`warnings.simplefilter()`.

if isinstance(category, type) and issubclass(category, Warning):
return True
if isinstance(category, tuple):
if __debug__:
Copy link
Member

Choose a reason for hiding this comment

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

I'd still consider the isinstance checks above to be expensive. Could you move this __debug__ check to before any calls to isinstance?

@@ -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):
Copy link
Member

Choose a reason for hiding this comment

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

We probably need to skip this if we're not in debug mode, so add something like @unittest.skipUnless(__debug__, ...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
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