Skip to content

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

Open
serhiy-storchaka wants to merge 4 commits intopython:mainfrom
serhiy-storchaka:unittest-assertWarns-no-swallow
Open

gh-143231: Do not swallow not matched warnings in assertWarns*()#149229
serhiy-storchaka wants to merge 4 commits intopython:mainfrom
serhiy-storchaka:unittest-assertWarns-no-swallow

Conversation

@serhiy-storchaka
Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka commented May 1, 2026

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
Copy link
Copy Markdown

read-the-docs-community Bot commented May 1, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32502932 | 📁 Comparing c8908f8 against main (f2c7c0d)

  🔍 Preview build  

4 files changed
± download.html
± library/unittest.html
± whatsnew/3.15.html
± whatsnew/changelog.html

Copy link
Copy Markdown
Member

@bitdancer bitdancer left a comment

Choose a reason for hiding this comment

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

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.

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.

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.

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.

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/test/test_unittest/test_case.py
Comment thread Lib/test/test_unittest/test_case.py
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.

Standard English would make this variable name non_matching_warnings.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 1, 2026

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

Copy link
Copy Markdown
Member

@bitdancer bitdancer left a comment

Choose a reason for hiding this comment

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

LGTM

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