Skip to content

Commit 74d6ec7

Browse files
committed
Fix: Treat Generic classes as not being is_list_like (pandas-dev#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)
1 parent ea73719 commit 74d6ec7

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
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-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from collections import abc
22
from decimal import Decimal
33
from enum import Enum
4-
from typing import Literal
5-
import warnings
4+
from typing import (
5+
Literal,
6+
_GenericAlias,
7+
)
68

79
cimport cython
810
from cpython.datetime cimport (
@@ -1136,7 +1138,8 @@ cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1:
11361138
# equiv: `isinstance(obj, abc.Iterable)`
11371139
getattr(obj, "__iter__", None) is not None and not isinstance(obj, type)
11381140
# we do not count strings/unicode/bytes as list-like
1139-
and not isinstance(obj, (str, bytes))
1141+
# exclude Generic types that have __iter__
1142+
and not isinstance(obj, (str, bytes, _GenericAlias))
11401143
# exclude zero-dimensional duck-arrays, effectively scalars
11411144
and not (hasattr(obj, "ndim") and obj.ndim == 0)
11421145
# 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)