Skip to content

TST: Suppressed warnings #7099

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 13 commits into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all 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: 21 additions & 2 deletions doc/release/1.12.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ previous implementation used in ``assert_array_almost_equal``. Due to the
change in implementation some very delicate tests may fail that did not
fail before.

``NoseTester`` behaviour of warnings during testing
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When ``raise_warnings="develop"`` is given, all uncaught warnings will now
be considered a test failure. Previously only selected ones were raised.
Warnings which are not caught or raised (mostly when in release mode)
will be shown once during the test cycle similar to the default python
settings.

``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
~~~~~
Expand Down Expand Up @@ -181,8 +200,8 @@ precision.
New array creation function ``geomspace`` added
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The new function ``geomspace`` generates a geometric sequence. It is similar
to ``logspace``, but with start and stop specified directly:
``geomspace(start, stop)`` behaves the same as
to ``logspace``, but with start and stop specified directly:
``geomspace(start, stop)`` behaves the same as
``logspace(log10(start), log10(stop))``.

New context manager for testing warnings
Expand Down
9 changes: 6 additions & 3 deletions numpy/core/arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from . import numerictypes as _nt
from .umath import maximum, minimum, absolute, not_equal, isnan, isinf
from .multiarray import (array, format_longfloat, datetime_as_string,
datetime_data)
datetime_data, dtype)
from .fromnumeric import ravel
from .numeric import asarray

Expand Down Expand Up @@ -734,7 +734,9 @@ class TimedeltaFormat(object):
def __init__(self, data):
if data.dtype.kind == 'm':
nat_value = array(['NaT'], dtype=data.dtype)[0]
v = data[not_equal(data, nat_value)].view('i8')
int_dtype = dtype(data.dtype.byteorder + 'i8')
int_view = data.view(int_dtype)
v = int_view[not_equal(int_view, nat_value.view(int_dtype))]
Copy link
Member

Choose a reason for hiding this comment

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

I assume the original was issuing a warning or some such?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, same, NAT Future Warning (NAT equality, could add more comments maybe).

if len(v) > 0:
# Max str length of non-NaT elements
max_str_len = max(len(str(maximum.reduce(v))),
Expand All @@ -748,7 +750,8 @@ def __init__(self, data):
self._nat = "'NaT'".rjust(max_str_len)

def __call__(self, x):
if x + 1 == x:
# TODO: After NAT == NAT deprecation should be simplified:
if (x + 1).view('i8') == x.view('i8'):
Copy link
Member

Choose a reason for hiding this comment

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

Needed to avoid warning?

Copy link
Member Author

Choose a reason for hiding this comment

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

NAT comparisons give a warning, and they should not while printing in any case. Actually, I am not 100% if you can simplify it currently easily, we may need np.isnat or np.isnan to support datetimes.

return self._nat
else:
return self.format % x.astype('i8')
72 changes: 41 additions & 31 deletions numpy/core/tests/test_datetime.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from __future__ import division, absolute_import, print_function

import pickle
import warnings

import numpy
import numpy as np
import datetime
from numpy.compat import asbytes
from numpy.testing import (
TestCase, run_module_suite, assert_, assert_equal, assert_raises,
assert_warns, dec
assert_warns, dec, suppress_warnings
)

# Use pytz to test out various time zones if available
Expand Down Expand Up @@ -129,10 +128,11 @@ def test_compare_generic_nat(self):
# regression tests for GH6452
assert_equal(np.datetime64('NaT'),
np.datetime64('2000') + np.timedelta64('NaT'))
# nb. we may want to make NaT != NaT true in the future; this test
# verifies the existing behavior (and that it should not warn)
assert_(np.datetime64('NaT') == np.datetime64('NaT', 'us'))
assert_(np.datetime64('NaT', 'us') == np.datetime64('NaT'))
# nb. we may want to make NaT != NaT true in the future
with suppress_warnings() as sup:
sup.filter(FutureWarning, ".*NAT ==")
assert_(np.datetime64('NaT') == np.datetime64('NaT', 'us'))
assert_(np.datetime64('NaT', 'us') == np.datetime64('NaT'))

def test_datetime_scalar_construction(self):
# Construct with different units
Expand Down Expand Up @@ -572,6 +572,12 @@ def test_timedelta_array_str(self):
a = np.array([-1, 'NaT', 1234567], dtype='m')
assert_equal(str(a), "[ -1 'NaT' 1234567]")

# Test with other byteorder:
a = np.array([-1, 'NaT', 1234567], dtype='>m')
assert_equal(str(a), "[ -1 'NaT' 1234567]")
a = np.array([-1, 'NaT', 1234567], dtype='<m')
assert_equal(str(a), "[ -1 'NaT' 1234567]")

def test_pickle(self):
# Check that pickle roundtripping works
dt = np.dtype('M8[7D]')
Expand Down Expand Up @@ -989,8 +995,8 @@ def test_datetime_multiply(self):
assert_raises(TypeError, np.multiply, 1.5, dta)

# NaTs
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
with suppress_warnings() as sup:
sup.filter(RuntimeWarning, "invalid value encountered in multiply")
nat = np.timedelta64('NaT')
def check(a, b, res):
assert_equal(a * b, res)
Expand Down Expand Up @@ -1053,8 +1059,8 @@ def test_datetime_divide(self):
assert_raises(TypeError, np.divide, 1.5, dta)

# NaTs
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
with suppress_warnings() as sup:
sup.filter(RuntimeWarning, r".*encountered in true\_divide")
nat = np.timedelta64('NaT')
for tp in (int, float):
assert_equal(np.timedelta64(1) / tp(0), nat)
Expand Down Expand Up @@ -1092,27 +1098,31 @@ def test_datetime_compare_nat(self):
td_nat = np.timedelta64('NaT', 'h')
td_other = np.timedelta64(1, 'h')

for op in [np.equal, np.less, np.less_equal,
np.greater, np.greater_equal]:
if op(dt_nat, dt_nat):
assert_warns(FutureWarning, op, dt_nat, dt_nat)
if op(dt_nat, dt_other):
assert_warns(FutureWarning, op, dt_nat, dt_other)
if op(dt_other, dt_nat):
assert_warns(FutureWarning, op, dt_other, dt_nat)
if op(td_nat, td_nat):
assert_warns(FutureWarning, op, td_nat, td_nat)
if op(td_nat, td_other):
assert_warns(FutureWarning, op, td_nat, td_other)
if op(td_other, td_nat):
assert_warns(FutureWarning, op, td_other, td_nat)

assert_warns(FutureWarning, np.not_equal, dt_nat, dt_nat)
assert_(np.not_equal(dt_nat, dt_other))
assert_(np.not_equal(dt_other, dt_nat))
assert_warns(FutureWarning, np.not_equal, td_nat, td_nat)
assert_(np.not_equal(td_nat, td_other))
assert_(np.not_equal(td_other, td_nat))
with suppress_warnings() as sup:
# The assert warns contexts will again see the warning:
sup.filter(FutureWarning, ".*NAT")

for op in [np.equal, np.less, np.less_equal,
np.greater, np.greater_equal]:
if op(dt_nat, dt_nat):
assert_warns(FutureWarning, op, dt_nat, dt_nat)
if op(dt_nat, dt_other):
assert_warns(FutureWarning, op, dt_nat, dt_other)
if op(dt_other, dt_nat):
assert_warns(FutureWarning, op, dt_other, dt_nat)
if op(td_nat, td_nat):
assert_warns(FutureWarning, op, td_nat, td_nat)
if op(td_nat, td_other):
assert_warns(FutureWarning, op, td_nat, td_other)
if op(td_other, td_nat):
assert_warns(FutureWarning, op, td_other, td_nat)

assert_warns(FutureWarning, np.not_equal, dt_nat, dt_nat)
assert_(np.not_equal(dt_nat, dt_other))
assert_(np.not_equal(dt_other, dt_nat))
assert_warns(FutureWarning, np.not_equal, td_nat, td_nat)
assert_(np.not_equal(td_nat, td_other))
assert_(np.not_equal(td_other, td_nat))

def test_datetime_minmax(self):
# The metadata of the result should become the GCD
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ def foo():
warnings.warn("foo", category=DeprecationWarning)

test_case_instance.assert_deprecated(foo)
test_case_instance.tearDown()


if __name__ == "__main__":
run_module_suite()
9 changes: 4 additions & 5 deletions numpy/core/tests/test_einsum.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import division, absolute_import, print_function

import warnings

import numpy as np
from numpy.testing import (
TestCase, run_module_suite, assert_, assert_equal, assert_array_equal,
assert_raises
assert_raises, suppress_warnings
)


class TestEinSum(TestCase):
def test_einsum_errors(self):
# Need enough arguments
Expand Down Expand Up @@ -282,8 +281,8 @@ def check_einsum_sums(self, dtype):
assert_equal(np.einsum(a, [0], b, [1]), np.outer(a, b))

# Suppress the complex warnings for the 'as f8' tests
with warnings.catch_warnings():
warnings.simplefilter('ignore', np.ComplexWarning)
with suppress_warnings() as sup:
sup.filter(np.ComplexWarning)

# matvec(a,b) / a.dot(b) where a is matrix, b is vector
for n in range(1, 17):
Expand Down
8 changes: 5 additions & 3 deletions numpy/core/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
typecodes, arange, isnan, ndarray, sqrt)
from numpy.testing import (
TestCase, run_module_suite, assert_, assert_equal, assert_raises,
assert_array_equal, assert_allclose
assert_array_equal, assert_allclose, suppress_warnings
)


Expand Down Expand Up @@ -205,8 +205,10 @@ def test_basic(self):
def test_corner(self):
y = list(linspace(0, 1, 1))
assert_(y == [0.0], y)
y = list(linspace(0, 1, 2.5))
assert_(y == [0.0, 1.0])
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, ".*safely interpreted as an integer")
y = list(linspace(0, 1, 2.5))
assert_(y == [0.0, 1.0])

def test_type(self):
t1 = linspace(0, 1, 0).dtype
Expand Down
16 changes: 9 additions & 7 deletions numpy/core/tests/test_memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from numpy import arange, allclose, asarray
from numpy.testing import (
TestCase, run_module_suite, assert_, assert_equal, assert_array_equal,
dec
dec, suppress_warnings
)

class TestMemmap(TestCase):
Expand Down Expand Up @@ -146,13 +146,15 @@ def test_ufunc_return_ndarray(self):
fp = memmap(self.tmpfp, dtype=self.dtype, shape=self.shape)
fp[:] = self.data

for unary_op in [sum, average, product]:
result = unary_op(fp)
assert_(isscalar(result))
assert_(result.__class__ is self.data[0, 0].__class__)
with suppress_warnings() as sup:
sup.filter(FutureWarning, "np.average currently does not preserve")
for unary_op in [sum, average, product]:
result = unary_op(fp)
assert_(isscalar(result))
assert_(result.__class__ is self.data[0, 0].__class__)

assert_(unary_op(fp, axis=0).__class__ is ndarray)
assert_(unary_op(fp, axis=1).__class__ is ndarray)
assert_(unary_op(fp, axis=0).__class__ is ndarray)
assert_(unary_op(fp, axis=1).__class__ is ndarray)

for binary_op in [add, subtract, multiply]:
assert_(binary_op(fp, self.data).__class__ is ndarray)
Expand Down
26 changes: 13 additions & 13 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
TestCase, run_module_suite, assert_, assert_raises,
assert_equal, assert_almost_equal, assert_array_equal,
assert_array_almost_equal, assert_allclose, IS_PYPY, HAS_REFCOUNT,
assert_array_less, runstring, dec, SkipTest, temppath
assert_array_less, runstring, dec, SkipTest, temppath, suppress_warnings
)

# Need to test an object that does not fully implement math interface
Expand Down Expand Up @@ -826,17 +826,17 @@ def test_subarray_comparison(self):
# This comparison invokes deprecated behaviour, and will probably
# start raising an error eventually. What we really care about in this
# test is just that it doesn't return True.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
with suppress_warnings() as sup:
sup.filter(FutureWarning, "elementwise == comparison failed")
assert_equal(x == y, False)

x = np.zeros((1,), dtype=[('a', ('f4', (2, 1))), ('b', 'i1')])
y = np.zeros((1,), dtype=[('a', ('f4', (2,))), ('b', 'i1')])
# This comparison invokes deprecated behaviour, and will probably
# start raising an error eventually. What we really care about in this
# test is just that it doesn't return True.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
with suppress_warnings() as sup:
sup.filter(FutureWarning, "elementwise == comparison failed")
assert_equal(x == y, False)

# Check that structured arrays that are different only in
Expand Down Expand Up @@ -3818,11 +3818,11 @@ def test_unbuffered_fromfile(self):
def fail(*args, **kwargs):
raise io.IOError('Can not tell or seek')

f = io.open(self.filename, 'rb', buffering=0)
f.seek = fail
f.tell = fail
y = np.fromfile(self.filename, dtype=self.dtype)
assert_array_equal(y, self.x.flat)
with io.open(self.filename, 'rb', buffering=0) as f:
f.seek = fail
f.tell = fail
y = np.fromfile(self.filename, dtype=self.dtype)
assert_array_equal(y, self.x.flat)

def test_largish_file(self):
# check the fallocate path on files > 16MB
Expand Down Expand Up @@ -6039,11 +6039,11 @@ def test_relaxed_strides(self):
class TestArrayAttributeDeletion(object):

def test_multiarray_writable_attributes_deletion(self):
"""ticket #2046, should not seqfault, raise AttributeError"""
# ticket #2046, should not seqfault, raise AttributeError
a = np.ones(2)
attr = ['shape', 'strides', 'data', 'dtype', 'real', 'imag', 'flat']
with warnings.catch_warnings():
warnings.simplefilter('ignore')
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, "Assigning the 'data' attribute")
for s in attr:
assert_raises(AttributeError, delattr, a, s)

Expand Down
9 changes: 3 additions & 6 deletions numpy/core/tests/test_nditer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from __future__ import division, absolute_import, print_function

import sys
import warnings

import numpy as np
from numpy import array, arange, nditer, all
from numpy.compat import asbytes, sixu
from numpy.core.multiarray_tests import test_nditer_too_large
from numpy.testing import (
run_module_suite, assert_, assert_equal, assert_array_equal,
assert_raises, dec, HAS_REFCOUNT
assert_raises, dec, HAS_REFCOUNT, suppress_warnings
)


Expand Down Expand Up @@ -1624,8 +1623,8 @@ def test_iter_buffered_cast_byteswapped():

assert_equal(a, 2*np.arange(10, dtype='f4'))

try:
warnings.simplefilter("ignore", np.ComplexWarning)
with suppress_warnings() as sup:
sup.filter(np.ComplexWarning)

a = np.arange(10, dtype='f8').newbyteorder().byteswap()
i = nditer(a, ['buffered', 'external_loop'],
Expand All @@ -1637,8 +1636,6 @@ def test_iter_buffered_cast_byteswapped():
v[...] *= 2

assert_equal(a, 2*np.arange(10, dtype='f8'))
finally:
warnings.simplefilter("default", np.ComplexWarning)

def test_iter_buffered_cast_byteswapped_complex():
# Test that buffering can handle a cast which requires swap->cast->copy
Expand Down
Loading