Skip to content

Commit de79463

Browse files
committed
BUG: Fix regression in is_list_like
is_list_like() used to use isinstance(obj, abc.Iterable) before pandas-dev#39852 where it was optimized for performance. This resulted in a regression where objects that explicitly declare __iter__ as None are considered "list like" but not instances of abc.Iterable.
1 parent 58059c1 commit de79463

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ def is_list_like(obj: object, allow_sets: bool = True) -> bool:
10921092
cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1:
10931093
return (
10941094
# equiv: `isinstance(obj, abc.Iterable)`
1095-
hasattr(obj, "__iter__") and not isinstance(obj, type)
1095+
getattr(obj, "__iter__", None) is not None and not isinstance(obj, type)
10961096
# we do not count strings/unicode/bytes as list-like
10971097
and not isinstance(obj, (str, bytes))
10981098
# exclude zero-dimensional numpy arrays, effectively scalars

pandas/tests/libs/test_lib.py

+7
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,10 @@ def test_no_default_pickle():
206206
# GH#40397
207207
obj = tm.round_trip_pickle(lib.no_default)
208208
assert obj is lib.no_default
209+
210+
211+
def test_is_list_like_iter_is_none():
212+
class NotListLike:
213+
__iter__ = None
214+
215+
assert not lib.is_list_like(NotListLike())

0 commit comments

Comments
 (0)