diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/conftest.py | 17 | ||||
-rw-r--r-- | numpy/core/tests/test_dtype.py | 43 | ||||
-rw-r--r-- | numpy/core/tests/test_half.py | 10 | ||||
-rw-r--r-- | numpy/testing/_private/utils.py | 4 |
4 files changed, 48 insertions, 26 deletions
diff --git a/numpy/conftest.py b/numpy/conftest.py index fd5fdd77d..8aa6587ee 100644 --- a/numpy/conftest.py +++ b/numpy/conftest.py @@ -117,3 +117,20 @@ def add_np(doctest_namespace): @pytest.fixture(autouse=True) def env_setup(monkeypatch): monkeypatch.setenv('PYTHONHASHSEED', '0') + + +@pytest.fixture(params=[True, False]) +def weak_promotion(request): + """ + Fixture to ensure "legacy" promotion state or change it to use the new + weak promotion (plus warning). `old_promotion` should be used as a + parameter in the function. + """ + state = numpy._get_promotion_state() + if request.param: + numpy._set_promotion_state("weak_and_warn") + else: + numpy._set_promotion_state("legacy") + + yield request.param + numpy._set_promotion_state(state) diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 637c2c141..d895459ed 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -11,7 +11,7 @@ from numpy.core._rational_tests import rational from numpy.core._multiarray_tests import create_custom_field_dtype from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_raises, HAS_REFCOUNT, - IS_PYSTON, OLD_PROMOTION) + IS_PYSTON, _OLD_PROMOTION) from numpy.compat import pickle from itertools import permutations import random @@ -1289,29 +1289,33 @@ class TestPromotion: tests are defined in `test_numeric.py` """ @np._no_nep50_warning() - @pytest.mark.parametrize(["other", "expected"], - [(2**16-1, np.complex64), - (2**32-1, np.complex128 if OLD_PROMOTION else np.complex64), - (np.float16(2), np.complex64), - (np.float32(2), np.complex64), - (np.longdouble(2), - np.complex64 if OLD_PROMOTION else np.clongdouble), + @pytest.mark.parametrize(["other", "expected", "expected_weak"], + [(2**16-1, np.complex64, None), + (2**32-1, np.complex128, np.complex64), + (np.float16(2), np.complex64, None), + (np.float32(2), np.complex64, None), + (np.longdouble(2), np.complex64, np.clongdouble), # Base of the double value to sidestep any rounding issues: (np.longdouble(np.nextafter(1.7e308, 0.)), - np.complex128 if OLD_PROMOTION else np.clongdouble), + np.complex128, np.clongdouble), # Additionally use "nextafter" so the cast can't round down: - (np.longdouble(np.nextafter(1.7e308, np.inf)), np.clongdouble), + (np.longdouble(np.nextafter(1.7e308, np.inf)), + np.clongdouble, None), # repeat for complex scalars: - (np.complex64(2), np.complex64), - (np.clongdouble(2), - np.complex64 if OLD_PROMOTION else np.clongdouble), + (np.complex64(2), np.complex64, None), + (np.clongdouble(2), np.complex64, np.clongdouble), # Base of the double value to sidestep any rounding issues: (np.clongdouble(np.nextafter(1.7e308, 0.) * 1j), - np.complex128 if OLD_PROMOTION else np.clongdouble), + np.complex128, np.clongdouble), # Additionally use "nextafter" so the cast can't round down: - (np.clongdouble(np.nextafter(1.7e308, np.inf)), np.clongdouble), + (np.clongdouble(np.nextafter(1.7e308, np.inf)), + np.clongdouble, None), ]) - def test_complex_other_value_based(self, other, expected): + def test_complex_other_value_based(self, + weak_promotion, other, expected, expected_weak): + if weak_promotion and expected_weak is not None: + expected = expected_weak + # This would change if we modify the value based promotion min_complex = np.dtype(np.complex64) @@ -1344,7 +1348,7 @@ class TestPromotion: def test_complex_pyscalar_promote_rational(self): with pytest.raises(TypeError, - match=r".* do not have a common DType"): + match=r".* no common DType exists for the given inputs"): np.result_type(1j, rational) with pytest.raises(TypeError, @@ -1364,11 +1368,12 @@ class TestPromotion: @pytest.mark.parametrize(["other", "expected"], [(1, rational), (1., np.float64)]) @np._no_nep50_warning() - def test_float_int_pyscalar_promote_rational(self, other, expected): + def test_float_int_pyscalar_promote_rational( + self, weak_promotion, other, expected): # Note that rationals are a bit akward as they promote with float64 # or default ints, but not float16 or uint8/int8 (which looks # inconsistent here). The new promotion fixes this (partially?) - if OLD_PROMOTION: + if not weak_promotion: with pytest.raises(TypeError, match=r".* do not have a common DType"): np.result_type(other, rational) diff --git a/numpy/core/tests/test_half.py b/numpy/core/tests/test_half.py index d554943a5..4e9a3c9d6 100644 --- a/numpy/core/tests/test_half.py +++ b/numpy/core/tests/test_half.py @@ -3,7 +3,7 @@ import pytest import numpy as np from numpy import uint16, float16, float32, float64 -from numpy.testing import assert_, assert_equal, OLD_PROMOTION +from numpy.testing import assert_, assert_equal, _OLD_PROMOTION def assert_raises_fpe(strmatch, callable, *args, **kwargs): @@ -452,7 +452,7 @@ class TestHalf: assert_equal(np.ldexp(b, [0, 1, 2, 4, 2]), [-2, 10, 4, 64, 12]) @np._no_nep50_warning() - def test_half_coercion(self): + def test_half_coercion(self, weak_promotion): """Test that half gets coerced properly with the other types""" a16 = np.array((1,), dtype=float16) a32 = np.array((1,), dtype=float32) @@ -462,12 +462,12 @@ class TestHalf: assert np.power(a16, 2).dtype == float16 assert np.power(a16, 2.0).dtype == float16 assert np.power(a16, b16).dtype == float16 - expected_dt = float16 if OLD_PROMOTION else float32 + expected_dt = float32 if weak_promotion else float16 assert np.power(a16, b32).dtype == expected_dt assert np.power(a16, a16).dtype == float16 assert np.power(a16, a32).dtype == float32 - expected_dt = float64 if OLD_PROMOTION else float16 + expected_dt = float16 if weak_promotion else float64 assert np.power(b16, 2).dtype == expected_dt assert np.power(b16, 2.0).dtype == expected_dt assert np.power(b16, b16).dtype, float16 @@ -477,7 +477,7 @@ class TestHalf: assert np.power(a32, a16).dtype == float32 assert np.power(a32, b16).dtype == float32 - expected_dt = float16 if OLD_PROMOTION else float32 + expected_dt = float32 if weak_promotion else float16 assert np.power(b32, a16).dtype == expected_dt assert np.power(b32, b16).dtype == float32 diff --git a/numpy/testing/_private/utils.py b/numpy/testing/_private/utils.py index 4ec42cbb8..d7e218486 100644 --- a/numpy/testing/_private/utils.py +++ b/numpy/testing/_private/utils.py @@ -36,7 +36,7 @@ __all__ = [ 'SkipTest', 'KnownFailureException', 'temppath', 'tempdir', 'IS_PYPY', 'HAS_REFCOUNT', 'suppress_warnings', 'assert_array_compare', 'assert_no_gc_cycles', 'break_cycles', 'HAS_LAPACK64', 'IS_PYSTON', - 'OLD_PROMOTION' + '_OLD_PROMOTION' ] @@ -53,7 +53,7 @@ IS_PYSTON = hasattr(sys, "pyston_version_info") HAS_REFCOUNT = getattr(sys, 'getrefcount', None) is not None and not IS_PYSTON HAS_LAPACK64 = numpy.linalg.lapack_lite._ilp64 -OLD_PROMOTION = np._get_promotion_state() == 'legacy' +_OLD_PROMOTION = lambda: np._get_promotion_state() == 'legacy' def import_nose(): |