Skip to content

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

Merged
merged 5 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pandas/tests/util/test_assert_produces_warning.py
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):
Copy link
Member

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?

Copy link
Contributor Author

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.

f()


@pytest.mark.filterwarnings('ignore:f1:FutureWarning')
def test_assert_produces_warning_message():
with tm.assert_produces_warning(FutureWarning, message='f2'):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a test for tm.assert_produces_warning(None)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Asserting that no warning emitted?

f(FutureWarning, FutureWarning)
15 changes: 12 additions & 3 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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 ''
Copy link
Contributor

Choose a reason for hiding this comment

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

how would one use message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
--------
Expand Down Expand Up @@ -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 = []

Expand Down