Author Devin Jeanpierre
Recipients Devin Jeanpierre, docs@python
Date 2017-04-04.22:51:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1491346313.99.0.131506773991.issue29986@psf.upfronthosting.co.za>
In-reply-to
Content
am not sure when TypeError is the right choice. Definitely, most of the time I've seen it done, it causes trouble, and NotImplemented usually does something better.

For example, see the work in https://bugs.python.org/issue8743 to get set to interoperate correctly with other set-like classes --- a problem caused by the use of TypeError instead of returning NotImplemented (e.g. https://hg.python.org/cpython/rev/3615cdb3b86d).

This advice seems to conflict with the usual and expected behavior of objects from Python: e.g. object().__lt__(1) returns NotImplemented rather than raising TypeError, despite < not "making sense" for object. Similarly for file objects and other uncomparable classes. Even complex numbers only return NotImplemented!


>>> 1j.__lt__(1j)
NotImplemented


If this note should be kept, this section could use a decent explanation of the difference between "undefined" (should return NotImplemented) and "nonsensical" (should apparently raise TypeError). Perhaps a reference to an example from the stdlib.
History
Date User Action Args
2017-04-04 22:51:54Devin Jeanpierresetrecipients: + Devin Jeanpierre, docs@python
2017-04-04 22:51:53Devin Jeanpierresetmessageid: <1491346313.99.0.131506773991.issue29986@psf.upfronthosting.co.za>
2017-04-04 22:51:53Devin Jeanpierrelinkissue29986 messages
2017-04-04 22:51:53Devin Jeanpierrecreate