]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-87995: Make MappingProxyType hashable (GH-94252)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 28 Jun 2022 09:54:58 +0000 (12:54 +0300)
committerGitHub <noreply@github.com>
Tue, 28 Jun 2022 09:54:58 +0000 (11:54 +0200)
Doc/library/types.rst
Doc/whatsnew/3.12.rst
Lib/test/test_types.py
Misc/NEWS.d/next/Core and Builtins/2022-06-25-10-19-43.gh-issue-87995.aMDHnp.rst [new file with mode: 0644]
Objects/descrobject.c

index e0e77dfbfe7ed2dd37082a8eaa8302671addf030..cce0ad960edf9703c7b7f86724fc628f6463d424 100644 (file)
@@ -417,6 +417,12 @@ Standard names are defined for the following types:
 
       .. versionadded:: 3.9
 
+   .. describe:: hash(proxy)
+
+      Return a hash of the underlying mapping.
+
+      .. versionadded:: 3.12
+
 
 Additional Utility Classes and Functions
 ----------------------------------------
index 0a4d49841eff8bcc0dd5c78b9baac78aff5bd29b..11e8b87f0288114a4ff0a1c4cb12d490dc8ab426 100644 (file)
@@ -79,6 +79,9 @@ New Features
 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
index 8556ca35ca06c125957ab3e8aa9e14f43846bece..f00da0a758d46f654239e043cf6d13380e90fc34 100644 (file)
@@ -1203,6 +1203,16 @@ class MappingProxyTests(unittest.TestCase):
         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):
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-06-25-10-19-43.gh-issue-87995.aMDHnp.rst b/Misc/NEWS.d/next/Core and Builtins/2022-06-25-10-19-43.gh-issue-87995.aMDHnp.rst
new file mode 100644 (file)
index 0000000..4154ebc
--- /dev/null
@@ -0,0 +1,2 @@
+:class:`types.MappingProxyType` instances are now hashable if the underlying
+mapping is hashable.
index 8ef6a82d7c6d963c5c6f138dac3ffd7a938064f1..82570e085143ed5b8387ee2cf51e0f98ae2f0583 100644 (file)
@@ -1178,6 +1178,12 @@ mappingproxy_getiter(mappingproxyobject *pp)
     return PyObject_GetIter(pp->mapping);
 }
 
+static Py_hash_t
+mappingproxy_hash(mappingproxyobject *pp)
+{
+    return PyObject_Hash(pp->mapping);
+}
+
 static PyObject *
 mappingproxy_str(mappingproxyobject *pp)
 {
@@ -1901,7 +1907,7 @@ PyTypeObject PyDictProxy_Type = {
     &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 */