mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
GH-100805: Support numpy.array() in random.choice(). (GH-100830)
(cherry picked from commit 9a68ff12c3
)
Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
This commit is contained in:
parent
6c7e32f6a8
commit
6184b800ee
3 changed files with 21 additions and 1 deletions
|
@ -111,6 +111,21 @@ class TestBasicOps:
|
|||
self.assertEqual(choice([50]), 50)
|
||||
self.assertIn(choice([25, 75]), [25, 75])
|
||||
|
||||
def test_choice_with_numpy(self):
|
||||
# Accommodation for NumPy arrays which have disabled __bool__().
|
||||
# See: https://github.com/python/cpython/issues/100805
|
||||
choice = self.gen.choice
|
||||
|
||||
class NA(list):
|
||||
"Simulate numpy.array() behavior"
|
||||
def __bool__(self):
|
||||
raise RuntimeError
|
||||
|
||||
with self.assertRaises(IndexError):
|
||||
choice(NA([]))
|
||||
self.assertEqual(choice(NA([50])), 50)
|
||||
self.assertIn(choice(NA([25, 75])), [25, 75])
|
||||
|
||||
def test_sample(self):
|
||||
# For the entire allowable range of 0 <= k <= N, validate that
|
||||
# the sample is of the correct length and contains only unique items
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue