Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderedDict views don't implement __reversed__ #63704

ThiefMaster mannequin opened this issue Nov 5, 2013 · 6 comments

OrderedDict views don't implement __reversed__ #63704

ThiefMaster mannequin opened this issue Nov 5, 2013 · 6 comments
stdlib Python modules in the Lib dir type-feature A feature request or enhancement


Copy link

ThiefMaster mannequin commented Nov 5, 2013

BPO 19505
Nosy @rhettinger, @ericsnowcurrently, @serhiy-storchaka, @ThiefMaster
  • OrderedDict_reversed_views.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = ''
    closed_at = <Date 2014-04-04.12:47:57.507>
    created_at = <Date 2013-11-05.16:17:58.155>
    labels = ['type-feature', 'library']
    title = "OrderedDict views don't implement __reversed__"
    updated_at = <Date 2014-04-04.12:47:57.507>
    user = '' fields:

    activity = <Date 2014-04-04.12:47:57.507>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2014-04-04.12:47:57.507>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2013-11-05.16:17:58.155>
    creator = 'ThiefMaster'
    dependencies = []
    files = ['32518']
    hgrepos = []
    issue_num = 19505
    keywords = ['patch']
    message_count = 6.0
    messages = ['202221', '202247', '202290', '215424', '215514', '215516']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'stutzbach', 'python-dev', 'eric.snow', 'serhiy.storchaka', 'ThiefMaster']
    pr_nums = []
    priority = 'low'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = ''
    versions = ['Python 3.5']

    Copy link
    Mannequin Author

    ThiefMaster mannequin commented Nov 5, 2013

    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

    @ThiefMaster ThiefMaster mannequin added the stdlib Python modules in the Lib dir label Nov 5, 2013
    Copy link

    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.


    @rhettinger rhettinger added the type-feature A feature request or enhancement label Nov 6, 2013
    Copy link

    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.

    Copy link

    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.

    Copy link

    python-dev mannequin commented Apr 4, 2014

    New changeset cee010fecdf5 by Serhiy Storchaka in branch 'default':
    Issue bpo-19505: The items, keys, and values views of OrderedDict now support

    Copy link

    Done. Thank you Raymond for your review.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    None yet

    No branches or pull requests

    3 participants