Skip to content

Commit f9566aa

Browse files
authored
Backport PR #49736 on branch 1.5.x (Fix: Treat Generic classes as not being is_list_like) (#50233)
* Fix: Treat Generic classes as not being is_list_like (#49736) * Fix: Treat Generic classes as not being is_list_like * tst: update test to test subclass of DataFrame * add is_list_like test for instance of generic dataframe * combine isinstance tests * update whatsnew with clearer message * Update whatsnew to reference DataFrame as a class Co-authored-by: Matthew Roeschke <[email protected]> * fix whatsnew blank line * fix whatsnew issue quotes * move whatsnew comment from 1.5.2 to 1.5.3 Co-authored-by: Matthew Roeschke <[email protected]> (cherry picked from commit e37040a) * Force checks * put back import warnings in lib.pyx - got deleted when merged
1 parent ea73719 commit f9566aa

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

doc/source/whatsnew/v1.5.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Bug fixes
2828
~~~~~~~~~
2929
- Bug in :meth:`.Styler.to_excel` leading to error when unrecognized ``border-style`` (e.g. ``"hair"``) provided to Excel writers (:issue:`48649`)
3030
- Bug when chaining several :meth:`.Styler.concat` calls, only the last styler was concatenated (:issue:`49207`)
31+
- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`)
3132
-
3233

3334
.. ---------------------------------------------------------------------------

pandas/_libs/lib.pyx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from collections import abc
22
from decimal import Decimal
33
from enum import Enum
4-
from typing import Literal
4+
from typing import (
5+
Literal,
6+
_GenericAlias,
7+
)
58
import warnings
69

710
cimport cython
@@ -1136,7 +1139,8 @@ cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1:
11361139
# equiv: `isinstance(obj, abc.Iterable)`
11371140
getattr(obj, "__iter__", None) is not None and not isinstance(obj, type)
11381141
# we do not count strings/unicode/bytes as list-like
1139-
and not isinstance(obj, (str, bytes))
1142+
# exclude Generic types that have __iter__
1143+
and not isinstance(obj, (str, bytes, _GenericAlias))
11401144
# exclude zero-dimensional duck-arrays, effectively scalars
11411145
and not (hasattr(obj, "ndim") and obj.ndim == 0)
11421146
# exclude sets if allow_sets is False

pandas/tests/dtypes/test_inference.py

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
from numbers import Number
1919
import re
2020
import sys
21+
from typing import (
22+
Generic,
23+
TypeVar,
24+
)
2125

2226
import numpy as np
2327
import pytest
@@ -228,6 +232,22 @@ def __getitem__(self, item):
228232
assert not inference.is_list_like(NotListLike())
229233

230234

235+
def test_is_list_like_generic():
236+
# GH 49649
237+
# is_list_like was yielding false positives for Generic classes in python 3.11
238+
T = TypeVar("T")
239+
240+
class MyDataFrame(DataFrame, Generic[T]):
241+
...
242+
243+
tstc = MyDataFrame[int]
244+
tst = MyDataFrame[int]({"x": [1, 2, 3]})
245+
246+
assert not inference.is_list_like(tstc)
247+
assert isinstance(tst, DataFrame)
248+
assert inference.is_list_like(tst)
249+
250+
231251
def test_is_sequence():
232252
is_seq = inference.is_sequence
233253
assert is_seq((1, 2))

0 commit comments

Comments
 (0)