Andrew Barnert added the comment:

> Perhaps there is a bug in typing.Reversible. It doesn't accept all types 
> supported by reversed().

> ... And accepts types that don't work with reversed().

The problem is the way the two are defined:

* Reversible is true if you implement __reversed__
* reverse works if you implement __reversed__ or implement the old-style 
sequence protocol.

That explains why it doesn't work on tuple, bytearray, etc. Iterable actually 
has the exact same problem, but, because it's a supertype of Sequence, and we 
have explicit Sequence.register(tuple) and MutableSequence.register(bytearray) 
in collections.abc, and typing.Iterable specifies collections.abc.Iterable as 
its "extra", it all works out.

We could do the same for Reversible: add a collections.abc.Reversible, make it 
a subtype of Iterable and make Sequence a subtype of Reversible instead of 
Iterable, and make that the extra for typing.Reversible. Then it would work for 
all of those builtin types (and many third-party types that explicitly register 
with Sequence), just as Iterable does.

But that only solves the problem in one direction. To solve it in the other 
direction, we'd need some way to either explicitly mark a method as not 
implemented (maybe setting it to None, or to any non-callable, or any data 
descriptor?) that ABC subclass hooks and/or typing checks are expected to 
understand, or unregister a class with an ABC so that it isn't a subtype even 
if it passes the implicit hooks.

Or... could we just drop Reversible as an implicit protocol? The lack of an 
explicit "deny" mechanism for implicit protocols and ABCs is a more general 
problem, but if this is the only actual instance of that problem in real life, 
do we need to solve the general problem? If not, there's no obvious way to 
define typing.Reversible that isn't wrong, it doesn't have a corresponding ABC, 
it doesn't seem like it will be useful often enough to be worth the problems it 
causes, and I doubt there's much real-life code out there already depending on 
it, so that seems a lot easier.

----------

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

Reply via email to