From 48a4064bbb72fc3cc0cc10d14a5f86ec33894a4d Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Wed, 16 Nov 2022 18:32:39 -0500 Subject: [PATCH 1/9] Fix: Treat Generic classes as not being is_list_like --- doc/source/whatsnew/v1.5.2.rst | 2 +- pandas/_libs/lib.pyx | 7 ++++++- pandas/tests/dtypes/test_inference.py | 23 ++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index 540ca2b12165c..ada1f7f5e0eff 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -27,7 +27,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`) -- +- Bug with python 3.11 and creating a subclass that is ``Generic`` (:issue:`49649`) .. --------------------------------------------------------------------------- .. _whatsnew_152.other: diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 3769bbf087fee..e396a8cbd965c 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1,7 +1,10 @@ from collections import abc from decimal import Decimal from enum import Enum -from typing import Literal +from typing import ( + Literal, + _GenericAlias, +) cimport cython from cpython.datetime cimport ( @@ -1100,6 +1103,8 @@ cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1: and not (hasattr(obj, "ndim") and obj.ndim == 0) # exclude sets if allow_sets is False and not (allow_sets is False and isinstance(obj, abc.Set)) + # exclude Generic types that have __iter__ + and not isinstance(obj, _GenericAlias) ) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index df2afad51abf8..f899d8685ae43 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -18,7 +18,11 @@ from numbers import Number import re import sys -from typing import Iterator +from typing import ( + Generic, + Iterator, + TypeVar, +) import numpy as np import pytest @@ -229,6 +233,23 @@ 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 Base: + def __init__(self, x: int): + self._x = x + + class Gen(Base, Generic[T]): + ... + + fooc = Gen[float] + + assert not inference.is_list_like(fooc) + + def test_is_sequence(): is_seq = inference.is_sequence assert is_seq((1, 2)) From dd097c3381095efeebacec110ad390c06cd47196 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Thu, 17 Nov 2022 07:13:43 -0500 Subject: [PATCH 2/9] tst: update test to test subclass of DataFrame --- pandas/tests/dtypes/test_inference.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index f899d8685ae43..4f6769c453b56 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -238,16 +238,14 @@ def test_is_list_like_generic(): # is_list_like was yielding false positives for Generic classes in python 3.11 T = TypeVar("T") - class Base: - def __init__(self, x: int): - self._x = x - - class Gen(Base, Generic[T]): + class MyDataFrame(DataFrame, Generic[T]): ... - fooc = Gen[float] + tstc = MyDataFrame[int] + tst = MyDataFrame[int]({"x": [1, 2, 3]}) - assert not inference.is_list_like(fooc) + assert not inference.is_list_like(tstc) + assert isinstance(tst, DataFrame) def test_is_sequence(): From a36d1c066f0f31196d0f8318c5a98a69246e593e Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Thu, 17 Nov 2022 12:42:06 -0500 Subject: [PATCH 3/9] add is_list_like test for instance of generic dataframe --- pandas/tests/dtypes/test_inference.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 4f6769c453b56..0faa2d83f61d1 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -246,6 +246,7 @@ class MyDataFrame(DataFrame, Generic[T]): assert not inference.is_list_like(tstc) assert isinstance(tst, DataFrame) + assert inference.is_list_like(tst) def test_is_sequence(): From 5260e4cff5076c6aaeb4883a0835988e74d2a7e3 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Thu, 17 Nov 2022 12:45:06 -0500 Subject: [PATCH 4/9] combine isinstance tests --- pandas/_libs/lib.pyx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index e396a8cbd965c..95c3895bf8726 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1098,13 +1098,12 @@ 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 and not (allow_sets is False and isinstance(obj, abc.Set)) - # exclude Generic types that have __iter__ - and not isinstance(obj, _GenericAlias) ) From c62461db793d5cd7038b5b0cb43a931b6eb45038 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Thu, 17 Nov 2022 17:08:50 -0500 Subject: [PATCH 5/9] update whatsnew with clearer message --- doc/source/whatsnew/v1.5.2.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index ada1f7f5e0eff..13c4a88de6dff 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -27,8 +27,8 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`) -- Bug with python 3.11 and creating a subclass that is ``Generic`` (:issue:`49649`) - +- Fixed bug when instantiating a ``DataFrame`` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:49649) +- .. --------------------------------------------------------------------------- .. _whatsnew_152.other: From b0f1561ca8c9a5d0b1d959b3f20cd0fd8280c024 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Fri, 18 Nov 2022 13:53:08 -0500 Subject: [PATCH 6/9] Update whatsnew to reference DataFrame as a class Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v1.5.2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index 13c4a88de6dff..a59542262c6c6 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -27,7 +27,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`) -- Fixed bug when instantiating a ``DataFrame`` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:49649) +- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`) - .. --------------------------------------------------------------------------- .. _whatsnew_152.other: From 5d12861aa3ef3f5c93b3da8c3a86804502081b89 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Sat, 19 Nov 2022 23:33:20 -0500 Subject: [PATCH 7/9] fix whatsnew blank line --- doc/source/whatsnew/v1.5.2.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index 13c4a88de6dff..c7c7478c6a671 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -27,8 +27,9 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`) -- Fixed bug when instantiating a ``DataFrame`` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:49649) +- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:49649) - + .. --------------------------------------------------------------------------- .. _whatsnew_152.other: From 6b214139043a144da2714a40a56b9c81018a7925 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Sat, 19 Nov 2022 23:34:10 -0500 Subject: [PATCH 8/9] fix whatsnew issue quotes --- doc/source/whatsnew/v1.5.2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index c7c7478c6a671..fbd10fae7ca0e 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -27,7 +27,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`) -- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:49649) +- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`) - .. --------------------------------------------------------------------------- From a4142ad6175c279af2a0d4321d0fcfedba18e7c1 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Sun, 11 Dec 2022 12:00:33 -0500 Subject: [PATCH 9/9] move whatsnew comment from 1.5.2 to 1.5.3 --- doc/source/whatsnew/v1.5.2.rst | 1 - doc/source/whatsnew/v1.5.3.rst | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index ba878fed567c3..e53037d5ee671 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -29,7 +29,6 @@ Bug fixes ~~~~~~~~~ - Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`) - Fixed memory leak in :meth:`.Styler.to_excel` (:issue:`49751`) -- 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/doc/source/whatsnew/v1.5.3.rst b/doc/source/whatsnew/v1.5.3.rst index c739c2f3656c5..0eb71f84786f8 100644 --- a/doc/source/whatsnew/v1.5.3.rst +++ b/doc/source/whatsnew/v1.5.3.rst @@ -27,6 +27,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in :meth:`.Styler.to_excel` leading to error when unrecognized ``border-style`` (e.g. ``"hair"``) provided to Excel writers (:issue:`48649`) +- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`) - .. ---------------------------------------------------------------------------