From df872b470211b79714603403c721fab0c09ef083 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Wed, 5 Oct 2022 22:00:04 +0200 Subject: [PATCH] TST: Add deprecation tests for out-of-bound pyint conversion deprecation --- numpy/core/tests/test_deprecations.py | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 5c13fcd4fbc7..bd286cc079f2 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -1196,3 +1196,37 @@ def test_deprecated_raised(self, dtype): np.loadtxt(["10.5"], dtype=dtype) except ValueError as e: assert isinstance(e.__cause__, DeprecationWarning) + + +class TestPyIntConversion(_DeprecationTestCase): + message = r".*stop allowing conversion of out-of-bound.*" + + @pytest.mark.parametrize("dtype", np.typecodes["AllInteger"]) + def test_deprecated_scalar(self, dtype): + dtype = np.dtype(dtype) + info = np.iinfo(dtype) + + # Cover the most common creation paths (all end up in the + # same place): + + def scalar(value, dtype): + dtype.type(value) + + def assign(value, dtype): + arr = np.array([0, 0, 0], dtype=dtype) + arr[2] = value + + def create(value, dtype): + np.array([value], dtype=dtype) + + + for creation_func in [scalar, assign, create]: + try: + self.assert_deprecated(lambda: creation_func(info.min - 1, dtype)) + except OverflowError: + pass # OverflowErrors always happened also before and are OK. + + try: + self.assert_deprecated(lambda: creation_func(info.max + 1, dtype)) + except OverflowError: + pass # OverflowErrors always happened also before and are OK.