-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Bug: assert_produces_warning(None) not raising AssertionError with warning #38626
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
Merged
jreback
merged 8 commits into
pandas-dev:master
from
mzeitlin11:bug/assert_produces_warning
Dec 22, 2020
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
724a2af
BUG: assert_produces_warning() with None or False doesn't raise
mzeitlin11 0a625f3
Remove commented line
mzeitlin11 2e1d58a
Honor extra warnings arg
mzeitlin11 aed3465
Add test for None with false raise on extra
mzeitlin11 c82b859
Refactor added tests
mzeitlin11 1938ad9
Merge remote-tracking branch 'upstream/master' into bug/assert_produc…
mzeitlin11 6f3c8ff
xfail failing tests
mzeitlin11 8193ea1
xfail parser test
mzeitlin11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2724,11 +2724,10 @@ class for all warnings. To check that no warning is returned, | |
extra_warnings = [] | ||
|
||
for actual_warning in w: | ||
if not expected_warning: | ||
continue | ||
|
||
expected_warning = cast(Type[Warning], expected_warning) | ||
if issubclass(actual_warning.category, expected_warning): | ||
if expected_warning and issubclass( | ||
actual_warning.category, expected_warning | ||
): | ||
Comment on lines
-2727
to
+2730
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is a good catch. |
||
saw_warning = True | ||
|
||
if check_stacklevel and issubclass( | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,3 +152,20 @@ def test_right_category_wrong_match_raises(pair_different_warnings): | |
with tm.assert_produces_warning(target_category, match=r"^Match this"): | ||
warnings.warn("Do not match it", target_category) | ||
warnings.warn("Match this", other_category) | ||
|
||
|
||
@pytest.mark.parametrize("false_or_none", [False, None]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good tests to prevent the issue from happening! |
||
class TestFalseOrNoneExpectedWarning: | ||
def test_raise_on_warning(self, false_or_none): | ||
msg = r"Caused unexpected warning\(s\)" | ||
with pytest.raises(AssertionError, match=msg): | ||
with tm.assert_produces_warning(false_or_none): | ||
f() | ||
|
||
def test_no_raise_without_warning(self, false_or_none): | ||
with tm.assert_produces_warning(false_or_none): | ||
pass | ||
|
||
def test_no_raise_with_false_raise_on_extra(self, false_or_none): | ||
with tm.assert_produces_warning(false_or_none, raise_on_extra_warnings=False): | ||
f() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this code functionally the same as existing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought if
expected_warning
isFalse
orNone
, thenextra_warnings
does not get appended to because of thecontinue
. So the check at the end for raising on extra warnings doesn't get triggered.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current implementation will ignore the else clause below, which consolidates extra warnings. I see that I messed that up in one of my previous PRs. This PR reverts that error.
Probably, if some functions are extracted, that would improve the readability of the logic (maybe in a separate PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The added tests do raise on master as well, so behavior for them has changed