From ec2bc12e54920d6fbd664ffc5af41dfb45a0a7f7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 30 Mar 2026 22:07:28 +0300 Subject: [PATCH] gh-145056: Add support for merging collections.UserDict and frozendict (GH-146465) --- Lib/collections/__init__.py | 4 ++-- Lib/test/test_userdict.py | 4 ++-- .../Library/2026-03-26-14-44-07.gh-issue-145056.L9KPC3.rst | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-03-26-14-44-07.gh-issue-145056.L9KPC3.rst diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index ba60df037f28..febab5216292 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -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 diff --git a/Lib/test/test_userdict.py b/Lib/test/test_userdict.py index 13285c9b2a3b..c60135ca5a12 100644 --- a/Lib/test/test_userdict.py +++ b/Lib/test/test_userdict.py @@ -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 index 000000000000..66e31117e33b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-26-14-44-07.gh-issue-145056.L9KPC3.rst @@ -0,0 +1 @@ +Add support for merging :class:`collections.UserDict` and :class:`frozendict`. -- 2.47.3