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) *  |
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) *  |
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) *  |
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) *  |
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)  |
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)  |
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) *  |
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. |
|
|