Skip to content

bpo-29986: Delete tip to raise TypeError from tp_richcompare. #987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

ssbr
Copy link
Contributor

@ssbr ssbr commented Apr 4, 2017

I 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

OTOH, if this note should be kept, and this PR rejected, 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.

https://bugs.python.org/issue29986

Automerge-Triggered-By: @tiran

I 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

OTOH, if this note should be kept, and this PR rejected, this section could use a decent explanation of the difference between "undefined" (should return NotImplemented) and "unsupported" (should apparently raise TypeError). Perhaps a reference to an example from the stdlib.
@mention-bot
Copy link

@ssbr, thanks for your PR! By analyzing the history of the files in this pull request, we identified @birkenfeld, @1st1 and @vadmium to be potential reviewers.

@berkerpeksag
Copy link
Member

Thanks for the PR, but please file an issue on bugs.python.org to discuss the patch.

@ssbr ssbr changed the title Delete tip to raise TypeError from tp_richcompare. bpo-29986: Delete tip to raise TypeError from tp_richcompare. Apr 4, 2017
@ssbr
Copy link
Contributor Author

ssbr commented Apr 4, 2017

Done: http://bugs.python.org/issue29986

@serhiy-storchaka serhiy-storchaka added the docs Documentation in the Doc dir label Dec 8, 2018

If you want to implement a type for which only a limited set of
comparisons makes sense (e.g. ``==`` and ``!=``, but not ``<`` and
friends), directly raise :exc:`TypeError` in the rich comparison function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of removing this note, maybe you should change it say return NotImplemented instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that clashes with guidance provided on the issue. :) Maybe this can get hashed out there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that clashes with guidance provided on the issue

The important thing is that the bad advice (raise TypeError) is removed. The consensus is to return NotImplemented in this case. How explicit you want to make that in the docs is bikeshedding territory.

In other words: I'm totally fine with just removing that paragraph.

@csabella
Copy link
Contributor

csabella commented Jun 5, 2019

@ericsnowcurrently, can you take a look at this change? You recently modified this section of the docs (which is the merge conflict), but you didn't change this part, so your input may be helpful. Thanks!

@ericsnowcurrently
Copy link
Member

Unfortunately, I am not familiar enough right now with the relevant code to make a helpful review. Sorry.

@ericsnowcurrently
Copy link
Member

FWIW, to fix the merge conflict the relevant note just needs to be removed from wherever it is now in the file. :)

@ericsnowcurrently ericsnowcurrently removed their request for review June 14, 2019 16:43
@ssbr
Copy link
Contributor Author

ssbr commented Aug 18, 2019

(I forgot about this PR; don't use github much and didn't notice the replies.) Is this blocked on me?

@JulienPalard JulienPalard reopened this Sep 9, 2019
@JulienPalard
Copy link
Member

Closing and reopening to retry failed CI job.

@JulienPalard
Copy link
Member

@ssbr Would you please rebase this on master? The tests won't pass due to an expired SSL certificate (unrelated to your PR).

@JulienPalard
Copy link
Member

Hi @ssbr I rebased your branch on top of master because the CI was not passing due to someting completly unrelated. I had not the right to fix it on your branch (probably too old or me) so I reopened it for you here: #16095 and it should be merged any minute soon.

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.