diff options
author | scoder <stefan_ml@behnel.de> | 2023-05-04 09:29:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-04 09:29:53 +0200 |
commit | 442c8f48d3146ec32c7d5387310e171276cf10ac (patch) | |
tree | d8911d1a64e384b7955d3fc09a07edd218a9f1ee /numpy/testing/tests/test_utils.py | |
parent | 3e4a6cba2da27bbe2a6e12c163238e503c9f6a07 (diff) | |
parent | 9163e933df91b516b6f0c7a9ba8ad1750e642f37 (diff) | |
download | numpy-442c8f48d3146ec32c7d5387310e171276cf10ac.tar.gz |
Merge branch 'main' into cython3_noexcept
Diffstat (limited to 'numpy/testing/tests/test_utils.py')
-rw-r--r-- | numpy/testing/tests/test_utils.py | 181 |
1 files changed, 99 insertions, 82 deletions
diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index 6a6cc664a..0aaa508ee 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -8,13 +8,12 @@ import weakref import numpy as np from numpy.testing import ( assert_equal, assert_array_equal, assert_almost_equal, - assert_array_almost_equal, assert_array_less, build_err_msg, raises, + assert_array_almost_equal, assert_array_less, build_err_msg, assert_raises, assert_warns, assert_no_warnings, assert_allclose, assert_approx_equal, assert_array_almost_equal_nulp, assert_array_max_ulp, clear_and_catch_warnings, suppress_warnings, assert_string_equal, assert_, tempdir, temppath, assert_no_gc_cycles, HAS_REFCOUNT ) -from numpy.core.overrides import ARRAY_FUNCTION_ENABLED class _GenericTest: @@ -65,7 +64,7 @@ class _GenericTest: class TestArrayEqual(_GenericTest): - def setup(self): + def setup_method(self): self._assert_func = assert_array_equal def test_generic_rank1(self): @@ -151,14 +150,13 @@ class TestArrayEqual(_GenericTest): self._test_equal(a, b) - c = np.empty(2, [('floupipi', float), ('floupa', float)]) + c = np.empty(2, [('floupipi', float), + ('floupi', float), ('floupa', float)]) c['floupipi'] = a['floupi'].copy() c['floupa'] = a['floupa'].copy() - with suppress_warnings() as sup: - l = sup.record(FutureWarning, message="elementwise == ") + with pytest.raises(TypeError): self._test_not_equal(c, b) - assert_equal(len(l), 1) def test_masked_nan_inf(self): # Regression test for gh-11121 @@ -192,8 +190,6 @@ class TestArrayEqual(_GenericTest): self._test_not_equal(a, b) self._test_not_equal(b, a) - @pytest.mark.skipif( - not ARRAY_FUNCTION_ENABLED, reason='requires __array_function__') def test_subclass_that_does_not_implement_npall(self): class MyArray(np.ndarray): def __array_function__(self, *args, **kwargs): @@ -207,6 +203,51 @@ class TestArrayEqual(_GenericTest): self._test_not_equal(a, b) self._test_not_equal(b, a) + def test_suppress_overflow_warnings(self): + # Based on issue #18992 + with pytest.raises(AssertionError): + with np.errstate(all="raise"): + np.testing.assert_array_equal( + np.array([1, 2, 3], np.float32), + np.array([1, 1e-40, 3], np.float32)) + + def test_array_vs_scalar_is_equal(self): + """Test comparing an array with a scalar when all values are equal.""" + a = np.array([1., 1., 1.]) + b = 1. + + self._test_equal(a, b) + + def test_array_vs_scalar_not_equal(self): + """Test comparing an array with a scalar when not all values equal.""" + a = np.array([1., 2., 3.]) + b = 1. + + self._test_not_equal(a, b) + + def test_array_vs_scalar_strict(self): + """Test comparing an array with a scalar with strict option.""" + a = np.array([1., 1., 1.]) + b = 1. + + with pytest.raises(AssertionError): + assert_array_equal(a, b, strict=True) + + def test_array_vs_array_strict(self): + """Test comparing two arrays with strict option.""" + a = np.array([1., 1., 1.]) + b = np.array([1., 1., 1.]) + + assert_array_equal(a, b, strict=True) + + def test_array_vs_float_array_strict(self): + """Test comparing two arrays with strict option.""" + a = np.array([1, 1, 1]) + b = np.array([1., 1., 1.]) + + with pytest.raises(AssertionError): + assert_array_equal(a, b, strict=True) + class TestBuildErrorMessage: @@ -255,7 +296,7 @@ class TestBuildErrorMessage: class TestEqual(TestArrayEqual): - def setup(self): + def setup_method(self): self._assert_func = assert_equal def test_nan_items(self): @@ -350,7 +391,7 @@ class TestEqual(TestArrayEqual): class TestArrayAlmostEqual(_GenericTest): - def setup(self): + def setup_method(self): self._assert_func = assert_array_almost_equal def test_closeness(self): @@ -434,10 +475,10 @@ class TestArrayAlmostEqual(_GenericTest): # (which, e.g., astropy Quantity cannot usefully do). See gh-8452. class MyArray(np.ndarray): def __eq__(self, other): - return super(MyArray, self).__eq__(other).view(np.ndarray) + return super().__eq__(other).view(np.ndarray) def __lt__(self, other): - return super(MyArray, self).__lt__(other).view(np.ndarray) + return super().__lt__(other).view(np.ndarray) def all(self, *args, **kwargs): raise NotImplementedError @@ -448,7 +489,7 @@ class TestArrayAlmostEqual(_GenericTest): class TestAlmostEqual(_GenericTest): - def setup(self): + def setup_method(self): self._assert_func = assert_almost_equal def test_closeness(self): @@ -585,10 +626,10 @@ class TestAlmostEqual(_GenericTest): # (which, e.g., astropy Quantity cannot usefully do). See gh-8452. class MyArray(np.ndarray): def __eq__(self, other): - return super(MyArray, self).__eq__(other).view(np.ndarray) + return super().__eq__(other).view(np.ndarray) def __lt__(self, other): - return super(MyArray, self).__lt__(other).view(np.ndarray) + return super().__lt__(other).view(np.ndarray) def all(self, *args, **kwargs): raise NotImplementedError @@ -599,7 +640,7 @@ class TestAlmostEqual(_GenericTest): class TestApproxEqual: - def setup(self): + def setup_method(self): self._assert_func = assert_approx_equal def test_simple_0d_arrays(self): @@ -642,7 +683,7 @@ class TestApproxEqual: class TestArrayAssertLess: - def setup(self): + def setup_method(self): self._assert_func = assert_array_less def test_simple_arrays(self): @@ -749,41 +790,6 @@ class TestArrayAssertLess: self._assert_func(-ainf, x) -@pytest.mark.skip(reason="The raises decorator depends on Nose") -class TestRaises: - - def setup(self): - class MyException(Exception): - pass - - self.e = MyException - - def raises_exception(self, e): - raise e - - def does_not_raise_exception(self): - pass - - def test_correct_catch(self): - raises(self.e)(self.raises_exception)(self.e) # raises? - - def test_wrong_exception(self): - try: - raises(self.e)(self.raises_exception)(RuntimeError) # raises? - except RuntimeError: - return - else: - raise AssertionError("should have caught RuntimeError") - - def test_catch_no_raise(self): - try: - raises(self.e)(self.does_not_raise_exception)() # raises? - except AssertionError: - return - else: - raise AssertionError("should have raised an AssertionError") - - class TestWarns: def test_warn(self): @@ -904,6 +910,25 @@ class TestAssertAllclose: msg = str(exc_info.value) assert_('Max relative difference: 0.5' in msg) + def test_timedelta(self): + # see gh-18286 + a = np.array([[1, 2, 3, "NaT"]], dtype="m8[ns]") + assert_allclose(a, a) + + def test_error_message_unsigned(self): + """Check the the message is formatted correctly when overflow can occur + (gh21768)""" + # Ensure to test for potential overflow in the case of: + # x - y + # and + # y - x + x = np.asarray([0, 1, 8], dtype='uint8') + y = np.asarray([4, 4, 4], dtype='uint8') + with pytest.raises(AssertionError) as exc_info: + assert_allclose(x, y, atol=3) + msgs = str(exc_info.value).split('\n') + assert_equal(msgs[4], 'Max absolute difference: 4') + class TestArrayAlmostEqualNulp: @@ -1211,7 +1236,7 @@ class TestStringEqual: lambda: assert_string_equal("aaa", "a+b")) -def assert_warn_len_equal(mod, n_in_context, py34=None, py37=None): +def assert_warn_len_equal(mod, n_in_context): try: mod_warns = mod.__warningregistry__ except AttributeError: @@ -1225,26 +1250,15 @@ def assert_warn_len_equal(mod, n_in_context, py34=None, py37=None): mod_warns = {} num_warns = len(mod_warns) - # Python 3.4 appears to clear any pre-existing warnings of the same type, - # when raising warnings inside a catch_warnings block. So, there is a - # warning generated by the tests within the context manager, but no - # previous warnings. + if 'version' in mod_warns: # Python 3 adds a 'version' entry to the registry, # do not count it. num_warns -= 1 - # Behavior of warnings is Python version dependent. Adjust the - # expected result to compensate. In particular, Python 3.7 does - # not make an entry for ignored warnings. - if sys.version_info[:2] >= (3, 7): - if py37 is not None: - n_in_context = py37 - elif sys.version_info[:2] >= (3, 4): - if py34 is not None: - n_in_context = py34 assert_equal(num_warns, n_in_context) + def test_warn_len_equal_call_scenarios(): # assert_warn_len_equal is called under # varying circumstances depending on serial @@ -1293,24 +1307,28 @@ def test_clear_and_catch_warnings(): warnings.simplefilter('ignore') warnings.warn('Some warning') assert_equal(my_mod.__warningregistry__, {}) - # Without specified modules, don't clear warnings during context - # Python 3.7 catch_warnings doesn't make an entry for 'ignore'. + # Without specified modules, don't clear warnings during context. + # catch_warnings doesn't make an entry for 'ignore'. with clear_and_catch_warnings(): warnings.simplefilter('ignore') warnings.warn('Some warning') - assert_warn_len_equal(my_mod, 1, py37=0) + assert_warn_len_equal(my_mod, 0) + + # Manually adding two warnings to the registry: + my_mod.__warningregistry__ = {'warning1': 1, + 'warning2': 2} + # Confirm that specifying module keeps old warning, does not add new with clear_and_catch_warnings(modules=[my_mod]): warnings.simplefilter('ignore') warnings.warn('Another warning') - assert_warn_len_equal(my_mod, 1, py37=0) - # Another warning, no module spec does add to warnings dict, except on - # Python 3.4 (see comments in `assert_warn_len_equal`) - # Python 3.7 catch_warnings doesn't make an entry for 'ignore'. + assert_warn_len_equal(my_mod, 2) + + # Another warning, no module spec it clears up registry with clear_and_catch_warnings(): warnings.simplefilter('ignore') warnings.warn('Another warning') - assert_warn_len_equal(my_mod, 2, py34=1, py37=0) + assert_warn_len_equal(my_mod, 0) def test_suppress_warnings_module(): @@ -1339,7 +1357,7 @@ def test_suppress_warnings_module(): # got filtered) assert_equal(len(sup.log), 1) assert_equal(sup.log[0].message.args[0], "Some warning") - assert_warn_len_equal(my_mod, 0, py37=0) + assert_warn_len_equal(my_mod, 0) sup = suppress_warnings() # Will have to be changed if apply_along_axis is moved: sup.filter(module=my_mod) @@ -1352,12 +1370,12 @@ def test_suppress_warnings_module(): warnings.warn('Some warning') assert_warn_len_equal(my_mod, 0) - # Without specified modules, don't clear warnings during context - # Python 3.7 does not add ignored warnings. + # Without specified modules with suppress_warnings(): warnings.simplefilter('ignore') warnings.warn('Some warning') - assert_warn_len_equal(my_mod, 1, py37=0) + assert_warn_len_equal(my_mod, 0) + def test_suppress_warnings_type(): # Initial state of module, no warnings @@ -1380,12 +1398,11 @@ def test_suppress_warnings_type(): warnings.warn('Some warning') assert_warn_len_equal(my_mod, 0) - # Without specified modules, don't clear warnings during context - # Python 3.7 does not add ignored warnings. + # Without specified modules with suppress_warnings(): warnings.simplefilter('ignore') warnings.warn('Some warning') - assert_warn_len_equal(my_mod, 1, py37=0) + assert_warn_len_equal(my_mod, 0) def test_suppress_warnings_decorate_no_record(): |