FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-143231: Do not swallow not matched warnings in assertWarns*() by serhiy-storchaka · Pull Request #149229 · python/cpython · GitHub

Repository navigation

gh-143231: Do not swallow not matched warnings in assertWarns*() - #149229

Merged
serhiy-storchaka merged 7 commits into
python:mainfrom
serhiy-storchaka:unittest-assertWarns-no-swallow
May 3, 2026
Merged

serhiy-storchaka merged 7 commits into
python:mainfrom
serhiy-storchaka:unittest-assertWarns-no-swallow

Conversation

serhiy-storchaka commented May 1, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

unittest.TestCase methods assertWarns() and assertWarnsRegex() no longer swallow warnings that do not match the specified category or regex. Nested context managers are now supported.

unittest.TestCase methods assertWarns() and assertWarnsRegex() no longer
swallow warnings that do not match the specified category or regex.
Nested context managers are now supported.

read-the-docs-community Bot commented May 1, 2026 •
edited
Loading

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32514225 | 📁 Comparing 6b0080a against main (f2c7c0d)

  🔍 Preview build  

41 files changed · ± 41 modified

± Modified

bitdancer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Has the behavior of record=True changed at all?

Comment on lines +409 to +410
self.assertWarnsRegex(UserWarning, 'foo', self.assertMessagesCM,
'assertWarnsRegex', (UserWarning, 'regex'),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

When I read this I thought at first that you were calling assertWarnsRegex directly instead of calling assertMessagesCM. Even after understanding what you are actually doing it is hard to understand exactly what this is testing. If I understand the difference correctly (now it emits the warning where before it didn't, but that's not what the test is testing), then I think it would be easier to understand if you wrote it like this:

Suggested change
self.assertWarnsRegex(UserWarning, 'foo', self.assertMessagesCM,
'assertWarnsRegex', (UserWarning, 'regex'),
with self.assertWarnsRegex(UserWarning, 'foo'):
self.assertMessagesCM('assertWarnsRegex', (UserWarning, 'regex'),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This test is for the callable interface of assertWarnsRegex(). The next test is for the context manager interface. I think it is better to not mix them in these tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Right, I understand that. But that part of the test is in the inner call to assertMessagesCM, which is the thing calling assertWarnsRexex multiple times using the callable interface. The fact that a warning that is not caught is also emitted is a consequence of the nature of the test, but that's a byproduct, and by using the assertWarns context manager we indicate that we are expecting that byproduct.

If you want to leave it the way you have it I won't block on it ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Oh, I missed this with other tests in test_case where I use nested callables. Here, I used the callable form only because it was the minimal change. But a context manager looks more readable.

Comment thread Lib/unittest/case.py Outdated
exc_name = str(self.expected)
first_matching = None
matched = False
not_matching_warnings = []

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Standard English would make this variable name non_matching_warnings.

bedevere-app Bot commented May 1, 2026

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

bitdancer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM

…ing by module name.

Add the module attribute to warnings.WarningMessage.

Copy link
Copy Markdown
Member Author

I fixed most of the remaining issues with minimal changes in the warnings module. User-visible changes are that warning filtering with actions "default" and "module" now works as expected, and filtering by module name now works.

It required more complex changes in unittest. I am planning to move them to the warnings module later, but this requires serious rewriting of the C code.

Copy link
Copy Markdown
Member Author

Changes to the warnings module were merged in a separate PR #149298, with additional tests.

serhiy-storchaka enabled auto-merge (squash) May 3, 2026 10:17
serhiy-storchaka merged commit a3435d5 into python:main May 3, 2026
51 checks passed
serhiy-storchaka deleted the unittest-assertWarns-no-swallow branch July 1, 2026 16:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL