Skip to content

Commit 10e97e4

Browse files
committed
Add isiterable test cases for objects with __getitem__ method and with __iter__ set to None
1 parent 5a3b8b1 commit 10e97e4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

toolz/tests/test_itertoolz.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,34 @@ def test_unique():
106106

107107

108108
def test_isiterable():
109+
# objects that have a __iter__() or __getitem__() method are iterable
110+
# https://docs.python.org/3/library/functions.html#iter
111+
class IterIterable:
112+
def __iter__(self):
113+
return iter(["a", "b", "c"])
114+
115+
class GetItemIterable:
116+
def __getitem__(self, item):
117+
return ["a", "b", "c"][item]
118+
119+
# "if a class sets __iter__() to None, the class is not iterable"
120+
# https://docs.python.org/3/reference/datamodel.html#special-method-names
121+
class NotIterable:
122+
__iter__ = None
123+
124+
class NotIterableEvenWithGetItem:
125+
__iter__ = None
126+
127+
def __getitem__(self, item):
128+
return ["a", "b", "c"][item]
129+
109130
assert isiterable([1, 2, 3]) is True
110131
assert isiterable('abc') is True
132+
assert isiterable(IterIterable()) is True
133+
assert isiterable(GetItemIterable()) is True
111134
assert isiterable(5) is False
135+
assert isiterable(NotIterable()) is False
136+
assert isiterable(NotIterableEvenWithGetItem()) is False
112137

113138

114139
def test_isdistinct():

0 commit comments

Comments
 (0)