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
Comments
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. |
That makes sense. PEP-234 requires |
Fixed in r86857. |
Alexander, do you want to take care of the backport? |
ok |
Committed in r86872 (3.1) and r86873 (2.7). |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: