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
Comments
The view objects for >>> 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 |
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 |
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. |
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. |
New changeset cee010fecdf5 by Serhiy Storchaka in branch 'default': |
Done. Thank you Raymond for your review. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: