Skip to content

Commit baa1032

Browse files
authored
BUG: Fix regression in is_list_like (#43373)
1 parent 8eb2425 commit baa1032

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.3.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Fixed regressions
2020
- Fixed regression in :meth:`merge` where ``on`` columns with ``ExtensionDtype`` or ``bool`` data types were cast to ``object`` in ``right`` and ``outer`` merge (:issue:`40073`)
2121
- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`)
2222
- Fixed regression in :meth:`read_parquet` where the ``fastparquet`` engine would not work properly with fastparquet 0.7.0 (:issue:`43075`)
23+
- Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`)
2324

2425
.. ---------------------------------------------------------------------------
2526

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/dtypes/test_inference.py

+12
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ def foo():
150150
foo()
151151

152152

153+
def test_is_list_like_iter_is_none():
154+
# GH 43373
155+
# is_list_like was yielding false positives with __iter__ == None
156+
class NotListLike:
157+
def __getitem__(self, item):
158+
return self
159+
160+
__iter__ = None
161+
162+
assert not inference.is_list_like(NotListLike())
163+
164+
153165
def test_is_sequence():
154166
is_seq = inference.is_sequence
155167
assert is_seq((1, 2))

0 commit comments

Comments
 (0)