-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-36144: Add PEP 584 operators to collections.ChainMap #18832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ba855e8
Update ChainMap to include | and |=
curtisbucher 4d83fee
Update ACKS
curtisbucher 906eff0
Update docs
curtisbucher d0c6d40
Update test_collections.py to include test_issue584().
curtisbucher ca6d478
Update test_union_operators
curtisbucher dd2ab1a
Update test_union_operators in TestChainMap
curtisbucher fe9b91d
Update test_union operators in test_collections.py
curtisbucher 4c7f937
Update test_union_operators in test_collections.py
curtisbucher 28b03fc
Check .maps rather than Chainmap equality.
curtisbucher dfac64a
Add news entry
curtisbucher e5b2592
Update Lib/test/test_collections.py
curtisbucher df57d59
Removed whitespace
curtisbucher 97a156d
Added Guido's changes
curtisbucher 60904c2
Fixed Docs
curtisbucher 5d63457
Removed whitespace
curtisbucher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -961,6 +961,25 @@ def clear(self): | |
'Clear maps[0], leaving maps[1:] intact.' | ||
self.maps[0].clear() | ||
|
||
def __ior__(self, other): | ||
self.maps[0] |= other | ||
return self | ||
|
||
def __or__(self, other): | ||
if isinstance(other, _collections_abc.Mapping): | ||
m = self.maps[0].copy() | ||
m.update(other) | ||
return self.__class__(m, *self.maps[1:]) | ||
return NotImplemented | ||
|
||
def __ror__(self, other): | ||
if isinstance(other, _collections_abc.Mapping): | ||
m = dict(other) | ||
for child in reversed(self.maps): | ||
m.update(child) | ||
return self.__class__(m) | ||
return NotImplemented | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an extra blank line here. |
||
|
||
################################################################################ | ||
### UserDict | ||
|
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 |
---|---|---|
|
@@ -232,6 +232,51 @@ def __contains__(self, key): | |
for k, v in dict(a=1, B=20, C=30, z=100).items(): # check get | ||
self.assertEqual(d.get(k, 100), v) | ||
|
||
def test_union_operators(self): | ||
cm1 = ChainMap(dict(a=1, b=2), dict(c=3, d=4)) | ||
cm2 = ChainMap(dict(a=10, e=5), dict(b=20, d=4)) | ||
cm3 = cm1.copy() | ||
d = dict(a=10, c=30) | ||
pairs = [('c', 3), ('p',0)] | ||
|
||
tmp = cm1 | cm2 # testing between chainmaps | ||
self.assertEqual(tmp.maps, [cm1.maps[0] | dict(cm2), *cm1.maps[1:]]) | ||
cm1 |= cm2 | ||
self.assertEqual(tmp, cm1) | ||
|
||
tmp = cm2 | d # testing between chainmap and mapping | ||
self.assertEqual(tmp.maps, [cm2.maps[0] | d, *cm2.maps[1:]]) | ||
self.assertEqual((d | cm2).maps, [d | dict(cm2)]) | ||
cm2 |= d | ||
self.assertEqual(tmp, cm2) | ||
|
||
# testing behavior between chainmap and iterable key-value pairs | ||
with self.assertRaises(TypeError): | ||
cm3 | pairs | ||
cm3 |= pairs | ||
self.assertEqual(cm3.maps, [cm3.maps[0] | dict(pairs), *cm3.maps[1:]]) | ||
gvanrossum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an extra blank line. |
||
# testing proper return types for ChainMap and it's subclasses | ||
class Subclass(ChainMap): | ||
pass | ||
|
||
class SubclassRor(ChainMap): | ||
def __ror__(self, other): | ||
return super().__ror__(other) | ||
|
||
tmp = ChainMap() | ChainMap() | ||
self.assertIs(type(tmp), ChainMap) | ||
self.assertIs(type(tmp.maps[0]), dict) | ||
tmp = ChainMap() | Subclass() | ||
self.assertIs(type(tmp), ChainMap) | ||
self.assertIs(type(tmp.maps[0]), dict) | ||
tmp = Subclass() | ChainMap() | ||
self.assertIs(type(tmp), Subclass) | ||
self.assertIs(type(tmp.maps[0]), dict) | ||
tmp = ChainMap() | SubclassRor() | ||
self.assertIs(type(tmp), SubclassRor) | ||
self.assertIs(type(tmp.maps[0]), dict) | ||
|
||
|
||
################################################################################ | ||
### Named Tuples | ||
|
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
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2020-03-07-11-26-08.bpo-36144.FG9jqy.rst
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 @@ | ||
Added :pep:`584` operators (``|`` and ``|=``) to :class:`collections.ChainMap`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.