Issue #10565: Iterator ABC should require both __next__ and __iter__.
authorRaymond Hettinger <python@rcn.com>
Mon, 29 Nov 2010 03:56:12 +0000 (03:56 +0000)
committerRaymond Hettinger <python@rcn.com>
Mon, 29 Nov 2010 03:56:12 +0000 (03:56 +0000)
Lib/_abcoll.py
Lib/test/test_collections.py
Misc/NEWS

index cac06e0825f48bd55cbdb5fddacff0fd116aa128..0957553613b770b9376511f39d0cf02e1e4c49c8 100644 (file)
@@ -90,7 +90,8 @@ class Iterator(Iterable):
     @classmethod
     def __subclasshook__(cls, C):
         if cls is Iterator:
-            if any("__next__" in B.__dict__ for B in C.__mro__):
+            if (any("__next__" in B.__dict__ for B in C.__mro__) and
+                any("__iter__" in B.__dict__ for B in C.__mro__)):
                 return True
         return NotImplemented
 
index f3f8b031561057c488d4192ce0485a871f168afc..02b9dc31cea8f2f955ba334f7da2cca7feba36f5 100644 (file)
@@ -356,8 +356,14 @@ class TestOneTrickPonyABCs(ABCTestCase):
         for x in samples:
             self.assertIsInstance(x, Iterator)
             self.assertTrue(issubclass(type(x), Iterator), repr(type(x)))
-        self.validate_abstract_methods(Iterator, '__next__')
-        self.validate_isinstance(Iterator, '__next__')
+        self.validate_abstract_methods(Iterator, '__next__', '__iter__')
+
+        # Issue 10565
+        class NextOnly:
+            def __next__(self):
+                yield 1
+                raise StopIteration
+        self.assertNotIsInstance(NextOnly(), Iterator)
 
     def test_Sized(self):
         non_samples = [None, 42, 3.14, 1j,
index 27c733166c9ca39a5c118d376693563d6cacc9b2..79171a53978bbf8d642021c3fde9f3bdf0ec32fa 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10565: The collections.Iterator ABC now checks for both
+  __iter__ and __next__.
+
 - Issue #10242: Fixed implementation of unittest.ItemsEqual and gave it
   a new more informative name, unittest.CountEqual.