[issue10565] isinstance(x, collections.Iterator) can return True, when x isn't iterable

Daniel Urban report at bugs.python.org
Sun Nov 28 16:58:27 CET 2010


New submission from Daniel Urban <urban.dani+py at gmail.com>:

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.

----------
components: Library (Lib)
messages: 122668
nosy: durban, rhettinger, stutzbach
priority: normal
severity: normal
status: open
title: isinstance(x, collections.Iterator) can return True, when x isn't iterable
type: behavior
versions: Python 3.1, Python 3.2

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


More information about the Python-bugs-list mailing list