-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TST: assert_produces_warning works with filterwarnings #25721
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import warnings | ||
|
||
import pytest | ||
|
||
import pandas.util.testing as tm | ||
|
||
|
||
def f(a=FutureWarning, b=RuntimeWarning): | ||
warnings.warn('f1', a) | ||
warnings.warn('f2', b) | ||
|
||
|
||
@pytest.mark.filterwarnings('ignore:f1:FutureWarning') | ||
@pytest.mark.filterwarnings('ignore:f2:RuntimeWarning') | ||
def test_assert_produces_warning_honors_filter(): | ||
with tm.assert_produces_warning(RuntimeWarning): | ||
f() | ||
|
||
|
||
@pytest.mark.filterwarnings('ignore:f1:FutureWarning') | ||
def test_assert_produces_warning_message(): | ||
with tm.assert_produces_warning(FutureWarning, message='f2'): | ||
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. can you add a test for 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. Asserting that no warning emitted? |
||
f(FutureWarning, FutureWarning) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2571,7 +2571,8 @@ def exception_matches(self, exc_type, exc_value, trace_back): | |
|
||
@contextmanager | ||
def assert_produces_warning(expected_warning=Warning, filter_level="always", | ||
clear=None, check_stacklevel=True): | ||
clear=None, check_stacklevel=True, | ||
message=''): | ||
""" | ||
Context manager for running code expected to either raise a specific | ||
warning, or not raise any warnings. Verifies that the code raises the | ||
|
@@ -2584,7 +2585,7 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always", | |
The type of Exception raised. ``exception.Warning`` is the base | ||
class for all warnings. To check that no warning is returned, | ||
specify ``False`` or ``None``. | ||
filter_level : str, default "always" | ||
filter_level : str or None, default "always" | ||
Specifies whether warnings are ignored, displayed, or turned | ||
into errors. | ||
Valid values are: | ||
|
@@ -2597,6 +2598,7 @@ class for all warnings. To check that no warning is returned, | |
* "module" - print the warning the first time it is generated | ||
from each module | ||
* "once" - print the warning the first time it is generated | ||
* None - do not apply a new filter | ||
|
||
clear : str, default None | ||
If not ``None`` then remove any previously raised warnings from | ||
|
@@ -2608,6 +2610,9 @@ class for all warnings. To check that no warning is returned, | |
If True, displays the line that called the function containing | ||
the warning to show were the function is called. Otherwise, the | ||
line that implements the function is displayed. | ||
message : str, default '' | ||
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. how would one use message? 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. It ends up being hard to use effectively, because unhandled warnings are cause us to raise immediately. Just removing for now, and we can re-add later if needed. |
||
Use in the filter with `filter_level` and `expected_warning` | ||
the control which warnings the filter applies to. | ||
|
||
Examples | ||
-------- | ||
|
@@ -2646,7 +2651,11 @@ class for all warnings. To check that no warning is returned, | |
pass | ||
|
||
saw_warning = False | ||
warnings.simplefilter(filter_level) | ||
if expected_warning and filter_level: | ||
warnings.filterwarnings(filter_level, message, expected_warning) | ||
elif filter_level: | ||
# no expected warnings. | ||
warnings.simplefilter(filter_level) | ||
yield w | ||
extra_warnings = [] | ||
|
||
|
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.
Shouldn't you be testing the new option here?
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.
Yes... I've messed something up, sorry.