From 8679cb34db6c0853069e56987c123d80d17ac813 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 7 Jan 2023 15:14:26 -0600 Subject: [PATCH 1/2] GH-100805: Support numpy.array() in random.choice(). --- Lib/random.py | 5 ++++- Lib/test/test_random.py | 15 +++++++++++++++ ...2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst diff --git a/Lib/random.py b/Lib/random.py index e60b7294b6dd6a..1c9e1a48b6626b 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -336,7 +336,10 @@ def randint(self, a, b): def choice(self, seq): """Choose a random element from a non-empty sequence.""" - if not seq: + + # As an accommodation for NumPy, we don't use "if not seq" + # because bool(numpy.array()) raises a ValueError. + if not len(seq): raise IndexError('Cannot choose from an empty sequence') return seq[self._randbelow(len(seq))] diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 67de54c7db8b13..50bea7be6d54c7 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -111,6 +111,21 @@ def test_choice(self): 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 diff --git a/Misc/NEWS.d/next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst b/Misc/NEWS.d/next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst new file mode 100644 index 00000000000000..be4813f0d51379 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst @@ -0,0 +1,2 @@ +Modify `random.choice()` implementation to once again work with NumPy +arrays. From 5da9a8f2079261f0a47baa2545fe1783b01345ef Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 7 Jan 2023 16:16:31 -0600 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst Co-authored-by: Alex Waygood --- .../next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst b/Misc/NEWS.d/next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst index be4813f0d51379..4424d7cff21bd2 100644 --- a/Misc/NEWS.d/next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst +++ b/Misc/NEWS.d/next/Library/2023-01-07-15-13-47.gh-issue-100805.05rBz9.rst @@ -1,2 +1,2 @@ -Modify `random.choice()` implementation to once again work with NumPy +Modify :func:`random.choice` implementation to once again work with NumPy arrays.