classification
Title: Should OrderedDict.viewitems compare equal to dict.viewitems when the items are equal?
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: eric.snow, jab, python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2015-05-25 23:28 by jab, last changed 2015-05-26 08:50 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
fix_view_registry.diff rhettinger, 2015-05-26 04:23 Register the dict views with the appropriate ABC
Messages (8)
msg244066 - (view) Author: Joshua Bronson (jab) * Date: 2015-05-25 23:28
Is it intentional that the second assertion in the following code fails?

```
from collections import OrderedDict

d = dict(C='carbon')
o = OrderedDict(d)
assert d == o
assert d.viewitems() == o.viewitems()
```

Since d == o, I'm surprised that d.viewitems() != o.viewitems(). If that's intentional, I'd love to understand the rationale.

Note: I hit this while testing a library I authored, https://pypi.python.org/pypi/bidict, which provides a https://en.wikipedia.org/wiki/Bidirectional_map implementation for Python, so I'm especially keen to understand all the subtleties in this area.

Thanks in advance.
msg244078 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-05-26 03:07
This question looks similar to:

Should list compare equal to set when the items are equal?
msg244079 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-05-26 03:21
This looks like a bug in Python 2.7:

# Python2.7
>>> from collections import Set
>>> isinstance({1:2}.viewitems(), Set)
False

# Python3.5
>>> from collections import Set
>>> isinstance({1:2}.items(), Set)
True

I think the dictitems object needs to be registered as a Set.
msg244081 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-05-26 03:57
The fix looks something like this:

diff --git a/Lib/_abcoll.py b/Lib/_abcoll.py
--- a/Lib/_abcoll.py
+++ b/Lib/_abcoll.py
@@ -473,6 +473,7 @@
         for key in self._mapping:
             yield (key, self._mapping[key])
 
+ItemsView.register(type({}.viewitems()))

Will add a more thorough patch with tests later.
msg244090 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-05-26 08:09
I don't know if it is worth to backport this feature (dict views were registered in 1f024a95e9d9), but the patch itself LGTM. I think tests should be foreported to 3.x (if they don't exist in 3.x).

Are there generic set tests similar to mapping_tests and seq_tests?
msg244092 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-05-26 08:36
New changeset 9213c70c67d2 by Raymond Hettinger in branch '2.7':
Issue #24286: Register dict views with the MappingView ABCs.
https://hg.python.org/cpython/rev/9213c70c67d2
msg244093 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-05-26 08:48
New changeset ff8b603ee51e by Raymond Hettinger in branch 'default':
Issue #24286:  Forward port dict view abstract base class tests.
https://hg.python.org/cpython/rev/ff8b603ee51e
msg244094 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-05-26 08:50
> I don't know if it is worth to backport this feature 

I don't think so either.  The Iterator registry is a bit of a waste.


> Are there generic set tests similar to mapping_tests and seq_tests?

Not that I know of.  Also, I don't see the need.
History
Date User Action Args
2015-05-26 08:50:42rhettingersetstage: resolved
2015-05-26 08:50:04rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg244094
2015-05-26 08:48:09python-devsetmessages: + msg244093
2015-05-26 08:36:25python-devsetnosy: + python-dev
messages: + msg244092
2015-05-26 08:09:30serhiy.storchakasetmessages: + msg244090
2015-05-26 04:53:51larrysetnosy: + eric.snow
2015-05-26 04:23:35rhettingersetfiles: + fix_view_registry.diff
keywords: + patch
2015-05-26 03:57:27rhettingersettype: behavior
messages: + msg244081
components: + Library (Lib)
2015-05-26 03:21:43rhettingersetmessages: + msg244079
2015-05-26 03:07:24serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg244078
2015-05-26 00:55:23rhettingersetassignee: rhettinger

nosy: + rhettinger
2015-05-25 23:28:07jabcreate