summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_random.py
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2023-01-08 19:40:35 (GMT)
committerGitHub <noreply@github.com>2023-01-08 19:40:35 (GMT)
commit9a68ff12c3e647a4f8dd935919ae296593770a6b (patch)
treead74d7cc798d2ecd2bd87d7a09317d7875cac51a /Lib/test/test_random.py
parent87d3bd0e02cddc415a42573052110eb9301d2c3d (diff)
downloadcpython-9a68ff12c3e647a4f8dd935919ae296593770a6b.zip
cpython-9a68ff12c3e647a4f8dd935919ae296593770a6b.tar.gz
cpython-9a68ff12c3e647a4f8dd935919ae296593770a6b.tar.bz2
GH-100805: Support numpy.array() in random.choice(). (GH-100830)
Diffstat (limited to 'Lib/test/test_random.py')
-rw-r--r--Lib/test/test_random.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index 67de54c..50bea7b 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -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