diff options
-rw-r--r-- | doc/release/1.12.0-notes.rst | 12 | ||||
-rw-r--r-- | numpy/testing/tests/test_utils.py | 12 | ||||
-rw-r--r-- | numpy/testing/utils.py | 11 |
3 files changed, 20 insertions, 15 deletions
diff --git a/doc/release/1.12.0-notes.rst b/doc/release/1.12.0-notes.rst index 9a9a50aa8..7b315b90b 100644 --- a/doc/release/1.12.0-notes.rst +++ b/doc/release/1.12.0-notes.rst @@ -112,6 +112,18 @@ change in implementation some very delicate tests may fail that did not fail before. +``assert_warns`` and ``deprecated`` decorator more specific +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +The ``assert_warns`` function and context manager are now more specific +to the given warning category. This increased specificity leads to them +being handled according to the outer warning settings. This means that +no warning may be raised in cases where a wrong category warning is given +and ignored outside the context. Alternatively the increased specificity +may mean that warnings that were incorrectly ignored will now be shown +or raised. See also the new ``suppress_warnings`` context manager. +The same is true for the ``deprecated`` decorator. + + C API ~~~~~ diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index c0f609883..c191aea5b 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -503,23 +503,21 @@ class TestWarns(unittest.TestCase): warnings.warn("yo", DeprecationWarning) failed = False - filters = sys.modules['warnings'].filters[:] - try: + with warnings.catch_warnings(): + warnings.simplefilter("error", DeprecationWarning) try: - # Should raise an AssertionError + # Should raise a DeprecationWarning assert_warns(UserWarning, f) failed = True - except AssertionError: + except DeprecationWarning: pass - finally: - sys.modules['warnings'].filters = filters if failed: raise AssertionError("wrong warning caught by assert_warn") class TestAssertAllclose(unittest.TestCase): - + def test_simple(self): x = 1e-3 y = 1e-9 diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index c7f4a0aa7..8789dd13c 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -1658,16 +1658,12 @@ class WarningManager(object): @contextlib.contextmanager def _assert_warns_context(warning_class, name=None): __tracebackhide__ = True # Hide traceback for py.test - with warnings.catch_warnings(record=True) as l: - warnings.simplefilter('always') + with suppress_warnings() as sup: + l = sup.record(warning_class) yield if not len(l) > 0: name_str = " when calling %s" % name if name is not None else "" raise AssertionError("No warning raised" + name_str) - if not l[0].category is warning_class: - name_str = "%s " % name if name is not None else "" - raise AssertionError("First warning %sis not a %s (is %s)" - % (name_str, warning_class, l[0])) def assert_warns(warning_class, *args, **kwargs): @@ -1676,8 +1672,7 @@ def assert_warns(warning_class, *args, **kwargs): A warning of class warning_class should be thrown by the callable when invoked with arguments args and keyword arguments kwargs. - If a different type of warning is thrown, it will not be caught, and the - test case will be deemed to have suffered an error. + If a different type of warning is thrown, it will not be caught. If called with all arguments other than the warning class omitted, may be used as a context manager: |