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.

classification
Title: collections.abc.Reversible doesn't fully support the reversing protocol
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: levkivskyi Nosy List: gvanrossum, levkivskyi, pablogsal, rhettinger, serhiy.storchaka, stutzbach
Priority: normal Keywords: patch

Created on 2017-03-05 18:49 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21338 merged pablogsal, 2020-07-05 18:17
Messages (5)
msg289039 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-05 18:49
collections.abc.Reversible doesn't work with types that are supported by reserved() but neither have the __reversed__ method nor are explicitly registered as collections.abc.Sequence. For example:

>>> issubclass(array.array, collections.abc.Reversible)
False 

The reversing protocol as well as the iterating protocol is supported not only by special method, but implicitly if the class has implemented __getitem__ and __len__ methods.

>>> class Counter(int):
...   def __getitem__(s, i): return i
...   def __len__(s): return s
... 
>>> list(reversed(Counter(10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> issubclass(Counter, collections.abc.Reversible)
False

typing.Reversible starves from the same bug. See https://github.com/python/typing/issues/170.
msg289040 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-03-05 19:02
I don't think this is a bug.  The purpose collections.abc.Reversible is to recognize type where the behavior has been guaranteed, not to recognize all types where it happens to work.

Part of the original reason for introducing ABCs in the first place was that in general there is no reliable way to detect whether a class defining __getitem__ is a sequence or a mapping.

A creator of such a class either needs to subclass from Sequence or register as a Sequence.  The fact that reversed(Counter(10)) happens to work is no more interesting that the fact Counter(10)[5] happens to work eventhough isinstance(Counter, Sequence) returns False.
msg289043 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-03-05 19:18
One other thought:  array.array() object should probably be registered so that it recognizable as a Sequence and as Reversible.
msg292265 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2017-04-25 15:31
I'm with Raymond.

I propose to register array.array() in all the appropriate places and then close this bug as a won't fix.

The typing issue might eventually be resolved via PEP 544 (if accepted), or not -- the"fallback protocols" based on __getitem__ and __len__ seem to be a complicating special case that we may want to put off.
msg373050 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-07-05 21:43
New changeset e51dd9dad6590bf3a940723fbbaaf4f64a3c9228 by Pablo Galindo in branch 'master':
bpo-29727: Register array.array as a MutableSequence (GH-21338)
https://github.com/python/cpython/commit/e51dd9dad6590bf3a940723fbbaaf4f64a3c9228
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73913
2021-02-17 15:37:56iritkatriellinkissue25737 superseder
2020-07-05 21:43:40pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-07-05 21:43:17pablogsalsetmessages: + msg373050
2020-07-05 18:17:08pablogsalsetkeywords: + patch
nosy: + pablogsal

pull_requests: + pull_request20486
stage: needs patch -> patch review
2019-02-07 13:09:21cheryl.sabellalinkissue30130 superseder
2017-04-26 11:13:48levkivskyisetassignee: levkivskyi
stage: needs patch
2017-04-25 15:31:05gvanrossumsetmessages: + msg292265
2017-04-25 14:43:21rhettingersetassignee: rhettinger -> (no value)
2017-03-06 07:11:30levkivskyisetnosy: + levkivskyi
2017-03-05 19:18:23rhettingersetmessages: + msg289043
2017-03-05 19:02:38rhettingersetassignee: rhettinger
messages: + msg289040
2017-03-05 18:49:23serhiy.storchakacreate