GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — MappingProxyType cannot hash a hashable underlying mapping · Issue #87995 · python/cpython

MappingProxyType cannot hash a hashable underlying mapping #87995

New issue
Copy link
New issue
Copy link
Closed
Closed
MappingProxyType cannot hash a hashable underlying mapping#87995
Copy link
Labels
3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

andy-maier
mannequin
opened on Apr 13, 2021
Issue body actions
BPO 43829
Nosy @rhettinger, @serhiy-storchaka, @andy-maier

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 = None
created_at = <Date 2021-04-13.08:25:32.893>
labels = ['type-bug', 'library', '3.10']
title = 'MappingProxyType cannot hash a hashable underlying mapping'
updated_at = <Date 2021-04-14.07:46:45.086>
user = 'https://github.com/andy-maier'

bugs.python.org fields:

activity = <Date 2021-04-14.07:46:45.086>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2021-04-13.08:25:32.893>
creator = 'andymaier'
dependencies = []
files = []
hgrepos = []
issue_num = 43829
keywords = []
message_count = 4.0
messages = ['390936', '391028', '391038', '391041']
nosy_count = 3.0
nosy_names = ['rhettinger', 'serhiy.storchaka', 'andymaier']
pr_nums = []
priority = 'low'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue43829'
versions = ['Python 3.10']

Reacted by Václav Brožík

Activity

  1. andy-maier commented on Apr 13, 2021

    @andy-maier
    andy-maiermannequin
    on Apr 13, 2021
    MannequinAuthor
    More actions

    Objects of MappingProxyType do expose a __hash__() method, but if the underlying mapping is hashable, it still does not support hashing it.

    Example:

    Content of mp_hash.py:

    ------
    #!/usr/bin/env python

    from nocasedict import NocaseDict, HashableMixin
    from types import MappingProxyType
    
    class HashableDict(HashableMixin, NocaseDict):
        """A hashable dictionary"""
        pass
    
    hd = HashableDict({'a': 1, 'b': 2})
    print("hash(hd): {}".format(hash(hd)))
    
    mp = MappingProxyType(hd)
    print("hash(mp): {}".format(hash(mp)))

    Running the mp_hash.py script:

    hash(hd): 3709951335832776636
    Traceback (most recent call last):
      File "/Users/maiera/Projects/Python/cpython/issues/mappingproxy/./mp_hash.py", line 14, in <module>
        print("hash(mp): {}".format(hash(mp)))
    TypeError: unhashable type: 'mappingproxy'

    There are use cases where a function wants to return an immutable view on an internal dictionary, and the caller of the function should be able to use the returned object like a dictionary, except that it is read-only.

    Note there is https://bugs.python.org/issue31209 on the inability to pickle MappingProxyType objects which was closed without adding the capability. That would fall under the same argument.

    Reacted by Václav Brožík and Jan-Hein Bührman
  2. andy-maier
    added
    3.9 (EOL)end of lifeend of life
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 13, 2021
  3. rhettinger commented on Apr 14, 2021

    @rhettinger
    rhettinger
    on Apr 14, 2021
    Contributor
    More actions

    Serhiy, what do you think? Would it make sense to pass through the underlying hash? I don't think there is much use for this but don't see any particular reason to block it.

  4. rhettinger
    added
    3.10 (EOL)end of lifeend of life
    and removed
    3.9 (EOL)end of lifeend of life
    on Apr 14, 2021
  5. serhiy-storchaka commented on Apr 14, 2021

    @serhiy-storchaka
    serhiy-storchaka
    on Apr 14, 2021
    Member
    More actions

    Perhaps MappingProxyType is unhashable by accident. It implements __eq__, and it makes it unhashable by default. And nobody made request for this feature before. I think that implementing __hash__ would not make anything wrong.

  6. serhiy-storchaka commented on Apr 14, 2021

    @serhiy-storchaka
    serhiy-storchaka
    on Apr 14, 2021
    Member
    More actions

    But there is an issue with comparison implementation in MappingProxyType (see bpo-43838). Resolving that issue can affect the decision about hashability.

    There should be always true the following predicate: if x == y then hash(x) == hash(y).

  7. ezio-melotti
    transferred this issue fromon Apr 10, 2022
  8. vbrozik commented on Jun 21, 2022

    @vbrozik
    vbrozik
    on Jun 21, 2022
    More actions

    The issue #88004 (bpo-43838) was already closed with reasoning:

    ... the reason we proxy in the first place is not absolute safety but to make sure
    people don’t accidentally update the dict when they intend to update the
    class ...

    In general, it is hard to completely wall-off instances against deliberate efforts to pry them open

    I do not se a reason why not allow the hashing. Recently I needed to collect a set of small unique dict objects. It is a pity that I could not utilize MappingProxyType directly.

  9. serhiy-storchaka
    added a commit that references this issue on Jun 25, 2022

    pythongh-87995: Make MappingProxyType hashable

    7afb2d2
  10. ambv
    added a commit that references this issue on Jun 28, 2022

    gh-87995: Make MappingProxyType hashable (GH-94252)

    efdc9d6
  11. ambv commented on Jun 28, 2022

    @ambv
    ambv
    on Jun 28, 2022
    Contributor
    More actions

    This is now fixed. Thanks, Serhiy! ✨ 🍰 ✨

  12. ambv
    closed this as completedon Jun 28, 2022
  13. gvanrossum
    added a commit that references this issue on Jun 30, 2022

    pythongh-87995: Make MappingProxyType hashable (pythonGH-94252)

    98082d0
  14. matanox commented on Dec 6, 2023

    @matanox
    matanox
    on Dec 6, 2023
    More actions

    Does this mean that this should no longer issue an "unhashable type" TypeError?

    hash(MappingProxyType({3:2, 4:3}))
    

    Or is this feature indeed coming as of python 3.12?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.