You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MappingProxyType cannot hash a hashable underlying mapping #87995
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.
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.
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.
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).
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.
Activity
andy-maier commentedon Apr 13, 2021
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
Running the mp_hash.py script:
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.
rhettinger commentedon Apr 14, 2021
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.
serhiy-storchaka commentedon Apr 14, 2021
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.
serhiy-storchaka commentedon Apr 14, 2021
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).
vbrozik commentedon Jun 21, 2022
The issue #88004 (bpo-43838) was already closed with reasoning:
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
MappingProxyTypedirectly.pythongh-87995: Make MappingProxyType hashable
gh-87995: Make MappingProxyType hashable (GH-94252)
ambv commentedon Jun 28, 2022
This is now fixed. Thanks, Serhiy! ✨ 🍰 ✨
pythongh-87995: Make MappingProxyType hashable (pythonGH-94252)
matanox commentedon Dec 6, 2023
Does this mean that this should no longer issue an "unhashable type" TypeError?
Or is this feature indeed coming as of python 3.12?