This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Return False from __contains__ method if object not hashable for set and dict
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: duplicate
Dependencies: Superseder: dict.__contains__ and dict.keys().__contains__ raises exception instead of returning False
View: 18510
Assigned To: Nosy List: eric.smith, heckad, serhiy.storchaka
Priority: normal Keywords:

Created on 2021-10-25 15:22 by heckad, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg404971 - (view) Author: Андрей Казанцев (heckad) * Date: 2021-10-25 15:22
Now if do something like `[] in set()` python raise an exception, but if an object isn't hashable then we know for sure that it isn't in the set. Propose return False for these cases.

P.S. I would like to make a patch
msg404972 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-10-25 15:30
That seems like a pretty big change that could break a lot of existing code. I suggest writing a wrapper around "in" if you want this behavior.
msg404973 - (view) Author: Андрей Казанцев (heckad) * Date: 2021-10-25 15:35
Can you please specify in what cases we need to get an exception instead of False? It just seems very strange to me to rely on this behaviour.
msg404975 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-10-25 15:46
I don't have any code that would care, but if there's one thing I've learned in 15 years of python core development, it's that this will break someone's code.

If you want more feedback, you should probably bring this up for discussion on the python-ideas mailing list.
msg404978 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-25 16:15
This is a duplicate of issue18510. It was also discussed on the Python-Dev mailing list (maybe more than once). If you have some new arguments or something in past 8 years made the old arguments no longer valid please open a new discussion on the mailing list.
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89768
2021-10-25 16:15:27serhiy.storchakasetstatus: open -> closed

superseder: dict.__contains__ and dict.keys().__contains__ raises exception instead of returning False

nosy: + serhiy.storchaka
messages: + msg404978
resolution: duplicate
stage: resolved
2021-10-25 15:46:38eric.smithsetmessages: + msg404975
2021-10-25 15:35:08heckadsetmessages: + msg404973
2021-10-25 15:30:09eric.smithsetnosy: + eric.smith
messages: + msg404972
2021-10-25 15:22:39heckadcreate