New submission from Andrew Barnert:

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.

----------
components: Library (Lib)
messages: 257310
nosy: abarnert
priority: normal
severity: normal
status: open
title: collections.abc.Reversible
type: enhancement

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25987>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to