From cc0269334fdf7be12de79316882befd5cdb4cf7e Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 1 Jun 2026 16:26:49 +0300 Subject: [PATCH] gh-149534: Fix unification of `defaultdict` and `frozendict` with `|` (#149539) --- Lib/test/test_defaultdict.py | 12 ++++++++++++ .../2026-05-08-09-11-48.gh-issue-149534.Tw7eeY.rst | 1 + Modules/_collectionsmodule.c | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-05-08-09-11-48.gh-issue-149534.Tw7eeY.rst diff --git a/Lib/test/test_defaultdict.py b/Lib/test/test_defaultdict.py index a193eb10f16..cc78f01e3e2 100644 --- a/Lib/test/test_defaultdict.py +++ b/Lib/test/test_defaultdict.py @@ -186,6 +186,18 @@ def test_union(self): with self.assertRaises(TypeError): i |= None + # frozendict + i_fd = i | frozendict(s) + self.assertIs(type(i_fd), defaultdict) + self.assertIs(i_fd.default_factory, int) + self.assertDictEqual(i_fd, {1: "one", 2: 2, 0: "zero"}) + self.assertEqual(list(i_fd), [1, 2, 0]) + + fd_i = frozendict(s) | i + self.assertIs(type(fd_i), frozendict) + self.assertEqual(fd_i, {1: "one", 2: 2, 0: "zero"}) + self.assertEqual(list(fd_i), [0, 1, 2]) + def test_factory_conflict_with_set_value(self): key = "conflict_test" count = 0 diff --git a/Misc/NEWS.d/next/Library/2026-05-08-09-11-48.gh-issue-149534.Tw7eeY.rst b/Misc/NEWS.d/next/Library/2026-05-08-09-11-48.gh-issue-149534.Tw7eeY.rst new file mode 100644 index 00000000000..0938935a75d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-08-09-11-48.gh-issue-149534.Tw7eeY.rst @@ -0,0 +1 @@ +Fix merging of :class:`collections.defaultdict` and :class:`frozendict`. diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index d702d655a40..541ca48633b 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -2421,7 +2421,7 @@ defdict_or(PyObject* left, PyObject* right) self = right; other = left; } - if (!PyDict_Check(other)) { + if (!PyAnyDict_Check(other)) { Py_RETURN_NOTIMPLEMENTED; } // Like copy(), this calls the object's class.