[Python-checkins] bpo-39264: Fix UserDict.get() to account for __missing__() (GH-17910)

miss-islington webhook-mailer at python.org
Tue May 10 17:23:54 EDT 2022


https://github.com/python/cpython/commit/30a43586f0d1776d25beb71b92f9880be7997e1b
commit: 30a43586f0d1776d25beb71b92f9880be7997e1b
branch: main
author: Bar Harel <bar.harel at biocatch.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-05-10T14:23:45-07:00
summary:

bpo-39264: Fix UserDict.get() to account for __missing__() (GH-17910)



Here's the patch according to the discussion at the [Python-Dev mailing list](https://mail.python.org/archives/list/python-dev@python.org/thread/SDXOEMAEM6KQ3CQCJVBVRT5QNSPAVU6X/).
UserDict.get() will match dict's behavior and not call `__missing__`.

Automerge-Triggered-By: GH:rhettinger

files:
A Misc/NEWS.d/next/Library/2020-01-09-01-57-12.bpo-39264.GsBL9-.rst
M Lib/collections/__init__.py
M Lib/test/test_collections.py

diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 7af8dcd526df8..58607874be93d 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -1132,10 +1132,17 @@ def __delitem__(self, key):
     def __iter__(self):
         return iter(self.data)
 
-    # Modify __contains__ to work correctly when __missing__ is present
+    # Modify __contains__ and get() to work like dict
+    # does when __missing__ is present.
     def __contains__(self, key):
         return key in self.data
 
+    def get(self, key, default=None):
+        if key in self:
+            return self[key]
+        return default
+
+
     # Now, add the methods in dicts but not in MutableMapping
     def __repr__(self):
         return repr(self.data)
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index fa1d0e014dee9..59b3f2ec7bfcb 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -71,6 +71,14 @@ def test_dict_copy(self):
         obj[123] = "abc"
         self._copy_test(obj)
 
+    def test_dict_missing(self):
+        class A(UserDict):
+            def __missing__(self, key):
+                return 456
+        self.assertEqual(A()[123], 456)
+        # get() ignores __missing__ on dict
+        self.assertIs(A().get(123), None)
+
 
 ################################################################################
 ### ChainMap (helper class for configparser and the string module)
diff --git a/Misc/NEWS.d/next/Library/2020-01-09-01-57-12.bpo-39264.GsBL9-.rst b/Misc/NEWS.d/next/Library/2020-01-09-01-57-12.bpo-39264.GsBL9-.rst
new file mode 100644
index 0000000000000..5f9ffdffce5c2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-09-01-57-12.bpo-39264.GsBL9-.rst
@@ -0,0 +1,3 @@
+Fixed :meth:`collections.UserDict.get` to not call
+:meth:`__missing__` when a value is not found. This matches the behavior of
+:class:`dict`. Patch by Bar Harel.



More information about the Python-checkins mailing list