https://github.com/python/cpython/commit/3a12e9f5242c1a2d9ce34ed04159f501b3795bba
commit: 3a12e9f5242c1a2d9ce34ed04159f501b3795bba
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-06-01T16:07:33Z
summary:

[3.15] gh-149534: Fix unification of `defaultdict` and `frozendict` with `|` 
(GH-149539) (#150709)

gh-149534: Fix unification of `defaultdict` and `frozendict` with `|` 
(GH-149539)
(cherry picked from commit cc0269334fdf7be12de79316882befd5cdb4cf7e)

Co-authored-by: sobolevn <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-05-08-09-11-48.gh-issue-149534.Tw7eeY.rst
M Lib/test/test_defaultdict.py
M Modules/_collectionsmodule.c

diff --git a/Lib/test/test_defaultdict.py b/Lib/test/test_defaultdict.py
index a193eb10f16d17..cc78f01e3e2ebd 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 00000000000000..0938935a75d8c1
--- /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 d702d655a406b6..541ca48633bb56 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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to