Skip to content

isinstance(x, collections.Iterator) can return True, when x isn't iterable #54774

Closed

Description

@durban
mannequin
BPO 10565
Nosy @rhettinger, @abalkin, @durban

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/abalkin'
closed_at = <Date 2010-11-30.01:19:59.192>
created_at = <Date 2010-11-28.15:58:23.020>
labels = ['type-bug', 'library']
title = "isinstance(x, collections.Iterator) can return True, when x isn't iterable"
updated_at = <Date 2010-11-30.01:19:59.190>
user = 'https://github.com/durban'

bugs.python.org fields:

activity = <Date 2010-11-30.01:19:59.190>
actor = 'belopolsky'
assignee = 'belopolsky'
closed = True
closed_date = <Date 2010-11-30.01:19:59.192>
closer = 'belopolsky'
components = ['Library (Lib)']
creation = <Date 2010-11-28.15:58:23.020>
creator = 'daniel.urban'
dependencies = []
files = []
hgrepos = []
issue_num = 10565
keywords = []
message_count = 6.0
messages = ['122668', '122702', '122763', '122861', '122865', '122876']
nosy_count = 4.0
nosy_names = ['rhettinger', 'belopolsky', 'stutzbach', 'daniel.urban']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue10565'
versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

Activity

  1. durban commented on Nov 28, 2010

    durbanmannequin
    MannequinAuthor

    If the type of x defines __next__, but not __iter__, isinstance(x, collections.Iterator) returns True, but in fact x isn't iterable.

    >>> class X:
    ...     def __next__(self):
    ...             raise StopIteration()
    ...
    >>> x = X()
    >>> isinstance(x, collections.Iterator)
    True
    >>> issubclass(X, collections.Iterator)
    True
    >>> list(x)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'X' object is not iterable

    The reason for this is that collections.Iterator.__subclasshook__ checks for a __next__ method, and if finds one, returns True. (The class provides an __iter__ mixin method, so this doesn't cause problems for classes inheriting collections.Iterator.)

    A possible solution could be in collections.Iterator.__subclasshook__ checking for both required methods.

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Nov 28, 2010
  3. rhettinger commented on Nov 28, 2010

    @rhettinger
    Contributor

    A possible solution could be in
    collections.Iterator.__subclasshook__
    checking for both required methods.

    That makes sense. PEP-234 requires
    iterators to support both methods.

  4. self-assigned this
    on Nov 28, 2010
  5. rhettinger commented on Nov 29, 2010

    @rhettinger
    Contributor

    Fixed in r86857.
    Needs backport.

  6. rhettinger commented on Nov 29, 2010

    @rhettinger
    Contributor

    Alexander, do you want to take care of the backport?

  7. abalkin commented on Nov 29, 2010

    @abalkin
    Member

    ok

  8. abalkin commented on Nov 30, 2010

    @abalkin
    Member

    Committed in r86872 (3.1) and r86873 (2.7).

  9. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions