classification
Title: collections.abc.Reversible
Type: enhancement Stage:
Components: Documentation, Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: abarnert, docs@python, gvanrossum, levkivskyi, python-dev
Priority: normal Keywords: patch

Created on 2016-01-01 22:00 by abarnert, last changed 2016-04-18 05:26 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
reversible.patch levkivskyi, 2016-04-02 21:02 A patch following the Andew's proposal review
Messages (10)
msg257310 - (view) Author: Andrew Barnert (abarnert) * Date: 2016-01-01 22:00
This came up as a side issue in the -ideas discussion on deprecating the old-style sequence protocol that came out of Guido's suggestion on https://github.com/ambv/typehinting/issues/170 (http://article.gmane.org/gmane.comp.python.ideas/37599):

> I also think it's fine to introduce Reversible as another ABC and carefully fit it into the existing hierarchy. It should be a one-trick pony and be another base class for Sequence; it should not have a default implementation. (But this has been beaten to death in other threads -- it's time to just file an issue with a patch.)

I'll file a patch this weekend. But in case there's anything to bikeshed, here are the details:

* Reversible is a subclass of Iterable.
 * It has a single abstract method, __reversed__, with no default implementation.
 * Its subclass hook that checks for __reversed__ existing and not being None.
* Sequence is a subclass of Reversible, Sized, and Container rather than directly of Iterable, Sized, and Container.

Builtins tuple and list, and any subclasses of them, will be Reversible because they register with Sequence or MutableSequence. Subclasses of collections.abc.Sequence will be Reversible (and should be, as they inherit Sequence.__reversed__). Custom old-style sequences will not be Reversible, even though reversed works on them.

Builtins dict, set, and frozenset, and any subclasses of them, will not be Reversible (unless they add a __reversed__ method, as OrderedDict does). Subclasses of collections.abc.Mapping will not be Reversible (and should not be, as, assuming #25864 goes through, they inherit Mapping.__reversed__=None) (unless they add a __reversed__ method, as most third-party sorted-dict types do).

I'll include tests for all of those things.

I believe this is all exactly parallel with collections.abc.Iterable, and will make collections.abc.Reversible compatible with typing.Reversible[...] in exactly the same way collections.abc.Iterable is compatible with typing.Iterable[...].

Alternatives: We could make Reversible independent of Iterable. Alternatively, we could make it subclass both Iterable and Sized instead of just Iterable. But I think this is the simplest place to slot it in.
msg257488 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-01-04 20:31
Yes please. Thanks for thinking about all the edge cases here!
msg262481 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-03-26 01:38
Andrew, did you lose interest in this? We now have a PR for typing.py that expects this to be resolved: https://github.com/python/typing/pull/194
msg262800 - (view) Author: Ivan Levkivskyi (levkivskyi) * Date: 2016-04-02 21:02
I was not sure whether Andrew is still interested. I made a simple-minded patch following his proposal. I also added tests and changed docs accordingly. Note that I also changed the expected MRO for MutableSequence in one of the functools tests according to the new hierarchy (otherwise test_functools fails).

Please review.

PS: Some tests was skipped on my machine, here is the list:
    test_bz2 test_curses test_dbm_gnu test_dbm_ndbm test_devpoll
    test_idle test_kqueue test_lzma test_msilib test_ossaudiodev
    test_smtpnet test_socketserver test_ssl test_startfile test_tcl
    test_timeout test_tix test_tk test_ttk_guionly test_ttk_textonly
    test_urllib2net test_urllibnet test_winreg test_winsound
    test_xmlrpc_net test_zipfile64
msg262858 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-04-04 17:41
Looks good. I'll merge this in a sec.
msg262859 - (view) Author: Roundup Robot (python-dev) Date: 2016-04-04 17:59
New changeset 07f73360ea8e by Guido van Rossum in branch 'default':
Add collections.Reversible. Patch by Ivan Levkivskyi. Fixes issue #25987.
https://hg.python.org/cpython/rev/07f73360ea8e
msg262860 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-04-04 18:00
Done. Thanks!
msg263614 - (view) Author: Ivan Levkivskyi (levkivskyi) * Date: 2016-04-17 15:39
I see that I forgot to include .. versionadded:: in the documentation.
Will this go into 3.5.2 or in 3.6?
msg263636 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-04-17 23:02
Because it's a change to collections.abc, it goes in 3.6 only.
msg263642 - (view) Author: Roundup Robot (python-dev) Date: 2016-04-18 05:26
New changeset 7d9f7d7a21ae by Georg Brandl in branch 'default':
#25987: add versionadded to Reversible.
https://hg.python.org/cpython/rev/7d9f7d7a21ae
History
Date User Action Args
2016-04-18 05:26:11python-devsetmessages: + msg263642
2016-04-17 23:02:32gvanrossumsetmessages: + msg263636
2016-04-17 15:39:17levkivskyisetnosy: + docs@python
messages: + msg263614

assignee: docs@python
components: + Documentation
2016-04-04 18:00:23gvanrossumsetstatus: open -> closed
resolution: fixed
messages: + msg262860
2016-04-04 17:59:58python-devsetnosy: + python-dev
messages: + msg262859
2016-04-04 17:41:04gvanrossumsetmessages: + msg262858
2016-04-02 21:02:50levkivskyisetfiles: + reversible.patch
keywords: + patch
messages: + msg262800
2016-03-26 08:58:52levkivskyisetnosy: + levkivskyi
2016-03-26 01:38:47gvanrossumsetmessages: + msg262481
2016-01-04 20:31:37gvanrossumsetnosy: + gvanrossum
messages: + msg257488
2016-01-01 22:00:33abarnertcreate