Skip to content

Commit e37040a

Browse files
Dr-Irvmroeschke
andauthored
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]>
1 parent b5953aa commit e37040a

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-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-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

69
cimport cython
710
from cpython.datetime cimport (
@@ -1119,7 +1122,8 @@ cdef bint c_is_list_like(object obj, bint allow_sets) except -1:
11191122
# equiv: `isinstance(obj, abc.Iterable)`
11201123
getattr(obj, "__iter__", None) is not None and not isinstance(obj, type)
11211124
# we do not count strings/unicode/bytes as list-like
1122-
and not isinstance(obj, (str, bytes))
1125+
# exclude Generic types that have __iter__
1126+
and not isinstance(obj, (str, bytes, _GenericAlias))
11231127
# exclude zero-dimensional duck-arrays, effectively scalars
11241128
and not (hasattr(obj, "ndim") and obj.ndim == 0)
11251129
# exclude sets if allow_sets is False

pandas/tests/dtypes/test_inference.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
from numbers import Number
1919
import re
2020
import sys
21-
from typing import Iterator
21+
from typing import (
22+
Generic,
23+
Iterator,
24+
TypeVar,
25+
)
2226

2327
import numpy as np
2428
import pytest
@@ -229,6 +233,22 @@ def __getitem__(self, item):
229233
assert not inference.is_list_like(NotListLike())
230234

231235

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

0 commit comments

Comments
 (0)