[docs] [issue29986] Documentation recommends raising TypeError from tp_richcompare

Devin Jeanpierre report at bugs.python.org
Tue Apr 4 18:51:54 EDT 2017


New submission from Devin Jeanpierre:

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.

----------
assignee: docs at python
components: Documentation
messages: 291144
nosy: Devin Jeanpierre, docs at python
priority: normal
pull_requests: 1167
severity: normal
status: open
title: Documentation recommends raising TypeError from tp_richcompare

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


More information about the docs mailing list