[issue17388] Providing invalid value to random.sample can result in incorrect error message

Raymond Hettinger report at bugs.python.org
Sat Mar 9 23:57:36 CET 2013


Raymond Hettinger added the comment:

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__'

----------
resolution:  -> rejected
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17388>
_______________________________________


More information about the Python-bugs-list mailing list