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

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

Closed
durban mannequin opened this issue Nov 28, 2010 · 6 comments
Closed

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

durban mannequin opened this issue Nov 28, 2010 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@durban
Copy link
Mannequin

durban mannequin commented Nov 28, 2010

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']

@durban
Copy link
Mannequin Author

durban mannequin commented Nov 28, 2010

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.

@durban durban mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 28, 2010
@rhettinger
Copy link
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.

@rhettinger rhettinger self-assigned this Nov 28, 2010
@rhettinger
Copy link
Contributor

Fixed in r86857.
Needs backport.

@rhettinger
Copy link
Contributor

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

@rhettinger rhettinger assigned abalkin and unassigned rhettinger Nov 29, 2010
@abalkin
Copy link
Member

abalkin commented Nov 29, 2010

ok

@abalkin
Copy link
Member

abalkin commented Nov 30, 2010

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

@abalkin abalkin closed this as completed Nov 30, 2010
@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
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants