From 74d6ec7a3500c50e566328dd9554dd96343e2f10 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Mon, 12 Dec 2022 20:44:54 -0500 Subject: [PATCH 1/3] 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 <10647082+mroeschke@users.noreply.github.com> * fix whatsnew blank line * fix whatsnew issue quotes * move whatsnew comment from 1.5.2 to 1.5.3 Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> (cherry picked from commit e37040a54b89ebb6a1c33dbde700b57900912ff4) --- doc/source/whatsnew/v1.5.3.rst | 1 + pandas/_libs/lib.pyx | 9 ++++++--- pandas/tests/dtypes/test_inference.py | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.5.3.rst b/doc/source/whatsnew/v1.5.3.rst index cb99495b837b2..a0c38f1f81538 100644 --- a/doc/source/whatsnew/v1.5.3.rst +++ b/doc/source/whatsnew/v1.5.3.rst @@ -28,6 +28,7 @@ Bug fixes ~~~~~~~~~ - Bug in :meth:`.Styler.to_excel` leading to error when unrecognized ``border-style`` (e.g. ``"hair"``) provided to Excel writers (:issue:`48649`) - Bug when chaining several :meth:`.Styler.concat` calls, only the last styler was concatenated (:issue:`49207`) +- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 439136d17fed2..7525033b3c168 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1,8 +1,10 @@ from collections import abc from decimal import Decimal from enum import Enum -from typing import Literal -import warnings +from typing import ( + Literal, + _GenericAlias, +) cimport cython from cpython.datetime cimport ( @@ -1136,7 +1138,8 @@ cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1: # equiv: `isinstance(obj, abc.Iterable)` getattr(obj, "__iter__", None) is not None and not isinstance(obj, type) # we do not count strings/unicode/bytes as list-like - and not isinstance(obj, (str, bytes)) + # exclude Generic types that have __iter__ + and not isinstance(obj, (str, bytes, _GenericAlias)) # exclude zero-dimensional duck-arrays, effectively scalars and not (hasattr(obj, "ndim") and obj.ndim == 0) # exclude sets if allow_sets is False diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index f08d6b8c9feb8..948d14c1bd1f9 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -18,6 +18,10 @@ from numbers import Number import re import sys +from typing import ( + Generic, + TypeVar, +) import numpy as np import pytest @@ -228,6 +232,22 @@ def __getitem__(self, item): assert not inference.is_list_like(NotListLike()) +def test_is_list_like_generic(): + # GH 49649 + # is_list_like was yielding false positives for Generic classes in python 3.11 + T = TypeVar("T") + + class MyDataFrame(DataFrame, Generic[T]): + ... + + tstc = MyDataFrame[int] + tst = MyDataFrame[int]({"x": [1, 2, 3]}) + + assert not inference.is_list_like(tstc) + assert isinstance(tst, DataFrame) + assert inference.is_list_like(tst) + + def test_is_sequence(): is_seq = inference.is_sequence assert is_seq((1, 2)) From 4bbbb2ee9c67f79e6a67995d3500e17398a4eab4 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Tue, 13 Dec 2022 09:55:16 -0500 Subject: [PATCH 2/3] Force checks From c17fd92a6d8ef31937e09d101ecd15e9fc14c995 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Tue, 13 Dec 2022 10:26:24 -0500 Subject: [PATCH 3/3] put back import warnings in lib.pyx - got deleted when merged --- pandas/_libs/lib.pyx | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 7525033b3c168..d2c2697c05812 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -5,6 +5,7 @@ from typing import ( Literal, _GenericAlias, ) +import warnings cimport cython from cpython.datetime cimport (