-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Move error test to function that needs it. Improve error message. #30008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Update to 15 March
@@ -338,7 +334,8 @@ def randint(self, a, b): | |||
|
|||
def choice(self, seq): | |||
"""Choose a random element from a non-empty sequence.""" | |||
# raises IndexError if seq is empty | |||
if not seq: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhettinger : This change prevents operation on numpy arrays: if not seq
will produce a ValueError
(ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
). While it might be perverse to use random
from stdlib on a numpy array, such code exists in the wild, such as in the test suite of scikit-learn which this change then breaks. Perhaps the test is better seq is None or len(seq) == 0
, since that's what is actually needed for the next line about an empty sequence?
Besides improving the error message and using clear code instead of a comment, this makes shuffle(), sample(), and randrange() slightly faster.