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.

Author abarnert
Recipients abarnert
Date 2016-01-01.22:00:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1451685633.1.0.273350637631.issue25987@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-01-01 22:00:33abarnertsetrecipients: + abarnert
2016-01-01 22:00:33abarnertsetmessageid: <1451685633.1.0.273350637631.issue25987@psf.upfronthosting.co.za>
2016-01-01 22:00:33abarnertlinkissue25987 messages
2016-01-01 22:00:31abarnertcreate