This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Providing invalid value to random.sample can result in incorrect error message
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Chris.Tandiono, mark.dickinson, rhettinger
Priority: low Keywords: patch

Created on 2013-03-09 09:07 by Chris.Tandiono, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
random_sample.patch Chris.Tandiono, 2013-03-09 09:07 review
Messages (3)
msg183808 - (view) Author: Chris Tandiono (Chris.Tandiono) Date: 2013-03-09 09:07
Currently, random.sample(population, k) raises a ValueError if k is out of the range of [0, len(population)], inclusive. However, the message says "Sample larger than population" even when the real problem is that k < 0. The attached patch fixes that.

The problem exists in all versions of Python that have the random.sample function.

I think I should be adding tests for this, but I don't know in what existing file, if any, this test should go into.
msg183854 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-03-09 22:57
A negative value will almost never be the cause of this exception.

The proposed new message is more accurate but is also less useful and informative.  Getting a ValueError exception already means "the value is invalid".  The job of the message is to suggest the likely cause so a person will no what to do about it. 

That said, I'll tweak the message at some point but I'm rejecting the patch as-is because I believe it makes the message worse rather better.

Please do keep looking for ways to improve Python's error messages and make them as useful as possible.

Here's food for thought:  In Py2.x, the error message for len(obj) is different depending on whether it is a new-style or old-style class.  If someone has forgotten to add the appropriate magic method to their class, which message would be the most helpful:

>>> class A(object): pass

>>> len(A())
Traceback (most recent call last):
TypeError: object of type 'A' has no len()

>>> class A: pass
>>> len(A())
Traceback (most recent call last):
AttributeError: A instance has no attribute '__len__'
msg183855 - (view) Author: Chris Tandiono (Chris.Tandiono) Date: 2013-03-09 23:32
Hmm. I'm not sure I buy the argument that the new message is less useful (wouldn't you like to know the exact values that caused the problem? that's what int() does when you provide it garbage). I guess it could be less informative, since you have to decide for yourself whether it's too large or too small a value.

At any rate, I think that whatever modification(s) you make to the message, it shouldn't say "Sample larger than population" when k < 0, because that's just plain misleading.
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61590
2013-06-25 11:14:14mark.dickinsonlinkissue18297 superseder
2013-03-09 23:32:33Chris.Tandionosetmessages: + msg183855
2013-03-09 22:57:36rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg183854
2013-03-09 22:43:32rhettingersetmessages: - msg183836
2013-03-09 18:20:17rhettingersetpriority: normal -> low
assignee: rhettinger
messages: + msg183836
2013-03-09 18:07:16ned.deilysetnosy: + rhettinger, mark.dickinson

versions: - Python 2.6, 3rd party, Python 3.1, Python 3.5
2013-03-09 09:07:50Chris.Tandionosettitle: Providing invalid value to -> Providing invalid value to random.sample can result in incorrect error message
2013-03-09 09:07:29Chris.Tandionocreate