Issue 24286: Should OrderedDict.viewitems compare equal to dict.viewitems when the items are equal? (original) (raw)

Created on 2015-05-25 23:28 by jab, last changed 2022-04-11 14:58 by admin. 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
2022-04-11 14:58:17 admin set github: 68474
2015-05-26 08:50:42 rhettinger set stage: resolved
2015-05-26 08:50:04 rhettinger set status: open -> closedresolution: fixedmessages: +
2015-05-26 08:48:09 python-dev set messages: +
2015-05-26 08:36:25 python-dev set nosy: + python-devmessages: +
2015-05-26 08:09:30 serhiy.storchaka set messages: +
2015-05-26 04:53:51 larry set nosy: + eric.snow
2015-05-26 04:23:35 rhettinger set files: + fix_view_registry.diffkeywords: + patch
2015-05-26 03:57:27 rhettinger set type: behaviormessages: + components: + Library (Lib)
2015-05-26 03:21:43 rhettinger set messages: +
2015-05-26 03:07:24 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2015-05-26 00:55:23 rhettinger set assignee: rhettingernosy: + rhettinger
2015-05-25 23:28:07 jab create