]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-149534: Fix unification of `defaultdict` and `frozendict` with `|` (#149539)
authorsobolevn <mail@sobolevn.me>
Mon, 1 Jun 2026 13:26:49 +0000 (16:26 +0300)
committerGitHub <noreply@github.com>
Mon, 1 Jun 2026 13:26:49 +0000 (16:26 +0300)
Lib/test/test_defaultdict.py
Misc/NEWS.d/next/Library/2026-05-08-09-11-48.gh-issue-149534.Tw7eeY.rst [new file with mode: 0644]
Modules/_collectionsmodule.c

index a193eb10f16d1784c33dfc0e7e4e01ee1b61c52f..cc78f01e3e2ebdcb27340f0614760151f0b4cae8 100644 (file)
@@ -186,6 +186,18 @@ class TestDefaultDict(unittest.TestCase):
         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 (file)
index 0000000..0938935
--- /dev/null
@@ -0,0 +1 @@
+Fix merging of :class:`collections.defaultdict` and :class:`frozendict`.
index d702d655a406b66036b8e6941b0bdec0b1d96f09..541ca48633bb56bec25125d3bbb9e435f7b125a4 100644 (file)
@@ -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.