[issue25864] collections.abc.Mapping should include a __reversed__ that raises TypeError

Martin Panter report at bugs.python.org
Fri Jan 1 18:05:28 EST 2016


Martin Panter added the comment:

IMO allowing any special method to be set to None seems to make more trouble than it is worth. Are there practical problems to address, or are they all theoretical?

Ideally I think it would be better to require __reversed__() for reverse() to work, but such a change would break compatibility.

Regarding test_enumerate.py, your class looks like this:

class Blocked(object):
    def __getitem__(self): return 1
    def __len__(self): return 2
    __reversed__ = None

The signature of __getitem__() is wrong, and causes a TypeError during iteration, although your particular test does not go that far. When I see someone using assertRaises() with a common exception like TypeError, I instinctively suggest checking the message to avoid these kind of test case bugs.

I suggest either remove __getitem__() if it serves no purpose, or change it to something like this if you really want an unreversible sequence:

def __getitem__(self, index):
    return (1, 1)[index]

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25864>
_______________________________________


More information about the Python-bugs-list mailing list