This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: OrderedDict views don't implement __reversed__
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: ThiefMaster, eric.snow, python-dev, rhettinger, serhiy.storchaka, stutzbach
Priority: low Keywords: patch

Created on 2013-11-05 16:17 by ThiefMaster, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
OrderedDict_reversed_views.patch serhiy.storchaka, 2013-11-06 21:29 review
Messages (6)
msg202221 - (view) Author: ThiefMaster (ThiefMaster) Date: 2013-11-05 16:17
The view objects for `collections.OrderedDict` do not implement `__reversed__` so something like this fails:

    >>> from collections import OrderedDict
    >>> od = OrderedDict()
    >>> reversed(od.viewvalues())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: argument to reversed() must be a sequence
msg202247 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2013-11-05 23:41
The view objects aren't sequences.  od.items() and od.keys() implement Set.  od.values() doesn't even do that much, only implementing __len__(), __iter__(), and __contains__().

The glossary implies that you should use "reversed(list(view))". [1]  More information on mapping views is located in the docs for collections.ABC and for dict. [2][3]  The source for the Mapping views is also helpful. [4]

Keep in mind that OrderedDict is not a sequence-like dict.  It is essentially just a dict with a well-defined iteration order (by insertion order). [5]  Just like its views, it should not used as a sequence.

msg202290 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-11-06 21:29
We can't add __reversed__() to the Set or MappingView protocols without breaking third party code, but we can add it to concrete implementations of mapping views. In particular for views of OrderedDict which itself already have __reversed__().

Here is a patch which makes OrderedDict's views reversible.
msg215424 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-04-03 05:57
This is approved.  Go ahead and apply the patch.

One minor nit, please position the three new views classes before the _Link class rather than after.
msg215514 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-04-04 12:21
New changeset cee010fecdf5 by Serhiy Storchaka in branch 'default':
Issue #19505: The items, keys, and values views of OrderedDict now support
msg215516 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-04-04 12:47
Done. Thank you Raymond for your review.
Date User Action Args
2022-04-11 14:57:53adminsetgithub: 63704
2014-04-04 12:47:57serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg215516

stage: patch review -> resolved
2014-04-04 12:21:05python-devsetnosy: + python-dev
messages: + msg215514
2014-04-03 05:57:03rhettingersetassignee: rhettinger -> serhiy.storchaka
messages: + msg215424
versions: + Python 3.5, - Python 3.4
2013-11-06 21:29:54serhiy.storchakasetfiles: + OrderedDict_reversed_views.patch
keywords: + patch
messages: + msg202290

stage: patch review
2013-11-06 20:26:09rhettingersetpriority: normal -> low
type: enhancement
versions: - Python 2.7, Python 3.3
2013-11-05 23:41:08eric.snowsetnosy: + eric.snow
messages: + msg202247
2013-11-05 21:07:27serhiy.storchakasetnosy: + stutzbach, serhiy.storchaka

versions: + Python 3.3, Python 3.4, - Python 3.1, Python 3.2
2013-11-05 20:45:55benjamin.petersonsetassignee: rhettinger

nosy: + rhettinger
2013-11-05 16:17:58ThiefMastercreate