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

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

OrderedDict views don't implement __reversed__ #63704

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

Comments

@ThiefMaster
Copy link
Mannequin

ThiefMaster mannequin commented Nov 5, 2013

BPO 19505
Nosy @rhettinger, @ericsnowcurrently, @serhiy-storchaka, @ThiefMaster
Files
  • 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 = 'https://github.com/serhiy-storchaka'
    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 = 'https://github.com/ThiefMaster'

    bugs.python.org 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 = 'https://bugs.python.org/issue19505'
    versions = ['Python 3.5']

    @ThiefMaster
    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
    @ericsnowcurrently
    Copy link
    Member

    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.

    [1] http://docs.python.org/3/glossary.html#term-view
    [2] http://docs.python.org/3/library/stdtypes.html#dict-views
    [3] http://docs.python.org/3/library/collections.abc.html#collections.abc.MappingView
    [4] http://hg.python.org/cpython/file/3.3/Lib/collections/abc.py#l435
    [5] http://docs.python.org/3.3/library/collections.html#collections.OrderedDict

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

    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.

    @rhettinger
    Copy link
    Contributor

    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.

    @python-dev
    Copy link
    Mannequin

    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
    http://hg.python.org/cpython/rev/cee010fecdf5

    @serhiy-storchaka
    Copy link
    Member

    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
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants