Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,14 @@ def __ior__(self, other):
return self

def __or__(self, other):
if not isinstance(other, dict):
if not isinstance(other, (dict, frozendict)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the C implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The C version already supported any mapping input, that is how the tests passed. But I will update the fast path to use PyAnyDict_CheckExact(arg).

return NotImplemented
new = self.__class__(self)
new.update(other)
return new

def __ror__(self, other):
if not isinstance(other, dict):
if not isinstance(other, (dict, frozendict)):
return NotImplemented
new = self.__class__(other)
new.update(self)
Expand Down Expand Up @@ -1221,14 +1221,14 @@ def __repr__(self):
def __or__(self, other):
if isinstance(other, UserDict):
return self.__class__(self.data | other.data)
if isinstance(other, dict):
if isinstance(other, (dict, frozendict)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any tests for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I didn't see where the dict version was tested so left this alone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very bad. We need to add tests with dict first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, let's leave this. There are many other missing tests for UserDict and UserList. I am working on this.

return self.__class__(self.data | other)
return NotImplemented

def __ror__(self, other):
if isinstance(other, UserDict):
return self.__class__(other.data | self.data)
if isinstance(other, dict):
if isinstance(other, (dict, frozendict)):
return self.__class__(other | self.data)
return NotImplemented

Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_ordered_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ def test_merge_operator(self):
d |= list(b.items())
expected = OrderedDict({0: 0, 1: 1, 2: 2, 3: 3})
self.assertEqual(a | dict(b), expected)
self.assertEqual(a | frozendict(b), expected)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not the type of the result to be tested too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing test didn't check result type. I don't want to feature creep this edit. Reviewing and expanding the existing test strategies can be a task for another day.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new tests are passed with unmodified code. Therefore they are not correct tests for this change.

self.assertEqual(a | b, expected)
self.assertEqual(c, expected)
self.assertEqual(d, expected)
Expand All @@ -706,6 +707,7 @@ def test_merge_operator(self):
c |= a
expected = OrderedDict({1: 1, 2: 1, 3: 3, 0: 0})
self.assertEqual(dict(b) | a, expected)
self.assertEqual(frozendict(b) | a, expected)
self.assertEqual(b | a, expected)
self.assertEqual(c, expected)

Expand Down
2 changes: 1 addition & 1 deletion Objects/odictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ static int
mutablemapping_update_arg(PyObject *self, PyObject *arg)
{
int res = 0;
if (PyDict_CheckExact(arg)) {
if (PyAnyDict_CheckExact(arg)) {
PyObject *items = PyDict_Items(arg);
if (items == NULL) {
return -1;
Expand Down
Loading