.. versionadded:: 3.9
+ .. describe:: hash(proxy)
+
+ Return a hash of the underlying mapping.
+
+ .. versionadded:: 3.12
+
Additional Utility Classes and Functions
----------------------------------------
Other Language Changes
======================
+* :class:`types.MappingProxyType` instances are now hashable if the underlying
+ mapping is hashable.
+ (Contributed by Serhiy Storchaka in :gh:`87995`.)
New Modules
self.assertDictEqual(mapping, {'a': 0, 'b': 1, 'c': 2})
self.assertDictEqual(other, {'c': 3, 'p': 0})
+ def test_hash(self):
+ class HashableDict(dict):
+ def __hash__(self):
+ return 3844817361
+ view = self.mappingproxy({'a': 1, 'b': 2})
+ self.assertRaises(TypeError, hash, view)
+ mapping = HashableDict({'a': 1, 'b': 2})
+ view = self.mappingproxy(mapping)
+ self.assertEqual(hash(view), hash(mapping))
+
class ClassCreationTests(unittest.TestCase):
--- /dev/null
+:class:`types.MappingProxyType` instances are now hashable if the underlying
+mapping is hashable.
return PyObject_GetIter(pp->mapping);
}
+static Py_hash_t
+mappingproxy_hash(mappingproxyobject *pp)
+{
+ return PyObject_Hash(pp->mapping);
+}
+
static PyObject *
mappingproxy_str(mappingproxyobject *pp)
{
&mappingproxy_as_number, /* tp_as_number */
&mappingproxy_as_sequence, /* tp_as_sequence */
&mappingproxy_as_mapping, /* tp_as_mapping */
- 0, /* tp_hash */
+ (hashfunc)mappingproxy_hash, /* tp_hash */
0, /* tp_call */
(reprfunc)mappingproxy_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */