[Python-ideas] Unhelpful error message from sorted

Steven D'Aprano steve at pearwood.info
Tue May 15 10:02:08 CEST 2012


On Tue, May 15, 2012 at 12:28:02AM +0100, Michael Foord wrote:
> Hello all,
> 
> It seems to me that the following error message, whilst technically
> correct, is unhelpful:
> 
> >>> sorted([3, 2, 1], reverse=None)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: an integer is required

I don't know what you mean by "technically correct". Surely the Pythonic 
idiom is to allow any value in a boolean context.

sorted() here is neither one thing nor the other, neither duck-typing, 
since it won't accept flags that quack like a bool, nor does it strictly 
insist on a bool, since it accepts ints:

>>> sorted([1,2,3], reverse=42)
[3, 2, 1]

I can't see any sense to this almost-but-not-quite type restriction.

+1 to allow any object that is truthy or falsey (i.e. anything).
+0 to allowing only True or False.
-1 to half-heartedly allowing ints but no other values.


-- 
Steven



More information about the Python-ideas mailing list