Skip to content

Commit cc02693

Browse files
authored
gh-149534: Fix unification of defaultdict and frozendict with | (#149539)
1 parent c987736 commit cc02693

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

Lib/test/test_defaultdict.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ def test_union(self):
186186
with self.assertRaises(TypeError):
187187
i |= None
188188

189+
# frozendict
190+
i_fd = i | frozendict(s)
191+
self.assertIs(type(i_fd), defaultdict)
192+
self.assertIs(i_fd.default_factory, int)
193+
self.assertDictEqual(i_fd, {1: "one", 2: 2, 0: "zero"})
194+
self.assertEqual(list(i_fd), [1, 2, 0])
195+
196+
fd_i = frozendict(s) | i
197+
self.assertIs(type(fd_i), frozendict)
198+
self.assertEqual(fd_i, {1: "one", 2: 2, 0: "zero"})
199+
self.assertEqual(list(fd_i), [0, 1, 2])
200+
189201
def test_factory_conflict_with_set_value(self):
190202
key = "conflict_test"
191203
count = 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix merging of :class:`collections.defaultdict` and :class:`frozendict`.

Modules/_collectionsmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,7 @@ defdict_or(PyObject* left, PyObject* right)
24212421
self = right;
24222422
other = left;
24232423
}
2424-
if (!PyDict_Check(other)) {
2424+
if (!PyAnyDict_Check(other)) {
24252425
Py_RETURN_NOTIMPLEMENTED;
24262426
}
24272427
// Like copy(), this calls the object's class.

0 commit comments

Comments
 (0)