FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-39264: Fix UserDict.get() to account for __missing__() by bharel · Pull Request #17910 · python/cpython · GitHub

Repository navigation

9 changes: 8 additions & 1 deletion Lib/collections/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,17 @@ def __delitem__(self, key): del self.data[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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Add a comment that get() is overridden to avoid a False positive when _missing_ is defined.

if key in self:
Comment thread
bharel marked this conversation as resolved.
return self[key]
return default

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Nit: Only one blank line so as to match the surrounding code.


# Now, add the methods in dicts but not in MutableMapping
def __repr__(self): return repr(self.data)
def __copy__(self):
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_collections.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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.

Back | FazBrowse Home | New Git URL