Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collections.abc.Reversible doesn't fully support the reversing protocol #73913

Closed
serhiy-storchaka opened this issue Mar 5, 2017 · 5 comments
Assignees
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 29727
Nosy @gvanrossum, @rhettinger, @serhiy-storchaka, @ilevkivskyi, @pablogsal
PRs
  • bpo-29727: Register array.array as a MutableSequence #21338
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ilevkivskyi'
    closed_at = <Date 2020-07-05.21:43:40.098>
    created_at = <Date 2017-03-05.18:49:23.099>
    labels = ['3.7', 'type-bug', 'library']
    title = "collections.abc.Reversible doesn't fully support the reversing protocol"
    updated_at = <Date 2020-07-05.21:43:40.097>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2020-07-05.21:43:40.097>
    actor = 'pablogsal'
    assignee = 'levkivskyi'
    closed = True
    closed_date = <Date 2020-07-05.21:43:40.098>
    closer = 'pablogsal'
    components = ['Library (Lib)']
    creation = <Date 2017-03-05.18:49:23.099>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29727
    keywords = ['patch']
    message_count = 5.0
    messages = ['289039', '289040', '289043', '292265', '373050']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'rhettinger', 'stutzbach', 'serhiy.storchaka', 'levkivskyi', 'pablogsal']
    pr_nums = ['21338']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue29727'
    versions = ['Python 3.6', 'Python 3.7']

    @serhiy-storchaka
    Copy link
    Member Author

    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 python/typing#170.

    @serhiy-storchaka serhiy-storchaka added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 5, 2017
    @rhettinger
    Copy link
    Contributor

    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.

    @rhettinger rhettinger self-assigned this Mar 5, 2017
    @rhettinger
    Copy link
    Contributor

    One other thought: array.array() object should probably be registered so that it recognizable as a Sequence and as Reversible.

    @rhettinger rhettinger removed their assignment Apr 25, 2017
    @gvanrossum
    Copy link
    Member

    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.

    @ilevkivskyi ilevkivskyi self-assigned this Apr 26, 2017
    @pablogsal
    Copy link
    Member

    New changeset e51dd9d by Pablo Galindo in branch 'master':
    bpo-29727: Register array.array as a MutableSequence (GH-21338)
    e51dd9d

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants