-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: implement matching warning message #37263
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 6 commits into
pandas-dev:master
from
ivanovmg:feature/assert-produces-warning
Oct 20, 2020
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11ade81
TST: add tests for matching warning messages
ivanovmg e61b417
ENH: implement matching warning message
ivanovmg 8f58dd1
FIX: import using tm alias
ivanovmg 155e8f3
DOC: add newlines in fixtures docstrings
ivanovmg 29debb6
TYP: assert_produces_warning
ivanovmg e0896df
MOVE: tests to test_assert_produces_warning
ivanovmg 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
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 |
---|---|---|
@@ -0,0 +1,138 @@ | ||
"""" | ||
Test module for testing ``pandas._testing.assert_produces_warning``. | ||
""" | ||
|
||
import warnings | ||
|
||
import pytest | ||
|
||
from pandas.errors import DtypeWarning, PerformanceWarning | ||
|
||
import pandas._testing as tm | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
RuntimeWarning, | ||
ResourceWarning, | ||
UserWarning, | ||
FutureWarning, | ||
DeprecationWarning, | ||
PerformanceWarning, | ||
DtypeWarning, | ||
], | ||
) | ||
def category(request): | ||
""" | ||
Return unique warning. | ||
|
||
Useful for testing behavior of tm.assert_produces_warning with various categories. | ||
""" | ||
return request.param | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
(RuntimeWarning, UserWarning), | ||
(UserWarning, FutureWarning), | ||
(FutureWarning, RuntimeWarning), | ||
(DeprecationWarning, PerformanceWarning), | ||
(PerformanceWarning, FutureWarning), | ||
(DtypeWarning, DeprecationWarning), | ||
(ResourceWarning, DeprecationWarning), | ||
(FutureWarning, DeprecationWarning), | ||
], | ||
ids=lambda x: type(x).__name__, | ||
) | ||
def pair_different_warnings(request): | ||
""" | ||
Return pair or different warnings. | ||
|
||
Useful for testing how several different warnings are handled | ||
in tm.assert_produces_warning. | ||
""" | ||
return request.param | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"message, match", | ||
[ | ||
("", None), | ||
("", ""), | ||
("Warning message", r".*"), | ||
("Warning message", "War"), | ||
("Warning message", r"[Ww]arning"), | ||
("Warning message", "age"), | ||
("Warning message", r"age$"), | ||
("Message 12-234 with numbers", r"\d{2}-\d{3}"), | ||
("Message 12-234 with numbers", r"^Mes.*\d{2}-\d{3}"), | ||
("Message 12-234 with numbers", r"\d{2}-\d{3}\s\S+"), | ||
("Message, which we do not match", None), | ||
], | ||
) | ||
def test_catch_warning_category_and_match(category, message, match): | ||
with tm.assert_produces_warning(category, match=match): | ||
warnings.warn(message, category) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"message, match", | ||
[ | ||
("Warning message", "Not this message"), | ||
("Warning message", "warning"), | ||
("Warning message", r"\d+"), | ||
], | ||
) | ||
def test_fail_to_match(category, message, match): | ||
msg = f"Did not see warning {repr(category.__name__)} matching" | ||
with pytest.raises(AssertionError, match=msg): | ||
with tm.assert_produces_warning(category, match=match): | ||
warnings.warn(message, category) | ||
|
||
|
||
def test_fail_to_catch_actual_warning(pair_different_warnings): | ||
expected_category, actual_category = pair_different_warnings | ||
match = "Did not see expected warning of class" | ||
with pytest.raises(AssertionError, match=match): | ||
with tm.assert_produces_warning(expected_category): | ||
warnings.warn("warning message", actual_category) | ||
|
||
|
||
def test_ignore_extra_warning(pair_different_warnings): | ||
expected_category, extra_category = pair_different_warnings | ||
with tm.assert_produces_warning(expected_category, raise_on_extra_warnings=False): | ||
warnings.warn("Expected warning", expected_category) | ||
warnings.warn("Unexpected warning OK", extra_category) | ||
|
||
|
||
def test_raise_on_extra_warning(pair_different_warnings): | ||
expected_category, extra_category = pair_different_warnings | ||
match = r"Caused unexpected warning\(s\)" | ||
with pytest.raises(AssertionError, match=match): | ||
with tm.assert_produces_warning(expected_category): | ||
warnings.warn("Expected warning", expected_category) | ||
warnings.warn("Unexpected warning NOT OK", extra_category) | ||
|
||
|
||
def test_same_category_different_messages_first_match(): | ||
category = UserWarning | ||
with tm.assert_produces_warning(category, match=r"^Match this"): | ||
warnings.warn("Match this", category) | ||
warnings.warn("Do not match that", category) | ||
warnings.warn("Do not match that either", category) | ||
|
||
|
||
def test_same_category_different_messages_last_match(): | ||
category = DeprecationWarning | ||
with tm.assert_produces_warning(category, match=r"^Match this"): | ||
warnings.warn("Do not match that", category) | ||
warnings.warn("Do not match that either", category) | ||
warnings.warn("Match this", category) | ||
|
||
|
||
def test_right_category_wrong_match_raises(pair_different_warnings): | ||
target_category, other_category = pair_different_warnings | ||
with pytest.raises(AssertionError, match="Did not see warning.*matching"): | ||
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) |
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.
There is already a file with tests for this, in
tests/util/test_assert_produces_warning.py
, can you move this there?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.
Oh, I completely missed it. I moved tests as requested.