diff options
| author | Matti Picus <matti.picus@gmail.com> | 2021-02-22 10:18:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-22 10:18:12 +0200 |
| commit | 30678f57d5f4c68e4a0590fda104b32895a216b8 (patch) | |
| tree | b3e3b714f8feba23d9b4113b445d4e3946040f4d | |
| parent | a8f38c5f49f92be3897888d2b02cb734a2f32187 (diff) | |
| parent | dfdff63cd6c53f1fd1d1c4a9754332887b746e7e (diff) | |
| download | numpy-30678f57d5f4c68e4a0590fda104b32895a216b8.tar.gz | |
Merge pull request #18443 from seberg/fix-stacklevel-shuffle
BUG: fix stacklevel in warning within random.shuffle
| -rw-r--r-- | numpy/random/_generator.pyx | 11 | ||||
| -rw-r--r-- | numpy/random/mtrand.pyx | 11 | ||||
| -rw-r--r-- | numpy/random/tests/test_random.py | 12 |
3 files changed, 24 insertions, 10 deletions
diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index a7d98e2ed..8d440d4f7 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -4445,11 +4445,12 @@ cdef class Generator: if not isinstance(x, Sequence): # See gh-18206. We may decide to deprecate here in the future. warnings.warn( - "`x` isn't a recognized object; `shuffle` is not guaranteed " - "to behave correctly. E.g., non-numpy array/tensor objects " - "with view semantics may contain duplicates after shuffling.", - UserWarning, stacklevel=2 - ) + f"you are shuffling a '{type(x).__name__}' object " + "which is not a subclass of 'Sequence'; " + "`shuffle` is not guaranteed to behave correctly. " + "E.g., non-numpy array/tensor objects with view semantics " + "may contain duplicates after shuffling.", + UserWarning, stacklevel=1) # Cython does not add a level if axis != 0: raise NotImplementedError("Axis argument is only supported " diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index df8d7e380..a7436aa39 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -4476,11 +4476,12 @@ cdef class RandomState: if not isinstance(x, Sequence): # See gh-18206. We may decide to deprecate here in the future. warnings.warn( - "`x` isn't a recognized object; `shuffle` is not guaranteed " - "to behave correctly. E.g., non-numpy array/tensor objects " - "with view semantics may contain duplicates after shuffling.", - UserWarning, stacklevel=2 - ) + f"you are shuffling a '{type(x).__name__}' object " + "which is not a subclass of 'Sequence'; " + "`shuffle` is not guaranteed to behave correctly. " + "E.g., non-numpy array/tensor objects with view semantics " + "may contain duplicates after shuffling.", + UserWarning, stacklevel=1) # Cython does not add a level with self.lock: for i in reversed(range(1, n)): diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 5f8b39ef9..a0c72b419 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -1,5 +1,7 @@ import warnings +import pytest + import numpy as np from numpy.testing import ( assert_, assert_raises, assert_equal, assert_warns, @@ -510,6 +512,16 @@ class TestRandomDist: assert_equal( sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask])) + @pytest.mark.parametrize("random", + [np.random, np.random.RandomState(), np.random.default_rng()]) + def test_shuffle_untyped_warning(self, random): + # Create a dict works like a sequence but isn't one + values = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6} + with pytest.warns(UserWarning, + match="you are shuffling a 'dict' object") as rec: + random.shuffle(values) + assert "test_random" in rec[0].filename + def test_shuffle_memoryview(self): # gh-18273 # allow graceful handling of memoryviews |
