mirror of
https://github.com/python/cpython.git
synced 2026-05-06 12:49:07 -04:00
gh-145056: Add support for merging collections.UserDict and frozendict (GH-146465)
This commit is contained in:
@@ -1216,14 +1216,14 @@ class UserDict(_collections_abc.MutableMapping):
|
||||
def __or__(self, other):
|
||||
if isinstance(other, UserDict):
|
||||
return self.__class__(self.data | other.data)
|
||||
if isinstance(other, dict):
|
||||
if isinstance(other, (dict, frozendict)):
|
||||
return self.__class__(self.data | other)
|
||||
return NotImplemented
|
||||
|
||||
def __ror__(self, other):
|
||||
if isinstance(other, UserDict):
|
||||
return self.__class__(other.data | self.data)
|
||||
if isinstance(other, dict):
|
||||
if isinstance(other, (dict, frozendict)):
|
||||
return self.__class__(other | self.data)
|
||||
return NotImplemented
|
||||
|
||||
|
||||
@@ -245,7 +245,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
|
||||
test_repr_deep = mapping_tests.TestHashMappingProtocol.test_repr_deep
|
||||
|
||||
def test_mixed_or(self):
|
||||
for t in UserDict, dict, types.MappingProxyType:
|
||||
for t in UserDict, dict, frozendict, types.MappingProxyType:
|
||||
with self.subTest(t.__name__):
|
||||
u = UserDict({0: 'a', 1: 'b'}) | t({1: 'c', 2: 'd'})
|
||||
self.assertEqual(u, {0: 'a', 1: 'c', 2: 'd'})
|
||||
@@ -276,7 +276,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
|
||||
self.assertIs(type(u), UserDictSubclass)
|
||||
|
||||
def test_mixed_ior(self):
|
||||
for t in UserDict, dict, types.MappingProxyType:
|
||||
for t in UserDict, dict, frozendict, types.MappingProxyType:
|
||||
with self.subTest(t.__name__):
|
||||
u = u2 = UserDict({0: 'a', 1: 'b'})
|
||||
u |= t({1: 'c', 2: 'd'})
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Add support for merging :class:`collections.UserDict` and :class:`frozendict`.
|
||||
Reference in New Issue
Block a user