summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2021-03-07 12:00:50 -0700
committerGitHub <noreply@github.com>2021-03-07 12:00:50 -0700
commit4af35a75b313411753b037f040d0eaf234321c5b (patch)
tree9b71705444034b2a9d6a9b75a6883c1a5c4d03e1
parent6c00ecd490384abd407bf2da2ed70fc353a3b173 (diff)
parent54572d275dd5d12b564ab444958af7a0aa91e701 (diff)
downloadnumpy-4af35a75b313411753b037f040d0eaf234321c5b.tar.gz
Merge pull request #18439 from WarrenWeckesser/random-choice-exc
MAINT: random: Use 'from exc' when raising a ValueError in choice.
-rw-r--r--numpy/random/_generator.pyx10
1 files changed, 6 insertions, 4 deletions
diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx
index cd950b3fa..ee2dcb24b 100644
--- a/numpy/random/_generator.pyx
+++ b/numpy/random/_generator.pyx
@@ -700,20 +700,22 @@ cdef class Generator:
cdef uint64_t set_size, mask
cdef uint64_t[::1] hash_set
# Format and Verify input
+ a_original = a
a = np.array(a, copy=False)
if a.ndim == 0:
try:
# __index__ must return an integer by python rules.
pop_size = operator.index(a.item())
- except TypeError:
- raise ValueError("a must an array or an integer")
+ except TypeError as exc:
+ raise ValueError("a must be a sequence or an integer, "
+ f"not {type(a_original)}") from exc
if pop_size <= 0 and np.prod(size) != 0:
- raise ValueError("a must be a positive integer unless no"
+ raise ValueError("a must be a positive integer unless no "
"samples are taken")
else:
pop_size = a.shape[axis]
if pop_size == 0 and np.prod(size) != 0:
- raise ValueError("a cannot be empty unless no samples are"
+ raise ValueError("a cannot be empty unless no samples are "
"taken")
if p is not None: