]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-145056: Add support for merging collections.UserDict and frozendict (GH-146465)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 30 Mar 2026 19:07:28 +0000 (22:07 +0300)
committerGitHub <noreply@github.com>
Mon, 30 Mar 2026 19:07:28 +0000 (22:07 +0300)
Lib/collections/__init__.py
Lib/test/test_userdict.py
Misc/NEWS.d/next/Library/2026-03-26-14-44-07.gh-issue-145056.L9KPC3.rst [new file with mode: 0644]

index ba60df037f28cbe6169a5fea029ff25ba1d6eecc..febab521629228cbd29a8ea3cc6ed4c649d96bde 100644 (file)
@@ -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
 
index 13285c9b2a3b7fe5acd6be1c71e4949496166890..c60135ca5a12a8f451f3357302d60341b2111fab 100644 (file)
@@ -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'})
diff --git a/Misc/NEWS.d/next/Library/2026-03-26-14-44-07.gh-issue-145056.L9KPC3.rst b/Misc/NEWS.d/next/Library/2026-03-26-14-44-07.gh-issue-145056.L9KPC3.rst
new file mode 100644 (file)
index 0000000..66e3111
--- /dev/null
@@ -0,0 +1 @@
+Add support for merging :class:`collections.UserDict` and :class:`frozendict`.