Skip to content

[3.12] gh-125893: Add type check for category argument in warnings.simplefilter and warnings.filterwarning#136303

Closed
hasrat17 wants to merge 2 commits intopython:3.12from
hasrat17:fix-warnings-simplefilter
Closed

[3.12] gh-125893: Add type check for category argument in warnings.simplefilter and warnings.filterwarning#136303
hasrat17 wants to merge 2 commits intopython:3.12from
hasrat17:fix-warnings-simplefilter

Conversation

@hasrat17
Copy link
Copy Markdown

@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'

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented Jul 4, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link
Copy Markdown

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.

@bedevere-app
Copy link
Copy Markdown

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.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Jul 4, 2025

Thanks for the PR but could you instead create a branch that is checked out from main, make your changes, then open a PR against main instead? we'll take care of backporting the changes to 3.13+ (3.12 won't get fixed as it's security only)

@brianschubert
Copy link
Copy Markdown
Contributor

Closing since #136305 has been opened against main

@hasrat17
Copy link
Copy Markdown
Author

hasrat17 commented Jul 4, 2025

As suggested by @picnixz I opened #136305 against main please review it. Thanks

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.

3 participants