Skip to content

Commit 0c8cbb0

Browse files
gh-145056: Fix merging of collections.OrderedDict and frozendict
1 parent 3a2b81e commit 0c8cbb0

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

Lib/collections/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,14 @@ def __ior__(self, other):
328328
return self
329329

330330
def __or__(self, other):
331-
if not isinstance(other, dict):
331+
if not isinstance(other, (dict, frozendict)):
332332
return NotImplemented
333333
new = self.__class__(self)
334334
new.update(other)
335335
return new
336336

337337
def __ror__(self, other):
338-
if not isinstance(other, dict):
338+
if not isinstance(other, (dict, frozendict)):
339339
return NotImplemented
340340
new = self.__class__(other)
341341
new.update(self)

Lib/test/test_ordered_dict.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ def test_merge_operator(self):
698698
d |= list(b.items())
699699
expected = OrderedDict({0: 0, 1: 1, 2: 2, 3: 3})
700700
self.assertEqual(a | dict(b), expected)
701+
self.assertEqual(a | frozendict(b), expected)
701702
self.assertEqual(a | b, expected)
702703
self.assertEqual(c, expected)
703704
self.assertEqual(d, expected)
@@ -706,12 +707,15 @@ def test_merge_operator(self):
706707
c |= a
707708
expected = OrderedDict({1: 1, 2: 1, 3: 3, 0: 0})
708709
self.assertEqual(dict(b) | a, expected)
710+
self.assertEqual(frozendict(b) | a, expected)
709711
self.assertEqual(b | a, expected)
710712
self.assertEqual(c, expected)
711713

712714
self.assertIs(type(a | b), OrderedDict)
713715
self.assertIs(type(dict(a) | b), OrderedDict)
716+
self.assertIs(type(frozendict(a) | b), frozendict) # BUG: should be OrderedDict
714717
self.assertIs(type(a | dict(b)), OrderedDict)
718+
self.assertIs(type(a | frozendict(b)), OrderedDict)
715719

716720
expected = a.copy()
717721
a |= ()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix merging of :class:`collections.OrderedDict` and :class:`frozendict`.

Objects/odictobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ odict_or(PyObject *left, PyObject *right)
906906
type = Py_TYPE(right);
907907
other = left;
908908
}
909-
if (!PyDict_Check(other)) {
909+
if (!PyAnyDict_Check(other)) {
910910
Py_RETURN_NOTIMPLEMENTED;
911911
}
912912
PyObject *new = PyObject_CallOneArg((PyObject*)type, left);
@@ -2271,7 +2271,7 @@ static int
22712271
mutablemapping_update_arg(PyObject *self, PyObject *arg)
22722272
{
22732273
int res = 0;
2274-
if (PyDict_CheckExact(arg)) {
2274+
if (PyAnyDict_CheckExact(arg)) {
22752275
PyObject *items = PyDict_Items(arg);
22762276
if (items == NULL) {
22772277
return -1;

0 commit comments

Comments
 (0)