Skip to content
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

Return False from __contains__ method if object not hashable for set and dict #89768

Closed
heckad mannequin opened this issue Oct 25, 2021 · 5 comments
Closed

Return False from __contains__ method if object not hashable for set and dict #89768

heckad mannequin opened this issue Oct 25, 2021 · 5 comments
Labels
3.11 only security fixes stdlib Python modules in the Lib dir

Comments

@heckad
Copy link
Mannequin

heckad mannequin commented Oct 25, 2021

BPO 45605
Nosy @ericvsmith, @serhiy-storchaka, @heckad
Superseder
  • bpo-18510: dict.contains and dict.keys().contains raises exception instead of returning False
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-10-25.16:15:27.466>
    created_at = <Date 2021-10-25.15:22:39.835>
    labels = ['library', '3.11']
    title = 'Return False from __contains__ method if object not hashable for set and dict'
    updated_at = <Date 2021-10-25.16:15:27.464>
    user = 'https://github.com/heckad'

    bugs.python.org fields:

    activity = <Date 2021-10-25.16:15:27.464>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-25.16:15:27.466>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2021-10-25.15:22:39.835>
    creator = 'heckad'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45605
    keywords = []
    message_count = 5.0
    messages = ['404971', '404972', '404973', '404975', '404978']
    nosy_count = 3.0
    nosy_names = ['eric.smith', 'serhiy.storchaka', 'heckad']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '18510'
    type = None
    url = 'https://bugs.python.org/issue45605'
    versions = ['Python 3.11']

    @heckad
    Copy link
    Mannequin Author

    heckad mannequin commented Oct 25, 2021

    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

    @heckad heckad mannequin added 3.11 only security fixes stdlib Python modules in the Lib dir labels Oct 25, 2021
    @ericvsmith
    Copy link
    Member

    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.

    @heckad
    Copy link
    Mannequin Author

    heckad mannequin commented Oct 25, 2021

    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.

    @ericvsmith
    Copy link
    Member

    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.

    @serhiy-storchaka
    Copy link
    Member

    This is a duplicate of bpo-18510. 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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants